feat(adk): expose gen_ai token usage Prometheus metric - #2673
Open
MartinForReal wants to merge 1 commit into
Open
feat(adk): expose gen_ai token usage Prometheus metric#2673MartinForReal wants to merge 1 commit into
MartinForReal wants to merge 1 commit into
Conversation
Record the OpenTelemetry GenAI-semconv gen_ai.client.token.usage metric from the Go ADK runtime using the native Prometheus client, served at /metrics. Labels follow the upstream Google ADK Python runtime so one dashboard works across both runtimes (gen_ai.token.type, gen_ai.request.model, gen_ai.response.model, gen_ai.provider.name, gen_ai.agent.name, gen_ai.operation.name, error.type). Recording and the scrape endpoint are gated default-OFF behind OTEL_METRICS_ENABLED, matching the existing OTEL_TRACING_ENABLED / OTEL_LOGGING_ENABLED gates. Signed-off-by: MartinForReal <fanshangxiang@gmail.com>
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.
What
Implements the GenAI token-usage metrics observability gap from #2148 for the Go ADK runtime. The Go ADK (
google.golang.org/adk/v2) ships spans only, no metric instruments, so kagent supplies the stdlib Prometheus instrument itself, in the shape of the open/dirty #2149 but rebuilt from scratch against today'sgo/adksource and aligned with the Python runtime's attribute set per #2458.How
pkg/telemetry/metrics.go— agen_ai_client_token_usagePrometheus histogram (semconvgen_ai.client.token.usage, semconv-recommended buckets) on the default registry.gen_ai_token_type(input/output),gen_ai_request_model,gen_ai_response_model(falls back to request model),gen_ai_provider_name(semconv well-known value viaSemconvProviderName),gen_ai_agent_name,gen_ai_operation_name(chat),error_type./metricsendpoint are default-OFF behindOTEL_METRICS_ENABLED=true, matching the existingOTEL_TRACING_ENABLED/OTEL_LOGGING_ENABLEDgates.pkg/a2a/server/server.go— serves/metricson the A2A mux when the gate is on, excluded from request tracing/span flushing.pkg/a2a/executor.go— records token usage from the ADKAfterEventCallback: skips partial (streaming) events so one observation is recorded per LLM call;input = PromptTokenCount,output = CandidatesTokenCount + ThoughtsTokenCountfromUsageMetadata.cmd/main.go/pkg/config— model + provider labels resolved at startup; agent name fromAppName.docs/genai-token-metrics.md— documents the metric, labels, gating, and scraping.Testing
go build ./adk/...,go vet, and golangci-lint (repo config) are clean.go test -racepasses for the changed packages (telemetry, a2a, a2a/server, config).metrics_test.go(record / skip-zero / disable-gate / response-model fallback / labels / provider-name mapping / /metrics handler) andexecutor_metrics_test.go(partial events not double-counted, per-LLM-call aggregation).Notes / decisions
go/core/internal/controller/translator/agent/manifest_builder.go, which no longer exists onmainafter the controller/runtime rework. That path was dropped rather than reinvented; operators can addprometheus.io/*annotations and this is documented. Flag if you want the annotation automation wired against the current runtime-provisioning location.make -C go lintcould not be run here:makeand the kube-api-linter plugin are unavailable in this environment. Lint was run with the repo's.golangci.yamlminus the plugin (0 issues); kube-apilinter only lintsapi/v1alphaCRD paths, untouched here.Refs: #2148