Fix parent-child span linking in async LangChain#123
Open
NikitaVoitov wants to merge 3 commits intosignalfx:mainfrom
Open
Fix parent-child span linking in async LangChain#123NikitaVoitov wants to merge 3 commits intosignalfx:mainfrom
NikitaVoitov wants to merge 3 commits intosignalfx:mainfrom
Conversation
- Add parent_span field to GenAI base class in types.py - Add _resolve_parent_span() method to callback_handler.py - Set parent_span on all entity types for proper span linking - Add tests for parent-child span linking
Trace from async healthcare agent on Snowflake SPCS showing: - All spans have same parentId (HTTP root) instead of proper nesting - Demonstrates Issue signalfx#1 (flat hierarchy) and Issue signalfx#2 (unknown_model)
Raw trace export from Splunk O11y showing flat hierarchy bug
|
All contributors have signed the CLA ✍️ ✅ |
Author
|
I have read the CLA Document and I hereby sign the CLA |
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.
Summary
Fixes parent-child span linking in LangChain instrumentation for async applications by resolving
parent_run_idto actual OpenTelemetrySpanobjects.Problem: In async LangChain/LangGraph applications using
await graph.ainvoke(), all spans appear as siblings (flat hierarchy) instead of proper parent-child nesting. Spans share the same Trace ID but all point to the root span as parent.Solution: Explicitly resolve
parent_run_idUUID to the actual parentSpanobject and pass it to the span emitter for proper context propagation.Fixes #122
The Bug (Before)
The Fix (After)
Changes
types.py
Added
parent_span: Span | None = Nonefield toGenAIbase class:callback_handler.py
Added
_resolve_parent_span(run_id)method to lookup parent span from tracked entities:Updated all entity creation methods to set
parent_span:_start_agent_invocation()- AgentInvocationon_chain_start()- ToolCall, Stepon_chat_model_start()- LLMInvocationon_tool_start()- ToolCallThe span emitter already has logic to use
parent_spanif provided:Previously,
parent_spanwas alwaysNone, soparent_ctxwasNone, and spans inherited from the current active context. In sync code this works (context propagates automatically), but in async code the context is lost betweenawaitcalls.By explicitly setting
parent_span, we ensure correct parent-child relationships regardless of sync/async execution.Testing
test_parent_span_linking- verifies parent_span is correctly resolved from parent_run_idtest_nested_span_hierarchy_three_levels- verifies 3-level hierarchy (agent → step → llm)Files Changed
util/opentelemetry-util-genai/src/opentelemetry/util/genai/types.pyparent_spanfieldinstrumentation-genai/opentelemetry-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/callback_handler.py_resolve_parent_span()method, setparent_spanon entitiesinstrumentation-genai/opentelemetry-instrumentation-langchain/tests/test_callback_handler_agent.pyget_span_by_run_id()to stub