POC: Add context scoped attributes with fallback on baggage(DO NOT MERGE)#270
Open
POC: Add context scoped attributes with fallback on baggage(DO NOT MERGE)#270
Conversation
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.
See PR, only baggage support
Changes Made
File: util/opentelemetry-util-genai/src/opentelemetry/util/genai/handler.py
Step 1 — Three new ContextVars + helper (lines 167–186)
Added _ctx_workflow_name, _ctx_agent_name, _ctx_agent_id below _current_genai_span, plus _get_ctx_attr() which reads from the ContextVar first and falls back to baggage.
Step 2 — Dual-write in _push_current_span (lines 652–664)
Step 3 — Token reset in _pop_current_span (lines 684–688)
After detaching the OTel context token, iterates invocation._ctx_tokens and calls ctx_var.reset(tok) for each, restoring the previous ContextVar state.
Step 4 — 6 read sites replaced
All baggage.get_baggage("workflow.name/agent.name/agent.id") calls in start_llm, start_embedding, start_retrieval, start_tool_call, start_agent, and start_step are now _get_ctx_attr(...) calls.
Baggage writes are kept (dual-write), so cross-boundary propagation via FastMCP/MCP is preserved.