Skip to main content

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​

  1. Add the callback to the config file:

    spanway_settings:
    callbacks: ["otel"]
  2. 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"
  3. 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"}]}'
  4. Open the collector's backend. Expected result: a span whose service name is spanway (or your OTEL_SERVICE_NAME) carries the model, provider, token counts and cost as attributes, and gen_ai.input.messages is 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.

VariableMeaningDefault
OTEL_EXPORTER or OTEL_EXPORTER_OTLP_PROTOCOLconsole, 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 setconsole, or otlp_http once an endpoint is set
OTEL_ENDPOINT or OTEL_EXPORTER_OTLP_ENDPOINTCollector base URLunset
OTEL_TRACES_ENDPOINT or OTEL_EXPORTER_OTLP_TRACES_ENDPOINTFull traces URL, used as given, version 2 only. The older logger ignores it and always sends to the endpoint plus /v1/tracesunset
OTEL_HEADERS or OTEL_EXPORTER_OTLP_HEADERSHeaders sent to the collector, key=value separated by commas. The second wins when both are setunset
OTEL_SERVICE_NAMEservice.name of every spanspanway
OTEL_ENVIRONMENT_NAMEdeployment.environmentproduction with the default logger, unset with SPANWAY_OTEL_V2=true
SPANWAY_OTEL_INTEGRATION_ENABLE_METRICSAlso record the gen_ai.client and gen_ai.server histograms. Both loggers read itfalse
SPANWAY_OTEL_INTEGRATION_ENABLE_EVENTSAlso emit OpenTelemetry log events, such as one for each failed call. Both loggers read itfalse
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENTWhether prompts and responses are sent, see belowno_content

More settings​

VariableMeaningDefault
OTEL_MODEL_IDThe model id resource attribute of every spanthe service name
OTEL_TRACER_NAME, SPANWAY_METER_NAME, SPANWAY_LOGGER_NAMEThe instrumentation scope names of spans, metrics and log eventsspanway
OTEL_IGNORE_CONTEXT_PROPAGATIONtrue ignores incoming traceparent headers, so every request starts its own trace. Use it when callers send trace ids you do not want to joinfalse
OTEL_SEMCONV_STABILITY_OPT_INOpt in to newer OpenTelemetry semantic convention names, for example gen_ai_latest_experimentalunset
SPANWAY_OTEL_BAGGAGE_TEAM_METADATA_KEYSTeam metadata keys copied into the trace baggage, so downstream services see them. Only list keys that may leave the gatewayunset
SPANWAY_OTEL_BAGGAGE_METADATA_KEYSRequest metadata keys copied into the trace baggage, with the same cautionunset
USE_OTEL_SPANWAY_REQUEST_SPANtrue always adds a spanway_request span, even under a caller's spanfalse
OTEL_LOGS_EXPORTERThe exporter for log events when events are on: console, otlp_http or otlp_grpcthe span exporter
OTEL_EXPORTER_OTLP_CERTIFICATE, OTEL_EXPORTER_OTLP_TRACES_CERTIFICATEA CA bundle for a collector with its own certificateunset
DEBUG_OTELtrue logs every export at DEBUG level, for one investigation onlyfalse
SPANWAY_OTEL_LEGACY_COMPAT, SPANWAY_OTEL_LANGFUSE_SPAN_SCOPEVersion 2 only. Keep the older attribute names next to the new ones, and llm_only sends only model call spans to Langfusetrue, 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.

SpanWhen
Received Proxy Server RequestThe gateway's handling of the HTTP request.
spanway_requestOne 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_requestThe provider request as sent. Only with content capture on, because it carries the prompt.
A guardrail span per guardrailEach guardrail check with its result and timing.
redis and postgres service spansCache 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.

CallbackReads
arize, arize_phoenixArize keys, or PHOENIX_PROJECT_NAME
langfuse_otelLangfuse keys and host
newrelicNEW_RELIC_LICENSE_KEY and NEW_RELIC_REGION (us or eu)
signozSIGNOZ_INGESTION_ENDPOINT and SIGNOZ_INGESTION_KEY
agentops, langtrace, levo, weave_otelTheir 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​

StatusWhereMeaningFix
No spans arrive, no errorCollectorOTEL_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 403CollectorThe 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 arrivesGET /health/services?service=otelThe 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_integrationsTeam or key destinationThe 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 RelicTeam callbackNew Relic as a team destination needs the version 2 logger.Set SPANWAY_OTEL_V2=true.

Logs​

LevelLineMeaning
WARNINGOpenTelemetry: 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.
WARNINGOpenTelemetry: Unknown log exporter '<value>', defaulting to console. Supported: console, otlp_http, otlp_grpcOnly 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.
WARNINGOpenTelemetry: Unknown metric exporter '<value>', defaulting to console. ...The same for the metrics exporter when SPANWAY_OTEL_INTEGRATION_ENABLE_METRICS is on.
WARNINGProxy 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.
ERROROpenTelemetry: Error setting tools attributes: ...A tool definition could not be recorded on the span. The span is still sent.