
# Export traces with OpenTelemetry

Send a span for every request the gateway serves to any OTLP/HTTP collector, alongside the traces in **Observability**.

Set the collector's address in the gateway's environment and restart the gateway:

```bash
TOKEDEX_OTEL_EXPORTER_OTLP_ENDPOINT='https://collector.example.com'
TOKEDEX_OTEL_EXPORTER_OTLP_HEADERS='Authorization=Bearer <collector-token>'
```

After the next request, the collector receives a span on `/v1/traces`, trimmed here:

```json
{
  "resource": { "attributes": [{ "key": "service.name", "value": { "stringValue": "tokedex-gateway" } }] },
  "spans": [{
    "name": "llm openai/gpt-6-luna",
    "status": { "code": 1 },
    "attributes": [
      { "key": "gen_ai.request.model", "value": { "stringValue": "openai/gpt-6-luna" } },
      { "key": "gen_ai.usage.input_tokens", "value": { "intValue": "14" } },
      { "key": "tokedex.request.id", "value": { "stringValue": "d3c36880-a8a6-431e-80bd-f0ea4639d403" } },
      { "key": "tokedex.cost.usd", "value": { "doubleValue": 1.92e-06 } }
    ]
  }]
}
```

You need: access to the gateway's environment and permission to restart it, and a collector or service that accepts OTLP/HTTP traces. On Kubernetes, set the variables under `extraEnv` in the Helm chart's values, with collector tokens and service keys read from a Secret, as [Deploy to production](/gateway/deploy#supply-provider-credentials) shows for provider keys; for a local gateway started with `npm start`, in the `.env` file in the repository root.

## Collector settings

| Variable | Value |
| --- | --- |
| `TOKEDEX_OTEL_EXPORTER_OTLP_ENDPOINT` | The collector's address. The gateway adds `/v1/traces` unless the address already ends with it. |
| `TOKEDEX_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT` | Used in place of the address above. The gateway also adds `/v1/traces` to it unless it already ends with it, so a collector at another path cannot be reached. |
| `TOKEDEX_OTEL_EXPORTER_OTLP_HEADERS` | Headers sent with each export, as comma-separated `name=value` pairs |
| `TOKEDEX_OTEL_SERVICE_NAME` | The `service.name` on each span. Default `tokedex-gateway`. |

The standard `OTEL_EXPORTER_OTLP_ENDPOINT`, `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT` and `OTEL_EXPORTER_OTLP_HEADERS` work too. The gateway uses the first one set of `TOKEDEX_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT`, `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT`, `TOKEDEX_OTEL_EXPORTER_OTLP_ENDPOINT` and `OTEL_EXPORTER_OTLP_ENDPOINT`. For headers, the `TOKEDEX_` name wins.

## Send traces to a named service

List the services in `TOKEDEX_OTEL_DRAINS`, separated by commas, and set each one's variables. The gateway skips a listed service whose required variables are missing.

```bash
TOKEDEX_OTEL_DRAINS='braintrust'
BRAINTRUST_API_KEY='<your-braintrust-key>'
```

| Service | `TOKEDEX_OTEL_DRAINS` | Required | Optional |
| --- | --- | --- | --- |
| Braintrust | `braintrust` | `BRAINTRUST_API_KEY` | `BRAINTRUST_PROJECT_NAME` (default `tokedex`) |
| Arize | `arize` | `ARIZE_API_KEY`, `ARIZE_SPACE_ID` | `ARIZE_PROJECT_NAME` (default `tokedex`) |
| SigNoz Cloud | `signoz` | `SIGNOZ_INGESTION_KEY` | `TOKEDEX_SIGNOZ_REGION`: `us`, `eu` or `in` |
| Self-hosted SigNoz | `signoz` | `TOKEDEX_SIGNOZ_OTLP_ENDPOINT`, a full `/v1/traces` URL | |
| LangSmith | `langsmith` | `LANGSMITH_API_KEY` | `LANGSMITH_PROJECT` |
| Langfuse | `langfuse` | `LANGFUSE_PUBLIC_KEY`, `LANGFUSE_SECRET_KEY` | `LANGFUSE_BASE_URL` |
| Weights & Biases Weave | `weave` | `WANDB_API_KEY`, `WANDB_ENTITY`, `WANDB_PROJECT` | `WEAVE_BASE_URL` |
| Galileo | `galileo` | `GALILEO_API_KEY`, `GALILEO_PROJECT` | `GALILEO_LOG_STREAM` (default `default`) |
| Datadog | `datadog` | `DD_API_KEY` | `DD_SITE` |
| Honeycomb | `honeycomb` | `HONEYCOMB_API_KEY` | `HONEYCOMB_DATASET`, `HONEYCOMB_REGION=eu` |
| New Relic | `new-relic` | `NEW_RELIC_LICENSE_KEY` | `NEW_RELIC_REGION`: `eu` or `fedramp` |

Use a named service when yours is listed: you set only its key, and the gateway builds its address and headers. Use the collector settings for any other destination, or for an OpenTelemetry Collector of your own that forwards spans onward. A named service and a collector can be used together: the gateway sends each span to every destination.

## Span contents

Each span is named `llm <provider>/<model>`, for the provider and model that answered. It carries OpenTelemetry GenAI attributes, such as `gen_ai.request.model` (the model ID the caller sent) and `gen_ai.usage.input_tokens`, and `tokedex.*` attributes for the request ID, session, key identifier, workspace, tags, tokens, duration, finish reason and cost. Its events record the gateway's routing steps.

A request that falls back sends one span for each attempt, with the same `tokedex.request.id`. A failed attempt has status code `2` and its `error.type`, such as `RateLimit`.

## Prompt and response text

Text is exported only for a request whose text the gateway captures: `TOKEDEX_OBSERVABILITY_TEXT_CAPTURE=true` in the gateway's environment, and `x-tokedex-trace-text: true` on the request. The request and response then appear in the span's `gen_ai.input.messages` and `gen_ai.output.messages` attributes, and a failed request's span carries the provider's error message. The gateway masks the key that authenticated the request before exporting the trace. See [Keep content private](/gateway/recording-and-retention#keep-content-private).

Each of the two attributes is a JSON string in the OpenTelemetry GenAI message format: a list of messages, each with a `role` and typed `parts`, such as `text`, `reasoning`, `tool_call` and `tool_call_response`. A system prompt is the first input message, with the role `system`. The same text is also in the span's events, under `tokedex.event.data_json`.

SigNoz never receives text. Before you turn text capture on, check that every other destination may hold prompt and response text, since each one receives it.

## Data that is not exported

Export follows recording. When recording is off (`TOKEDEX_OBSERVABILITY=0`), or a request sends `x-tokedex-trace: false`, nothing is exported for it.

## Check that export works

1. Restart the gateway with the variables set.
2. Send a request through the gateway. It calls a provider and can incur charges.
3. Find a span named `llm <provider>/<model>` in your collector or service.

If none arrives, read the gateway's log. A failed export logs `OTLP drain "<name>" export failed — span dropped`, with the HTTP status or error. The gateway waits at most 10 seconds for each export and keeps at most 64 in flight to each destination, so a slow collector loses spans, never delays requests. Keep recording on as the complete record, and use the export for analysis that can tolerate a lost span.

## Receive traces at the gateway

Set `TOKEDEX_OBSERVABILITY_INGEST_TOKEN` and restart, and the gateway stores the OTLP/HTTP JSON spans that the SDK or another exporter sends to `POST /observability/v1/traces`, so calls made without the gateway appear in **Observability**.

```bash
curl -sS "$TOKEDEX_URL/observability/v1/traces" \
  -H "Authorization: Bearer $TOKEDEX_INGEST_TOKEN" \
  -H "Content-Type: application/json" \
  --data-binary @spans.json
```

```json
{"object":"observability_ingest","spans":1,"accepted":1}
```

The token can only write spans: it reads nothing, and no other key is accepted on this route. Generate a long random value, give it only to the exporters that send spans, and keep it out of logs. The token is 1 to 512 visible ASCII characters, without commas, and the route exists only while recording is on.

Next: [What the gateway records](/gateway/recording-and-retention) or [Find and inspect a request](/gateway/requests-and-sessions).
