fix(google): normalize token usage extraction across ADK and GenAI instrumentation - #1465
Open
himanshu-xyz-1 wants to merge 1 commit into
Open
himanshu-xyz-1 wants to merge 1 commit into
himanshu-xyz-1 wants to merge 1 commit into
Conversation
…strumentation Google ADK and Gemini API telemetry emit token counts using varied attribute naming (e.g., input_tokens/output_tokens or prompt_token_count/candidates_token_count). When ADK emits input_tokens/output_tokens, spans missed token usage attributes, causing 0 token count / cost aggregation in trace metrics. Updated _extract_llm_attributes in google_adk and _set_response_attributes in google_genai to normalize input/output token counts and compute total_tokens when absent. Added unit tests verifying token usage normalization.
This branch has not been deployed
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
Closes #1418.
Google ADK and Gemini API telemetry payloads emit token counts using varying attribute keys (
input_tokens/output_tokens,input_token_count/output_token_count, orprompt_token_count/candidates_token_count).When Google ADK emits
gen_ai.usage.input_tokensandgen_ai.usage.output_tokens, AgentOps failed to extract the usage counts intoSpanAttributes.LLM_USAGE_PROMPT_TOKENSandSpanAttributes.LLM_USAGE_COMPLETION_TOKENS. As a result, ADK Gemini spans recorded 0 tokens and $0.00 cost in trace metrics.Solution
_extract_llm_attributesinagentops/instrumentation/agentic/google_adk/patch.pyto normalize prompt/input and completion/output token keys and calculatetotal_token_countif absent._set_response_attributesinagentops/instrumentation/providers/google_genai/attributes/model.pyto handle dictionary and object usage metadata variants consistently.tests/unit/instrumentation/test_google_token_usage.pycovering token normalization across both ADK and GenAI instrumentation.Testing
uv run pytest tests/unit/instrumentation/test_google_token_usage.py(4/4 passed).uv run pytest tests/unit/instrumentation/(142/142 passed).