Skip to content

[opentelemetry-util-genai] Add inference span to the context to avoid duplication of span - #479

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

[opentelemetry-util-genai] Add inference span to the context to avoid duplication of span#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 can happen at different levels in the call stack by different instrumentation libraries.

It makes sense to add it to the context under a constant key, where instrumentations can check if it exists before creating it.

Instrumentations may modify the span (if they have something useful to add) or suppress their instrumentation entirely if not.

Instrumentations both outside and inside this repo should do this. I also sent a PR to the seem conv repo: open-telemetry/semantic-conventions-genai#475

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

Checklista

  • 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-08-25 20:56 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.

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