Skip to content

feat(telemetry): record client inference metrics - #2314

Open
argaj wants to merge 13 commits into
google:mainfrom
argaj:feat/telemetry-client-metrics
Open

argaj wants to merge 13 commits into
google:mainfrom
argaj:feat/telemetry-client-metrics

Conversation

@argaj

@argaj argaj commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Every model call through InstrumentGenerateContent records the OpenTelemetry GenAI client inference metrics: duration, per-call input and output token histograms, usage counters by modality, and the two chunk timings for streamed calls. The duration uses the span's own timestamps, and the token metrics use the same newest usage report the span reports.

The generate_content span changes with them: it always carries gen_ai.provider.name from the resolver the metrics use, a canceled call is no longer an error, a panic while the span is open records error.type _OTHER, and cache writes the Anthropic model reports appear as gen_ai.usage.cache_write.input_tokens.

Fixes #2292

@argaj
argaj requested a review from a team as a code owner November 13, 2026 12:46
Every model call through InstrumentGenerateContent records the
OpenTelemetry GenAI client inference metrics: duration, per-call input
and output token histograms, usage counters by modality, and the two
chunk timings for streamed calls. The duration uses the span's own
timestamps, and the token metrics use the same newest usage report the
span reports.

The generate_content span changes with them: it always carries
gen_ai.provider.name from the resolver the metrics use, a canceled call
is no longer an error, a panic while the span is open records
error.type _OTHER, and cache writes the Anthropic model reports appear
as gen_ai.usage.cache_write.input_tokens.

Fixes google#2292
@argaj
argaj force-pushed the feat/telemetry-client-metrics branch from 4ea6b00 to 88de1b7 Compare November 13, 2026 13:01
@argaj
argaj marked this pull request as draft November 17, 2026 10:30
The generate_content span was started and ended with explicit
timestamps, which override the times a wrapping tracer provider
chooses. Start and end it without them. When the ended span exposes
StartTime and EndTime, the duration metric is the span's own.

Otherwise the duration is timed on platform.Now from the call's
context. Chunk timings stay on the wall clock, with the first chunk
timed from the span's start when it is readable.
@argaj
argaj force-pushed the feat/telemetry-client-metrics branch from b403bbd to f1ef126 Compare November 17, 2026 15:38
@argaj
argaj marked this pull request as ready for review November 17, 2026 15:42
Comment thread internal/telemetry/instrumentation.go Resolved
Comment thread internal/telemetry/instrumentation.go Resolved
Comment thread internal/telemetry/instrumentation.go Resolved
Comment thread internal/telemetry/telemetry.go Resolved
Comment thread internal/telemetry/token_usage.go Resolved
argaj added 11 commits November 23, 2026 15:08
gen_ai.provider.name is required on the inference metrics, but a backend
GenAISystemAttr does not know left it empty on the metrics, the span and
the inference details event. BackendEnterprise now reports
gcp.vertex_ai, as the genai SDK turns it into Vertex AI, and any other
unknown backend reports gcp.gen_ai.

An Apigee path's provider segment (vertex_ai, gemini, openai) now names
the provider. The Apigee model reports no backend, so these paths were
guessed from the environment and could name the wrong one. A model name
that names a provider still wins. An Apigee path without a provider
segment follows the Apigee model's own rule, GOOGLE_GENAI_USE_VERTEXAI
alone.

The environment rule for other models moves from telemetry to
googlellm.EnvironmentBackend.
The conventions require every gen_ai.client.inference.usage.*
measurement to be a known, strictly positive modality contribution.
A call with nothing to add to a counter added zero under the unknown
modality; it now adds nothing. The per-call token histograms still
record 0 for a call without usage.

A breakdown entry marked MODALITY_UNSPECIFIED counted as text and now
counts as unknown, as do modalities the genai SDK does not define. The
split is a struct with one field per modality instead of a map.
The delegation and dynamic workflow scenarios scripted no usage, so
their goldens showed zero tokens for calls that succeeded. Every
successful mock model call now reports usage, with a per-modality
breakdown marking its input, candidates and cache read counts as text,
so the goldens show text where a model breaks the count down. Failed
calls still report none.

The usage reports were named for turns, which was not true of the
streaming scenario, which repeats one report on both chunks of a single
call. They are now usageA, usageB and usageC.
…place

An error reached the span and its event through result.Error and the
metrics through observe, and the panic sentinel took a third path, so
the rule that a cancellation is not an error was applied in several
places. reportError now hands every error, the panic sentinel included,
to all three, and applies that rule once.

A panic was told apart by a flag set before every return. The loop now
runs in an inner function, and the flag is set once after it returns,
which a panic skips. inferenceError is renamed notCanceledError.
The telemetry package is large enough that what it keeps private is
hard to see. The accounting of what one model call spent moves to
internal/telemetry/tokenusage, which exports only what telemetry reads:
Usage, FromResponses and CacheWriteKey. No behavior changes.
The reviewer asked us not to guess the provider. A call whose model name
and backend name none, a backend GenAISystemAttr does not know, and an
Apigee path without a provider segment were guessed from
GOOGLE_GENAI_USE_ENTERPRISE and GOOGLE_GENAI_USE_VERTEXAI or reported as
gcp.gen_ai. gen_ai.provider.name is required and the conventions allow a
custom value, so these now report unknown on the span, the inference
details event and the metrics.

The provider still comes from the model name, then an Apigee provider
segment, then the call's backend. The environment rule is removed, with
googlellm.EnvironmentBackend.

The functional test mock model can now report a backend, and the
scenarios set the Gemini API, so the goldens keep gcp.gemini and also
show the gen_ai.system attribute the 1.36 log records carry.
The reviewer considered the special case a nice to have that belongs in
a separate change. A panic that unwinds a model call while its span is
open is still recorded as an error on the span and the metrics, but
errorType no longer maps it to _OTHER; it gets the type a plain error
gets, *errors.errorString.
Drops comments that restate the code or point at adk-python, and leaves
the gen_ai.system comments in logger.go and compaction.go as they are on
main.
A chunk is a provider event carrying new, non-empty output: text,
reasoning, audio or images, or a tool call's name or arguments. A
response carrying only usage, a finish reason or a thought signature is
not one, so it no longer records a chunk timing or resets the time per
output chunk.
A generic check of what spans and metrics share is planned, so this
narrower one goes.
…-metrics

# Conflicts:
#	internal/telemetry/functionaltest/testdata/delegation/event_only-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/delegation/no_content-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/delegation/span_and_event-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/delegation/span_only-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/dynamicworkflow/event_only-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/dynamicworkflow/first-agent-error-event_only-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/dynamicworkflow/first-agent-error-no_content-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/dynamicworkflow/first-agent-error-span_and_event-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/dynamicworkflow/first-agent-error-span_only-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/dynamicworkflow/no_content-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/dynamicworkflow/second-agent-error-event_only-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/dynamicworkflow/second-agent-error-no_content-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/dynamicworkflow/second-agent-error-span_and_event-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/dynamicworkflow/second-agent-error-span_only-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/dynamicworkflow/span_and_event-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/dynamicworkflow/span_only-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/streaming/event_only-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/streaming/no_content-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/streaming/span_and_event-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/streaming/span_only-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/toolcall/event_only-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/toolcall/inference-error-event_only-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/toolcall/inference-error-no_content-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/toolcall/inference-error-span_and_event-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/toolcall/inference-error-span_only-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/toolcall/no_content-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/toolcall/span_and_event-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/toolcall/span_only-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/toolcall/tool-error-event_only-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/toolcall/tool-error-no_content-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/toolcall/tool-error-span_and_event-otel_semconv_1_36.json
#	internal/telemetry/functionaltest/testdata/toolcall/tool-error-span_only-otel_semconv_1_36.json
#	internal/telemetry/instrumentation_test.go

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

telemetry: record the GenAI client inference metrics

2 participants