Description
When creating a root span (no parent span) in the Langfuse tracer, the trace_id from Haystack's tracing context was not being passed to Langfuse's start_as_current_observation method. This caused custom trace IDs set in the tracing context to be ignored, making it difficult to correlate traces across different systems or maintain consistent trace identifiers.
Root Cause
In the DefaultSpanHandler.create_span() method, when creating a root span, the code was calling start_as_current_observation() without passing the trace_context parameter that contains the trace_id from the Haystack tracing context.
Fix
- Import
TraceContext type from langfuse.types
- Extract
trace_id from the tracing context (tracing_ctx)
- Create a
trace_context dictionary with the trace_id if it exists
- Pass
trace_context parameter to start_as_current_observation()
Impact
This fix ensures that when users set a custom trace_id in the Haystack tracing context, it will be properly propagated to Langfuse, enabling better trace correlation and consistent trace identification across distributed systems.
Testing
Added test case test_create_span_root_trace to verify that the trace_id is correctly extracted from the tracing context and passed to Langfuse when creating a root span.
Description
When creating a root span (no parent span) in the Langfuse tracer, the
trace_idfrom Haystack's tracing context was not being passed to Langfuse'sstart_as_current_observationmethod. This caused custom trace IDs set in the tracing context to be ignored, making it difficult to correlate traces across different systems or maintain consistent trace identifiers.Root Cause
In the
DefaultSpanHandler.create_span()method, when creating a root span, the code was callingstart_as_current_observation()without passing thetrace_contextparameter that contains thetrace_idfrom the Haystack tracing context.Fix
TraceContexttype fromlangfuse.typestrace_idfrom the tracing context (tracing_ctx)trace_contextdictionary with thetrace_idif it existstrace_contextparameter tostart_as_current_observation()Impact
This fix ensures that when users set a custom
trace_idin the Haystack tracing context, it will be properly propagated to Langfuse, enabling better trace correlation and consistent trace identification across distributed systems.Testing
Added test case
test_create_span_root_traceto verify that thetrace_idis correctly extracted from the tracing context and passed to Langfuse when creating a root span.