[google-genai] Provide a way to attach extra attributes to the operation-details event but not to the span#4581
Closed
Achuth17 wants to merge 1 commit into
Closed
Conversation
…ion-details event but not to the span
|
Contributor
|
This looks good but should be opened in https://github.com/open-telemetry/opentelemetry-python-genai where we recently moved the instrumentations.. Also open-telemetry/opentelemetry-python-genai#10 -- you might wait until this is merged but I can also merge my PR on top of yours so that doesn't matter too much |
Author
|
Moved the PR to open-telemetry/opentelemetry-python-genai#24 |
Achuth17
added a commit
to Achuth17/opentelemetry-python-genai
that referenced
this pull request
May 18, 2026
…ion-details event but not to the span
Add GENERATE_CONTENT_EVENT_ONLY_EXTRA_ATTRIBUTES_CONTEXT_KEY for caller-supplied
attributes that should be emitted only on the
gen_ai.client.inference.operation.details log event and never on the
generate_content {model} span. Threaded through all four wrappers
(sync/async x streaming/non-streaming). On key collisions with
GENERATE_CONTENT_EXTRA_ATTRIBUTES_CONTEXT_KEY, the event-only value wins on
the event; the span carries the existing extra-attributes value.
Originally proposed at open-telemetry/opentelemetry-python-contrib#4581 before
the package moved to this repo.
Assisted-by: opencode agent
Achuth17
added a commit
to Achuth17/opentelemetry-python-genai
that referenced
this pull request
May 18, 2026
…ion-details event but not to the span
Add GENERATE_CONTENT_EVENT_ONLY_EXTRA_ATTRIBUTES_CONTEXT_KEY for caller-supplied
attributes that should be emitted only on the
gen_ai.client.inference.operation.details log event and never on the
generate_content {model} span. Threaded through all four wrappers
(sync/async x streaming/non-streaming).
Precedence on the event: caller-supplied extra_attributes, then
event-only extra_attributes (so they win over extra_attributes on
collision), then request_attributes and final_attributes. Putting the
instrumentation-owned semconv fields last ensures callers cannot
accidentally clobber them (e.g. gen_ai.usage.input_tokens) via the
event-only context value. The span continues to carry only
extra_attributes for any collisions; event-only attributes are never set
on the span.
Tests cover sync/async x streaming/non-streaming for: event-only
attributes not appearing on the span, the event-only-vs-extra-attributes
collision on the event, and event-only not overriding semconv fields.
Originally proposed at open-telemetry/opentelemetry-python-contrib#4581 before
the package moved to this repo.
Assisted-by: opencode agent
Achuth17
added a commit
to Achuth17/opentelemetry-python-genai
that referenced
this pull request
May 18, 2026
…ion-details event but not to the span
Add GENERATE_CONTENT_EVENT_ONLY_EXTRA_ATTRIBUTES_CONTEXT_KEY for caller-supplied
attributes that should be emitted only on the
gen_ai.client.inference.operation.details log event and never on the
generate_content {model} span. Threaded through all four wrappers
(sync/async x streaming/non-streaming).
Precedence on the event: caller-supplied extra_attributes, then
event-only extra_attributes (so they win over extra_attributes on
collision), then request_attributes and final_attributes. Putting the
instrumentation-owned semconv fields last ensures callers cannot
accidentally clobber them (e.g. gen_ai.usage.input_tokens) via the
event-only context value. The span continues to carry only
extra_attributes for any collisions; event-only attributes are never set
on the span.
Tests cover sync/async x streaming/non-streaming for: event-only
attributes not appearing on the span, the event-only-vs-extra-attributes
collision on the event, and event-only not overriding semconv fields.
Originally proposed at open-telemetry/opentelemetry-python-contrib#4581 before
the package moved to this repo.
Assisted-by: opencode agent
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.
Fixes #4580
Description
Adds a sibling context key,
GENERATE_CONTENT_EVENT_ONLY_EXTRA_ATTRIBUTES_CONTEXT_KEY, toopentelemetry-instrumentation-google-genaifor attaching caller-suppliedattributes that should be emitted only on the
gen_ai.client.inference.operation.detailslog event and never on thegenerate_content {model}span.The existing
GENERATE_CONTENT_EXTRA_ATTRIBUTES_CONTEXT_KEY(added in #3961for spans, extended in #4103 for events) writes attributes to both the
span and the event with no opt-out. That makes it unsafe for caller-supplied
attributes that are acceptable on event log records but must not land on
broadly-sampled spans, for example, an end-user identifier (
user.id).Concrete motivating case: the [Google ADK][adk] passes a
common_attributesdict containing theUSER_IDsemconv constant(string value
"user.id", fromopentelemetry.semconv._incubating.attributes.user_attributes) through thiscontext key, which causes
user.idto land on everygenerate_contentspanas soon as
opentelemetry-instrumentation-google-genaiis installed. ADKhas no way today to keep that value off the span without also losing it from
the operation-details event.
The new key follows the exact same naming and access pattern as the
existing
GENERATE_CONTENT_EXTRA_ATTRIBUTES_CONTEXT_KEY, keeping thepublic API surface uniform. It is read in all four
generate_contentwrappers (sync/async × streaming/non-streaming) and merged into the
LogRecordonly — never intospan.set_attributes(...). On key collisionswith the existing extras channel, the event-only value wins on the event;
the span carries the existing extra-attributes value.
No new semconv keys are introduced, keys remain caller-defined, like the
existing extras channel. No external dependencies added or changed.
Fixes # (issue)
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Targeted unit tests (8 new test cases — 2 per wrapper, run for sync +
async × streaming + non-streaming via the existing shared base classes
in
nonstreaming_base.pyandstreaming_base.py):test_event_only_extra_attributes_not_set_on_span— verifies anattribute attached only via
GENERATE_CONTENT_EVENT_ONLY_EXTRA_ATTRIBUTES_CONTEXT_KEYis absent from
generate_content {model}span attributes.test_event_only_extra_attributes_set_on_event_only— verifies thesame attribute appears on the
gen_ai.client.inference.operation.detailsevent LogRecord, plus thecollision-precedence rule (event-only value wins on the event; the
existing-extras value is what reaches the span).
Test commands run:
Does This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.