Skip to content

POC: Add context scoped attributes with fallback on baggage(DO NOT MERGE)#270

Open
wrisa wants to merge 5 commits intomainfrom
replace-baggage-with-context-scoped-attributes
Open

POC: Add context scoped attributes with fallback on baggage(DO NOT MERGE)#270
wrisa wants to merge 5 commits intomainfrom
replace-baggage-with-context-scoped-attributes

Conversation

@wrisa
Copy link
Copy Markdown
Contributor

@wrisa wrisa commented Apr 14, 2026

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)

  • For Workflow: sets baggage and calls _ctx_workflow_name.set(), storing the token on invocation._ctx_tokens.
  • For AgentInvocation: sets baggage and calls .set() for both _ctx_agent_name / _ctx_agent_id, storing both tokens in invocation._ctx_tokens.

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.

@wrisa wrisa requested review from a team as code owners April 14, 2026 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant