Quick start
Export is off by default. SetOTEL_ENABLED=true and point the exporter at
your collector:
opentelemetry enabled at startup together with the selected
exporters. Exporters are asynchronous: the gateway starts even when the
collector is down and retries in the background.
Without
OTEL_EXPORTER_OTLP_ENDPOINT the SDK sends to
http://localhost:4318 (http/protobuf) or localhost:4317 (grpc).Configuration
OTEL_ENABLED is the only GoModel-specific switch. Everything else follows
the standard
OpenTelemetry environment variables,
so GoModel is configured exactly like any other instrumented service. The
common settings are also available under opentelemetry: in
config.yaml; a variable set in the environment
wins over the YAML value.
A typical production setup:
What is exported
HTTP server spans and metrics
Every request gets aSERVER span named after its route (for example
POST /v1/chat/completions) with the standard http.request.method,
http.route, http.response.status_code, and url.scheme attributes, plus the
http.server.request.duration histogram and the request and response body size
histograms. Requests with a user path also carry gomodel.user_path (see
Per-team traces).
Trace context from the caller is honored, so a gateway span appears as a child
of the calling application’s span whenever the client sends traceparent (or
whichever headers OTEL_PROPAGATORS selects).
/health, /health/ready, the Prometheus endpoint (METRICS_ENDPOINT), and
/debug/pprof are excluded: they are polled, and would otherwise dominate the
trace volume.
Provider call spans and GenAI metrics
Each logical call to a model provider is instrumented with:
The response and usage attributes are set on chat completions, Responses API,
and translated
/v1/messages calls, buffered or streamed, once the gateway has
decoded the response. Usage is present when the provider reported it. For
streams, GoModel asks the provider for a usage chunk only while usage tracking
is on (the default). /v1/messages requests that GoModel forwards natively to an Anthropic
provider, and other passthrough calls, carry the request attributes only.
Metrics are histograms in seconds, carrying the same attributes except
gomodel.user_path:
gomodel.client.empty_responses counts buffered chat and Responses API calls
that returned 200 with no choices, no output, or no token usage. Those calls
look successful in the histograms above, so alert on this counter instead. Its error.type is
no_choices, no_output (a completed Responses API call with no output
items), or no_usage (content with zero token usage).
A buffered call produces a CLIENT span named <operation> <model>, such as
chat gpt-5, nested under the HTTP server span. Retries and failovers to
another provider are separate calls and therefore separate spans, so a request
that failed over shows exactly which provider failed and which one answered.
A streamed chat completion, Responses API, or translated /v1/messages call
gets a client span that ends when the stream ends, so its duration covers the
whole generation. Other streams, such as passthrough calls, get no client
span: the gateway sees only when they were established, and a span ending at
the headers would misreport latency. Their HTTP server span still covers the
full stream lifetime as seen by the client. A stream that fails to establish,
or ends before delivering its first chunk, always gets a failure span, so
errors are traced.
Token usage and cost metrics
GoModel exports the tokens and the cost of every request it records usage for. The metrics are updated when the response ends; a streaming request is exported once, when its stream closes.input covers every prompt token (uncached, cached, and cache-write) and
output every completion token (reasoning included), for every provider. The
details are parts of those totals, so they are a separate metric: no sum
over one metric counts a token or a dollar twice. The semantic conventions
define no metric for the details or for cost, hence the gomodel.* names.
All four carry gen_ai.provider.name, gomodel.provider.name,
gen_ai.request.model (when known), gen_ai.response.model, and
gen_ai.operation.name: the operation of the provider call, the same value
gen_ai.client.operation.duration reports (generate_content for native
Gemini calls, text_completion for /v1/completions). Endpoints without a
GenAI operation (audio, images, realtime) have none.
- Usage tracking must be on (
USAGE_ENABLED=true, the default). - Cost needs pricing. A model without a known price adds tokens but no
cost.
gomodel.client.billed_costis exported only when pricing markups are enabled. - Response-cache hits never reach a provider: they add no tokens and no provider cost, only their billed cost when cache hits are billed.
- MCP tool calls record neither tokens nor cost, so they add nothing.
OTEL_METRICS_EXPORTER=noneswitches these metrics off with the others.
Caller attributes
Three opt-in settings add who made the request to these four metrics. They are shared with the Prometheus metrics, which explains the cardinality limits in detail.
Each dimension keeps the first
METRICS_CALLER_MAX_VALUES (default 100)
distinct values; later values, values over 256 bytes, values that are not
valid UTF-8, and values spelled like {other} or {unknown} are exported as
{other} (/{other} for user paths). The duration histograms never carry
caller attributes. Spans carry the full user path but no other caller
attribute; see Per-team traces.
gen_ai.client.token.usage is not a call counter. A call records one point
per token type, a side without tokens records none, and a response-cache hit
records nothing, so the histogram’s count is about two per call. Counting
only the gen_ai.token.type=input points gives the calls that reported input
tokens; for request counts per caller, use
gomodel_caller_requests_total
in Prometheus.
Prompt and completion capture
Content capture is off by default. Turn it on to see the exchanged messages in your tracing backend, for example as generation input and output in Langfuse:/v1/messages calls then carry the
GenAI message attributes
as JSON:
Text parts longer than 64 KiB are truncated and marked, and each attribute
keeps at most 512 KiB of text: a long conversation loses its oldest messages’
text first. Images, audio, and files are reduced to their part type: binary
data and data URLs are never exported. Requests dropped by the trace sampler
are not captured at all.
The variable also accepts
SPAN_ONLY and SPAN_AND_EVENT, the mode names of
newer OpenTelemetry instrumentations.
Privacy
The exporter is designed so that telemetry can go to a third-party backend without leaking what flows through the gateway:- Prompts, completions, and tool calls are attached to spans only when content capture is on, and never to metrics. Request and response bodies are never exported as-is.
- Credentials and upstream error messages are never exported; failures carry only a status code or an error class.
client.address,network.peer.*,server.address,server.port, anduser_agent.originalare stripped from HTTP spans, and host-derived dimensions are excluded from HTTP metrics so a client cannot inflate metric cardinality through theHostheader.- Spans carry the caller’s user path as
gomodel.user_path, the same value usage and request log records carry, and never the API key name or request labels. - User paths, API key names, and request labels reach metrics only on the token and cost metrics, and only when you turn on the matching caller attribute. API key secrets and IDs are never exported.
Per-team traces
Every gateway span of a request carries the caller’s user path asgomodel.user_path. It is the effective
path that usage, budgets, and request logs use: the API key’s bound path when it
has one, otherwise the X-GoModel-User-Path header.
It is always the full path, independent of the
caller attributes setting, which cuts the metric value
to METRICS_CALLER_USER_PATH_DEPTH segments.
Filter on it in your backend, for example
{ span.gomodel.user_path =~ "/team/alpha(/.*)?" } in Tempo’s TraceQL to
match /team/alpha and its subtree.
To give each team its own tracing backend, point GoModel at an
OpenTelemetry Collector and route
on the attribute with the
routing connector.
The pattern ^/team/alpha(/|$) matches the whole /team/alpha subtree;
traces with no matching route go to the default pipeline.
otel-collector.yaml
Content capture is global. When it is on,
every team’s prompts reach the Collector, which forwards each trace only to
its team’s backend.
Reloading
gomodel --reload (SIGHUP) rebuilds the OpenTelemetry pipeline with the
current environment, so exporter, sampling, and propagation changes apply
without a restart. The previous pipeline is flushed before it is discarded.
Local collector example
A minimaldocker-compose.yml that shows traces in Jaeger:
http://localhost:16686.