Skip to content

[google-genai] Provide a way to attach extra attributes to the operation-details event but not to the span#24

Open
Achuth17 wants to merge 1 commit into
open-telemetry:mainfrom
Achuth17:feature/log-only-attributes
Open

[google-genai] Provide a way to attach extra attributes to the operation-details event but not to the span#24
Achuth17 wants to merge 1 commit into
open-telemetry:mainfrom
Achuth17:feature/log-only-attributes

Conversation

@Achuth17
Copy link
Copy Markdown

@Achuth17 Achuth17 commented May 15, 2026

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

@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented May 15, 2026

CLA Not Signed

@Achuth17 Achuth17 marked this pull request as ready for review May 15, 2026 22:17
@Achuth17 Achuth17 requested a review from a team as a code owner May 15, 2026 22:17
Copilot AI review requested due to automatic review settings May 15, 2026 22:17
Achuth17 added a commit to Achuth17/opentelemetry-python-genai that referenced this pull request May 15, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_KEY and thread it through all 4 generate_content wrappers (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).

Comment thread instrumentation/opentelemetry-instrumentation-google-genai/CHANGELOG.md Outdated
@aabmass
Copy link
Copy Markdown
Member

aabmass commented May 18, 2026

FYI if your PR needs a changelog entry, please update it to use towncrier #16

@Achuth17 Achuth17 force-pushed the feature/log-only-attributes branch 2 times, most recently from a10d6de to 5e6b28b Compare May 18, 2026 17:57
Copy link
Copy Markdown
Member

@lmolkova lmolkova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@Achuth17 Achuth17 force-pushed the feature/log-only-attributes branch from 5e6b28b to aab3512 Compare May 18, 2026 23:04
@MikeGoldsmith
Copy link
Copy Markdown
Member

@Achuth17 please sign the CLA - we can't accept any contribution without it.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 3, 2026

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.
If you're still working on this, please add a comment or push new commits.

@github-actions github-actions Bot added the Stale Issue or PR has been inactive label Jun 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Stale Issue or PR has been inactive

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[google-genai] Provide a way to attach extra attributes to the operation-details event but not to the span

5 participants