feat: propagate trace attributes onto all child spans on update#1396
Merged
hassiebp merged 2 commits intosteffen/nested-context-propagationfrom Oct 8, 2025
Merged
Conversation
df6b82d
into
steffen/nested-context-propagation
7 of 11 checks passed
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.
Important
Introduces
correlation_contextfor trace attribute propagation to child spans, deprecatingupdate_current_traceandupdate_trace.correlation_context()inclient.pyto propagate trace attributes to child spans.update_current_trace()inclient.pyandupdate_trace()inspan.py.LangfuseSpanProcessorinspan_processor.pyto propagate correlation context to spans.get_attribute_key_from_correlation_context()inutils.pyfor attribute key mapping.test_core_sdk.pyforcorrelation_context()propagation ofuser_id,session_id, and metadata.This description was created by
for cf3ec39. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Overview
Updated On: 2025-10-07 17:24:05 UTC
Summary
This PR introduces a new `correlation_context` feature that enables automatic propagation of trace-level attributes (user_id, session_id, metadata) to all child spans within a given scope, replacing the manual approach of calling `update_trace()` on each span individually.Key Changes Made:
New Context Manager API: A new
langfuse.correlation_context()context manager allows developers to set trace attributes once and have them automatically applied to all nested spans and generations within that scope.OpenTelemetry Integration: The implementation leverages OpenTelemetry's context propagation mechanisms, supporting both local context and baggage propagation modes. The baggage mode enables cross-service attribute propagation in distributed tracing scenarios.
Automatic Span Processing: The
LangfuseSpanProcessornow includes anon_start()method that automatically reads correlation context from both OpenTelemetry baggage and local context, then applies relevant attributes to new spans as they're created.Deprecation Path: The existing
update_current_trace()andspan.update_trace()methods are deprecated with clear warnings directing users to the new correlation context approach.Support Infrastructure: New utility functions and constants support the attribute mapping between correlation context keys and OpenTelemetry span attributes, with special handling for well-known attributes like user_id and session_id.
This change addresses a common pain point in complex AI application tracing where maintaining consistent context across nested operations (like agent workflows, tool calls, etc.) was previously manual and error-prone. The new API follows OpenTelemetry best practices and provides a more ergonomic developer experience.
Important Files Changed
Changed Files
correlation_contextcontext manager with OpenTelemetry integration and deprecatesupdate_current_traceon_startmethod to inherit correlation context on child spansupdate_tracemethod with minor import reorganizationConfidence score: 4/5
Sequence Diagram
sequenceDiagram participant User participant Langfuse as Langfuse Client participant SpanProcessor as LangfuseSpanProcessor participant CorrelationContext as Correlation Context participant OTELSpan as OpenTelemetry Span participant API as Langfuse API User->>Langfuse: start_as_current_span(name="parent") Langfuse->>OTELSpan: create parent span User->>Langfuse: correlation_context({"user_id": "123", "session_id": "456"}) Langfuse->>CorrelationContext: set context values CorrelationContext->>CorrelationContext: store in OTEL context User->>Langfuse: start_span(name="child") Langfuse->>OTELSpan: create child span SpanProcessor->>SpanProcessor: on_start() SpanProcessor->>CorrelationContext: get_value(LANGFUSE_CORRELATION_CONTEXT_KEY) CorrelationContext-->>SpanProcessor: return correlation context SpanProcessor->>SpanProcessor: process correlation context loop For each context key-value pair SpanProcessor->>SpanProcessor: get_attribute_key_from_correlation_context(key) SpanProcessor->>OTELSpan: set_attribute(attribute_key, value) end SpanProcessor->>SpanProcessor: propagate baggage attributes User->>Langfuse: flush() Langfuse->>SpanProcessor: export spans SpanProcessor->>API: send span data with propagated attributes Note over SpanProcessor: All child spans automatically inherit<br/>trace attributes from correlation context