OpenTelemetry
OpenTelemetry is the vendor neutral way to get traces out of the gateway. The otel callback sends one span for every model call, with the model, provider, token usage, cost and timing, to any collector that speaks OTLP, so model calls show up in the same tracing backend as the rest of your services. It is free on every Spanway gateway.
A real-world scenario
A platform team already runs an OpenTelemetry collector that feeds their tracing backend. Their applications call the gateway, and today a slow checkout page shows a gap in its trace where the model call happens. They turn on the otel callback and point it at the collector. The next trace of the checkout page shows a child span for the model call with the model name, the provider, the tokens and the cost, so the team can tell a slow model from a slow database at a glance.
Turn it on
-
Add the callback to the config file:
spanway_settings:callbacks: ["otel"] -
Tell the gateway where the collector is and restart it:
export OTEL_EXPORTER="otlp_http"export OTEL_ENDPOINT="http://collector:4318"export OTEL_HEADERS="authorization=Bearer abc" -
Send one request through the gateway:
curl -X POST http://localhost:4000/v1/chat/completions \-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \-d '{"model": "claude-haiku-5-5", "messages": [{"role": "user", "content": "Hi"}]}' -
Open the collector's backend. Expected result: a span whose service name is
spanway(or yourOTEL_SERVICE_NAME) carries the model, provider, token counts and cost as attributes, andgen_ai.input.messagesis absent because content is off by default.
You can also add the callback from the Admin UI. Open Settings, then Logging & Alerts, click Add Callback, choose Open Telemetry and fill in Endpoint URL, Headers and Export Protocol. Traces Endpoint URL is only read with SPANWAY_OTEL_V2=true and is ignored otherwise. Turning the destination on sends a span for every request, and removing it stops all export at once. Saving registers it as a success callback without a restart. Confirm it is running with GET /callbacks/list, and send a test span with GET /health/services?service=otel.
Two implementations
The gateway ships two OpenTelemetry loggers and uses one at a time. Setting SPANWAY_OTEL_V2=true selects the newer one, which follows the OpenTelemetry gen_ai semantic conventions, adds the destination presets below, optional metrics and events, and spans for the gateway's own HTTP handling. Without it the older logger runs, which most existing setups use. With version 2 the newrelic callback sends OTLP spans through the preset below. That is the only New Relic mode the gateway images support, because they do not include the New Relic Python agent the older mode needs, and New Relic as a team destination is refused unless SPANWAY_OTEL_V2=true.
| Variable | Meaning | Default |
|---|---|---|
OTEL_EXPORTER or OTEL_EXPORTER_OTLP_PROTOCOL | console, otlp_http (also http/protobuf) or otlp_grpc (also grpc). Version 2 also accepts http for otlp_http, and http/json, which sends OTLP JSON for collectors that do not take protobuf. The older logger sends protobuf for http/json and treats http as console. OTEL_EXPORTER_OTLP_PROTOCOL wins when both are set | console, or otlp_http once an endpoint is set |
OTEL_ENDPOINT or OTEL_EXPORTER_OTLP_ENDPOINT | Collector base URL | unset |
OTEL_TRACES_ENDPOINT or OTEL_EXPORTER_OTLP_TRACES_ENDPOINT | Full traces URL, used as given, version 2 only. The older logger ignores it and always sends to the endpoint plus /v1/traces | unset |
OTEL_HEADERS or OTEL_EXPORTER_OTLP_HEADERS | Headers sent to the collector, key=value separated by commas. The second wins when both are set | unset |
OTEL_SERVICE_NAME | service.name of every span | spanway |
OTEL_ENVIRONMENT_NAME | deployment.environment | production with the default logger, unset with SPANWAY_OTEL_V2=true |
SPANWAY_OTEL_INTEGRATION_ENABLE_METRICS | Also record the gen_ai.client and gen_ai.server histograms. Both loggers read it | false |
SPANWAY_OTEL_INTEGRATION_ENABLE_EVENTS | Also emit OpenTelemetry log events, such as one for each failed call. Both loggers read it | false |
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT | Whether prompts and responses are sent, see below | no_content |
More settings
| Variable | Meaning | Default |
|---|---|---|
OTEL_MODEL_ID | The model id resource attribute of every span | the service name |
OTEL_TRACER_NAME, SPANWAY_METER_NAME, SPANWAY_LOGGER_NAME | The instrumentation scope names of spans, metrics and log events | spanway |
OTEL_IGNORE_CONTEXT_PROPAGATION | true ignores incoming traceparent headers, so every request starts its own trace. Use it when callers send trace ids you do not want to join | false |
OTEL_SEMCONV_STABILITY_OPT_IN | Opt in to newer OpenTelemetry semantic convention names, for example gen_ai_latest_experimental | unset |
SPANWAY_OTEL_BAGGAGE_TEAM_METADATA_KEYS | Team metadata keys copied into the trace baggage, so downstream services see them. Only list keys that may leave the gateway | unset |
SPANWAY_OTEL_BAGGAGE_METADATA_KEYS | Request metadata keys copied into the trace baggage, with the same caution | unset |
USE_OTEL_SPANWAY_REQUEST_SPAN | true always adds a spanway_request span, even under a caller's span | false |
OTEL_LOGS_EXPORTER | The exporter for log events when events are on: console, otlp_http or otlp_grpc | the span exporter |
OTEL_EXPORTER_OTLP_CERTIFICATE, OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE | A CA bundle for a collector with its own certificate | unset |
DEBUG_OTEL | true logs every export at DEBUG level, for one investigation only | false |
SPANWAY_OTEL_LEGACY_COMPAT, SPANWAY_OTEL_LANGFUSE_SPAN_SCOPE | Version 2 only. Keep the older attribute names next to the new ones, and llm_only sends only model call spans to Langfuse | true, full |
Settings in config.yaml
callback_settings.otel in the config file sets the same options for the otel callback without environment variables.
spanway_settings:
callbacks: ["otel"]
callback_settings:
otel:
message_logging: false
baggage_team_metadata_keys: ["cost_center"]
attributes:
exclude_list: ["user_api_key_alias"]
message_logging: false keeps prompts and answers off the spans whatever OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT says. baggage_team_metadata_keys and baggage_metadata_keys work like the variables above. attributes takes either an include_list or an exclude_list of attribute names for the OpenTelemetry metrics, to keep their number of series down. Setting both is refused at startup.
What the trace looks like
A request becomes a small tree of spans. When the caller sends a W3C traceparent header, and tracestate if it has one, the gateway joins that trace, so the model call appears under the caller's own span. Without one it starts a new trace.
| Span | When |
|---|---|
Received Proxy Server Request | The gateway's handling of the HTTP request. |
spanway_request | One model call, with model, provider, tokens, cost and timing. Under a caller's span it is only added with USE_OTEL_SPANWAY_REQUEST_SPAN=true, otherwise its attributes go on the request span. |
raw_gen_ai_request | The provider request as sent. Only with content capture on, because it carries the prompt. |
| A guardrail span per guardrail | Each guardrail check with its result and timing. |
redis and postgres service spans | Cache and database calls the gateway made for the request. |
Prompts and responses
By default the otel callback sends no prompt or response text, with either logger. OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT takes no_content, span_only (content as gen_ai.input.messages and gen_ai.output.messages on the span), event_only or span_and_event, and true means event_only. An unknown value counts as no_content. A team or key destination may only choose no_content or span_only. With the older logger, turn_off_message_logging and message_logging: false turn content off whatever this says. The destination presets below exist to show conversations, so with the older logger they keep sending content until you set no_content or turn_off_message_logging. The collector keeps what it receives under its own retention, and a data subject erasure in Spanway does not reach it, so leave content off unless the collector is covered by the same privacy terms.
Destination presets
With version 2 these callback names send to a known backend with the right endpoint, headers and attribute names, in place of otel.
| Callback | Reads |
|---|---|
arize, arize_phoenix | Arize keys, or PHOENIX_PROJECT_NAME |
langfuse_otel | Langfuse keys and host |
newrelic | NEW_RELIC_LICENSE_KEY and NEW_RELIC_REGION (us or eu) |
signoz | SIGNOZ_INGESTION_ENDPOINT and SIGNOZ_INGESTION_KEY |
agentops, langtrace, levo, weave_otel | Their own API keys |
langfuse_otel, arize, weave_otel and newrelic can also be a team or key destination, which needs the scoped_integrations license feature.
Provider attempt spans
With version 2, a model call that retried or fell back gets one child span per provider attempt under its model call span, named attempt <number> <provider model>, with the deployment, the outcome, the HTTP status and error class of a failed try and how long the try took. A call served on the first try gets no extra span. App traces and provider attempts lists the attributes.
Gateway HTTP spans
Version 2 also traces the gateway's own request handling when opentelemetry-instrumentation-fastapi is installed. Health checks, /metrics, the Admin UI and the API docs are skipped by default. Set OTEL_PYTHON_FASTAPI_EXCLUDED_URLS to replace that list, or to an empty string to trace every route.
Errors
| Status | Where | Meaning | Fix |
|---|---|---|---|
| No spans arrive, no error | Collector | OTEL_EXPORTER is console, which is the default when no endpoint is set, so spans are printed to the gateway's output instead. | Set OTEL_ENDPOINT or OTEL_EXPORTER_OTLP_ENDPOINT, or set OTEL_EXPORTER=otlp_http. |
| Collector answers 401 or 403 | Collector | The headers in OTEL_HEADERS are missing or wrong. | Set OTEL_HEADERS="authorization=Bearer <token>" in the form the collector expects. |
Mock LLM request made - check otel. but nothing arrives | GET /health/services?service=otel | The gateway sent a test span, so the callback runs, but the collector did not record it. | Check OTEL_ENDPOINT, OTEL_HEADERS and the collector's own log. |
403 naming scoped_integrations | Team or key destination | The license does not include team and key logging. | Use the gateway wide callback, or add the feature to the license. |
| Team destination refused for New Relic | Team callback | New Relic as a team destination needs the version 2 logger. | Set SPANWAY_OTEL_V2=true. |
Logs
| Level | Line | Meaning |
|---|---|---|
| WARNING | OpenTelemetry: unknown span exporter '<value>', spans are printed to the console instead of being sent. ... | OTEL_EXPORTER has a value the logger does not know, such as a typo, so nothing reaches the collector. Fix the value. |
| WARNING | OpenTelemetry: Unknown log exporter '<value>', defaulting to console. Supported: console, otlp_http, otlp_grpc | Only with SPANWAY_OTEL_INTEGRATION_ENABLE_EVENTS on: the log events exporter has an unknown value, so events go to the console. Spans are not affected. Fix OTEL_LOGS_EXPORTER or OTEL_EXPORTER. |
| WARNING | OpenTelemetry: Unknown metric exporter '<value>', defaulting to console. ... | The same for the metrics exporter when SPANWAY_OTEL_INTEGRATION_ENABLE_METRICS is on. |
| WARNING | Proxy Server is not installed. Skipping OpenTelemetry initialization. | The older logger could not attach itself to the gateway, so the Received Proxy Server Request span and the cache and database spans are missing. Model call spans still work. It only appears when the logger runs outside the gateway, for example in a script. |
| ERROR | OpenTelemetry: Error setting tools attributes: ... | A tool definition could not be recorded on the span. The span is still sent. |
Related
- Logging for the other destinations and what each log entry carries.
- Team logging for a per team collector.
- Levo integration and New Relic integration for two presets built on this callback.
- What Spanway stores for the content rules every destination follows.
- Error reference and log reference.