feat(langchain): set callback spans to active in OTEL context #1310
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.
Important
Enhances LangChain CallbackHandler with OTEL context management for better distributed tracing in Langfuse.
_attach_observation()and_detach_observation()methods inCallbackHandler.pyto manage OTEL context.self.runsassignments with span attachment/detachment in methods likeon_chain_start,on_tool_start,on_llm_start, etc.context_tokensdictionary to track OTEL context tokens alongsideruns.CallbackHandler.py.This description was created by
for a0ce171. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Summary
This PR enhances the LangChain CallbackHandler in Langfuse to properly integrate with OpenTelemetry (OTEL) context management. The changes implement systematic span attachment and detachment methods that ensure Langfuse spans become the "active span" in OTEL context, which is crucial for distributed tracing and frameworks like LangGraph that rely on OTEL context propagation.
The implementation adds two key methods:
_attach_observation(): Sets a Langfuse span as active in OTEL context usingtrace.set_span_in_context()andcontext.attach(), storing the context token for later cleanup_detach_observation(): Properly cleans up OTEL context by callingcontext.detach()with the stored token and removes the span from trackingThese methods replace direct assignment to
self.runsthroughout all callback lifecycle methods (on_chain_start,on_tool_start,on_llm_start, etc.). The change adds acontext_tokensdictionary to track OTEL context tokens alongside the existingrunsdictionary, ensuring proper pairing of attach/detach operations.This integration allows Langfuse spans to participate correctly in OTEL's context propagation mechanism, ensuring that child operations created by other OTEL-aware libraries are properly nested under Langfuse spans in the trace hierarchy. This is particularly important for complex workflows using frameworks like LangGraph where multiple tracing systems need to work together seamlessly.
Confidence score: 4/5
_attach_observation()and_detach_observation()methods to ensure OTEL context management is working correctly