> ## Documentation Index
> Fetch the complete documentation index at: https://s2.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenTelemetry

> Export OpenTelemetry traces, metrics, and logs to S2 over OTLP/HTTP using direct exporters or the OpenTelemetry Collector.

S2 implements the [OTLP/HTTP](https://opentelemetry.io/docs/specs/otlp/#otlphttp) specification, so it
can be used as a telemetry backend for trace, metric, and log signals.

Signals are routed to the appropriate streams by setting the `s2.stream` [resource](https://opentelemetry.io/docs/concepts/resources/)
attribute. You'd typically set it when initializing your [TracerProvider](https://opentelemetry.io/docs/concepts/signals/traces/#tracer-provider),
[MeterProvider](https://opentelemetry.io/docs/concepts/signals/metrics/#meter-provider),
or [LoggerProvider](https://opentelemetry.io/docs/concepts/signals/logs/#logger-provider).
For example, in Python, if you want all logs from `process-a` to go to `stream-a`, you need to configure it as follows:

```python theme={null}
import logging
from opentelemetry._logs import set_logger_provider
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
from opentelemetry.sdk.resources import Resource

provider = LoggerProvider(resource=Resource.create({"s2.stream": "stream-a"}))
set_logger_provider(provider)
handler = LoggingHandler(level=logging.INFO, logger_provider=provider)
logging.basicConfig(handlers=[handler], level=logging.INFO)
```

You can export your signals directly to a telemetry backend from your processes, but it is generally
recommended to use a [Collector](https://opentelemetry.io/docs/collector/#when-to-use-a-collector).
If you are already using a Collector, you can easily start exporting signals to S2 by using
an [OTLP/HTTP exporter](https://github.com/open-telemetry/opentelemetry-collector/tree/main/exporter/otlphttpexporter).

S2's `OTLP/HTTP` endpoint supports both `json` and `proto` encodings. You need to set the `Content-Type` header to
`application/json` or `application/x-protobuf` accordingly.

Below is our recommended config for the exporter:

```yaml theme={null}
exporters:
  otlphttp:
    endpoint: https://<your-basin>.b.s2.dev/otlp
    encoding: <your-preferred-encoding>
    headers:
      Content-Type: <value-based-on-encoding>
      Authorization: Bearer <your-access-token>
    sending_queue:
      num_consumers: 1
      batch:
        flush_timeout: 50ms
        min_size: 262144 # 256KiB
        max_size: 524288 # 512KiB
        sizer: bytes
```

**Note:**

* If you don't have an access token already, you can generate one from the [dashboard](https://s2.dev/dashboard).
* You need to create a basin with `create-stream-on-append` flag on, if you want streams to be created automatically
  when signals are exported.
* `sending_queue.num_consumers: 1` ensures batches are sent in order to S2. You can increase it if ordering is not a
  concern.
* If you want to change `sending_queue.batch` config, please be aware of the limits \[[1](/platform/limits#records), [2](/platform/limits#appends)]
  and how that will impact your changes.
