Prototype setting / getting inference span and event on / from the context - #479
Closed
DylanRussell wants to merge 8 commits into
Closed
DylanRussell wants to merge 8 commits into
DylanRussell wants to merge 8 commits into
Conversation
Contributor
There was a problem hiding this comment.
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.contexthelpers to set/get an inference span in OpenTelemetry context. - Updates
InferenceInvocationto 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.
Pull request dashboard statusClosed · refreshed 2026-09-18 14:32 UTC Status above doesn't look right?
|
DylanRussell
marked this pull request as draft
August 25, 2026 19:29
DylanRussell
force-pushed
the
suppress_instrumentation
branch
from
September 4, 2026 14:20
06358fb to
b98f73a
Compare
opentelemetry-util-genai] Add inference span to the context to avoid duplication of span# Conflicts: # util/opentelemetry-util-genai/src/opentelemetry/util/genai/_invocation.py
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.
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:
GenAIInvocationbase class so it can be extended to any invocation type.server.addressandserver.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.invocation.already_started: ifTrue, they can skip request/response parsing and only set missing or override attributes.google-genaias the reference implementation andscripts/prototype_inference_deduplication.pyto demo the behavior.Type of change
Please delete options that are not relevant.
How has this been tested?
Unit tests
Checklist