Prototype setting / getting inference span and event on / from the context - #479
Prototype setting / getting inference span and event on / from the context#479DylanRussell wants to merge 6 commits into
Conversation
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 statusWaiting on the author · refreshed 2026-09-04 14:22 UTC Move out of draft to request review. Status above doesn't look right?
|
06358fb to
b98f73a
Compare
opentelemetry-util-genai] Add inference span to the context to avoid duplication of span
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