-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat: Adds telemetry and fixes usage metadata for live mode #2325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
emilmilow
wants to merge
13
commits into
google:main
Choose a base branch
from
emilmilow:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+606
−13
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
195886d
feat: Adds telemetry and fixes usage metadata for live mode
1734afc
Merge branch 'main' into main
emilmilow 6df797e
Merge remote-tracking branch 'google/main'
db32e7e
Merge branch 'main' into main
emilmilow f38f0ac
Merge branch 'main' into main
emilmilow a640f2f
Merge remote-tracking branch 'google/main'
596c93f
Merge remote-tracking branch 'google/main'
5abf1f4
Merge remote-tracking branch 'google/main'
6eed324
Merge remote-tracking branch 'google/main'
702eecf
Merge branch 'main' into main
emilmilow 817c630
Merge branch 'main' into main
emilmilow 2052404
Merge branch 'main' into main
emilmilow bb6437e
Merge branch 'main' into main
emilmilow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -230,9 +230,31 @@ def trace_call_llm( | |
| llm_request: The LLM request object. | ||
| llm_response: The LLM response object. | ||
| """ | ||
| span = trace.get_current_span() | ||
| # Special standard Open Telemetry GenaI attributes that indicate | ||
| # that this is a span related to a Generative AI system. | ||
| # For live events with usage metadata, create a new span for each event | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hm what's the reasoning for creating a new span for every live event? just wondering if it would it cause too much overhead by generating too many spans |
||
| # For regular events or live events without usage data, use the current span | ||
| if ( | ||
| hasattr(invocation_context, 'live_request_queue') | ||
| and invocation_context.live_request_queue | ||
| and llm_response.usage_metadata is not None | ||
| ): | ||
| # Live mode with usage data: create new span for each event | ||
| span_name = f'llm_call_live_event [{event_id[:8]}]' | ||
| with tracer.start_as_current_span(span_name) as span: | ||
| _set_llm_span_attributes( | ||
| span, invocation_context, event_id, llm_request, llm_response | ||
| ) | ||
| else: | ||
| # Regular mode or live mode without usage data: use current span | ||
| span = trace.get_current_span() | ||
| _set_llm_span_attributes( | ||
| span, invocation_context, event_id, llm_request, llm_response | ||
| ) | ||
|
|
||
|
|
||
| def _set_llm_span_attributes( | ||
| span, invocation_context, event_id, llm_request, llm_response | ||
| ): | ||
| """Set LLM span attributes.""" | ||
| span.set_attribute('gen_ai.system', 'gcp.vertex.agent') | ||
| span.set_attribute('gen_ai.request.model', llm_request.model) | ||
| span.set_attribute( | ||
|
|
@@ -271,10 +293,11 @@ def trace_call_llm( | |
| ) | ||
|
|
||
| if llm_response.usage_metadata is not None: | ||
| span.set_attribute( | ||
| 'gen_ai.usage.input_tokens', | ||
| llm_response.usage_metadata.prompt_token_count, | ||
| ) | ||
| if llm_response.usage_metadata.prompt_token_count is not None: | ||
| span.set_attribute( | ||
| 'gen_ai.usage.input_tokens', | ||
| llm_response.usage_metadata.prompt_token_count, | ||
| ) | ||
| if llm_response.usage_metadata.candidates_token_count is not None: | ||
| span.set_attribute( | ||
| 'gen_ai.usage.output_tokens', | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will this cause duplicated usage metadata since we're adding it to all the LlmResponses? i.e. in the case that a message contains both
content.partsandmessage.server_content.input_transcription