feat: emit gen_ai.client.token.usage as an OTel Histogram (GenAI semconv)#102
Open
syrac88 wants to merge 1 commit into
Open
feat: emit gen_ai.client.token.usage as an OTel Histogram (GenAI semconv)#102syrac88 wants to merge 1 commit into
syrac88 wants to merge 1 commit into
Conversation
…emconv Adds a Histogram instrument alongside the existing opencode.token.usage Counter so GenAI-semconv-compliant backends (e.g. Splunk Observability Cloud AI Agent Monitoring) can populate their token tiles, which require histogram-typed metrics and silently ignore counters regardless of name. The metric name is intentionally unprefixed (fixed by the spec), uses the semconv-recommended bucket boundaries, and skips zero-valued token types so the distribution is not distorted. Fully additive: the existing opencode.token.usage Counter is unchanged. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
|
The overall direction looks good: the unprefixed Histogram name, I think two semantic-convention correctness issues should be addressed before merging:
input = tokens.input + tokens.cache.read + tokens.cache.write
output = tokens.output + tokens.reasoningThe detailed subtype measurements can remain available through
Two smaller points:
With the canonical input/output aggregation and provider normalization resolved, this looks like a useful compatibility feature. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The plugin exports token usage exclusively via
opencode.token.usage, a Counterinstrument. There is no
gen_ai.client.token.usagemetric, and the OTel GenAISemantic Conventions
define that metric as a Histogram. Some downstream platforms (e.g. Splunk
Observability Cloud's AI Agent Monitoring pages) require the histogram shape to
populate their token tiles — a Counter under any name is silently ignored.
Same root cause reported against Claude Code's own telemetry:
anthropics/claude-code#77562
Change
Adds a
gen_ai.client.token.usageHistogram instrument, recorded alongside(not replacing) the existing
opencode.token.usageCounter:backends only recognise it verbatim
gen_ai.token.type,gen_ai.request.model,gen_ai.operation.name,gen_ai.provider.name(token types mirror theCounter's
typevalues:input,output,reasoning,cacheRead,cacheCreation)advice(unlike a Counter, a Histogram counts every
record())OPENCODE_DISABLE_METRICS=gen_ai.client.token.usageFully additive — existing dashboards built on
opencode.token.usageare unaffected.Testing
bun run typecheck/bun run lint/bun run buildpassbun test: 3 new unit tests (attributes per token type, zero-value skipping,disable flag); no regressions in the existing suite
createInstruments()against anInMemoryMetricExporter: themetric exports with
dataPointType: HISTOGRAM, unit{token}, and thesemconv bucket boundaries
linked Claude Code issue
Happy to adjust naming, gate this behind an opt-in/opt-out flag, or rework based
on maintainer preference.
🤖 Generated with Claude Code