Skip to content

Prototype setting / getting inference span and event on / from the context - #479

Draft
DylanRussell wants to merge 6 commits into
open-telemetry:mainfrom
DylanRussell:suppress_instrumentation
Draft

Prototype setting / getting inference span and event on / from the context#479
DylanRussell wants to merge 6 commits into
open-telemetry:mainfrom
DylanRussell:suppress_instrumentation

Conversation

@DylanRussell

@DylanRussell DylanRussell commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Description

The creation of the inference span/event can happen at different levels in the call stack by different instrumentation libraries; we use the context to avoid duplication.

Key points:

  • Most of the reuse logic is inside the GenAIInvocation base class so it can be extended to any invocation type.
  • Spans are write-only: Attributes cannot be read back from a span once written. The invocation subclass explicitly applies safe start attributes (server.address and server.port), and instrumentations can set attributes (invocation.attributes[...]) or invocation properties (e.g., invocation.top_p = ...) post-creation to override them on the span and event.
  • Instrumentations should check invocation.already_started: if True, they can skip request/response parsing and only set missing or override attributes.
  • While point-in-time events cannot be enriched retroactively, inference events are created on the request path and only emitted on the response path, making in-place enrichment possible.
  • Instrumentations both inside and outside this repo should adopt this pattern. Relevant semantic conventions PR: open-telemetry/semantic-conventions-genai#475.
  • Includes google-genai as the reference implementation and scripts/prototype_inference_deduplication.py to demo the behavior.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How has this been tested?

Unit tests

Checklist

  • Followed the style guidelines of this project
  • Changelog updated if the change requires an entry
  • Unit tests added
  • Documentation updated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Adds a well-known context entry for the active GenAI inference span so different instrumentations can detect/reuse it and avoid emitting duplicate inference spans.

Changes:

  • Introduces opentelemetry.util.genai.context helpers to set/get an inference span in OpenTelemetry context.
  • Updates InferenceInvocation to attach its span under the inference-span context key during invocation start.
  • Adds unit tests and documentation guidance for downstream instrumentations to check for an existing inference span.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
util/opentelemetry-util-genai/src/opentelemetry/util/genai/context.py Adds context helpers and establishes the inference-span context key.
util/opentelemetry-util-genai/src/opentelemetry/util/genai/_invocation.py Adds a context creation hook and uses it before attaching context.
util/opentelemetry-util-genai/src/opentelemetry/util/genai/_inference_invocation.py Ensures inference invocations publish their span into the inference-span context key.
util/opentelemetry-util-genai/src/opentelemetry/util/genai/init.py Exposes the new context helper API at the package top level.
util/opentelemetry-util-genai/tests/test_context.py Adds tests validating context behavior and interoperability.
AGENTS.md Documents how to avoid duplicate inference spans using get_current_inference_span().
.github/instructions/instrumentation.instructions.md Adds guidance to check get_current_inference_span() before creating a duplicate span.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread util/opentelemetry-util-genai/src/opentelemetry/util/genai/context.py Outdated
Comment thread util/opentelemetry-util-genai/tests/test_context.py Outdated
Comment thread AGENTS.md Outdated
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Aug 25, 2026

Copy link
Copy Markdown

Pull request dashboard status

Waiting on the author · refreshed 2026-09-04 14:22 UTC

Move out of draft to request review.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@DylanRussell
DylanRussell force-pushed the suppress_instrumentation branch from 06358fb to b98f73a Compare September 4, 2026 14:20
@DylanRussell DylanRussell changed the title [opentelemetry-util-genai] Add inference span to the context to avoid duplication of span Prototype setting / getting inference span and event on / from the context Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants