[google-genai] Provide a way to attach extra attributes to the operation-details event but not to the span#24
Conversation
|
…emetry#24 Assisted-by: opencode agent
There was a problem hiding this comment.
Pull request overview
Adds an additional Context API channel for caller-supplied attributes that should be emitted only on the gen_ai.client.inference.operation.details event (LogRecord) and never on the generate_content {model} span, addressing #22 for PII/sampling-sensitive attributes.
Changes:
- Introduce
GENERATE_CONTENT_EVENT_ONLY_EXTRA_ATTRIBUTES_CONTEXT_KEYand thread it through all 4generate_contentwrappers (sync/async × streaming/non-streaming). - Merge event-only attributes into the operation-details event payload while keeping span attributes unchanged.
- Add unit tests validating “event-only not on span” and collision precedence behavior.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| instrumentation/opentelemetry-instrumentation-google-genai/src/opentelemetry/instrumentation/google_genai/generate_content.py | Adds new context key + retrieval helper; threads event-only attributes into operation-details event creation. |
| instrumentation/opentelemetry-instrumentation-google-genai/src/opentelemetry/instrumentation/google_genai/init.py | Re-exports the new context key from the package API. |
| instrumentation/opentelemetry-instrumentation-google-genai/tests/generate_content/nonstreaming_base.py | Adds tests ensuring event-only extras never appear on spans and collision behavior is correct. |
| instrumentation/opentelemetry-instrumentation-google-genai/tests/generate_content/streaming_base.py | Adds the same event-only attribute tests for streaming paths. |
| instrumentation/opentelemetry-instrumentation-google-genai/CHANGELOG.md | Documents the new context key (currently with a placeholder PR link). |
|
FYI if your PR needs a changelog entry, please update it to use towncrier #16 |
a10d6de to
5e6b28b
Compare
lmolkova
left a comment
There was a problem hiding this comment.
A more generic solution to this problem is coming via open-telemetry/opentelemetry-specification#4931
In the current scope this problem can be solved with custom context key and custom log record processor which would be a recommended approach until context-scoped attributes land in OTel spec and python implementation.
If there is a strong use-case to stamp information on events only and avoid spans, please consider sharing this feedback on open-telemetry/opentelemetry-specification#4931.
…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
5e6b28b to
aab3512
Compare
|
@Achuth17 please sign the CLA - we can't accept any contribution without it. |
|
This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment. |
Fixes #22
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: Gemini