fix(client): setting OTEL span status as error on Langfuse error (#1387) #1388
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
Sets OTEL span status to ERROR for Langfuse spans marked as ERROR, ensuring consistency with OpenTelemetry.
_set_otel_span_status_if_error()inLangfuseObservationWrapperto set OTEL span status to ERROR when Langfuse level is ERROR._set_otel_span_status_if_error()in__init__()andupdate()methods ofLangfuseObservationWrapper.tests/test_otel.pyto verify OTEL span status is set to ERROR for spans and generations with level='ERROR'.StatusandStatusCodefromopentelemetry.trace.statusinlangfuse/_client/span.py.This description was created by
for 7e9cde2. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Overview
Updated On: 2025-10-01 10:07:55 UTC
Summary
This PR adds automatic synchronization between Langfuse ERROR level spans and OpenTelemetry span status. When a Langfuse span is created or updated with `level='ERROR'`, the underlying OpenTelemetry span status is now automatically set to `StatusCode.ERROR` with an optional description from `status_message`.The implementation adds a new private method
_set_otel_span_status_if_errorin theStatefulSpanClientclass that checks if the level is 'ERROR' and the OTEL span is recording, then sets the appropriate error status. This method is called during span initialization and updates to ensure consistent error state representation.This change enhances integration with OpenTelemetry-compatible monitoring systems by ensuring error spans in Langfuse are properly recognized as errors by downstream OTEL processors, APM tools, and monitoring dashboards that rely on OTEL span status. The implementation includes silent error handling to maintain backward compatibility and avoid disrupting existing workflows.
The PR includes comprehensive test coverage with 8 new test methods that verify the error handling functionality across different scenarios including span creation, updates, generation handling, multiple updates, and edge cases.
Important Files Changed
Changed Files
Confidence score: 5/5
Sequence Diagram
sequenceDiagram participant User participant LangfuseClient as "Langfuse Client" participant LangfuseSpan as "LangfuseObservationWrapper" participant OtelSpan as "OpenTelemetry Span" User->>LangfuseClient: "start_span(level='ERROR')" LangfuseClient->>LangfuseSpan: "create LangfuseObservationWrapper" LangfuseSpan->>LangfuseSpan: "__init__(level='ERROR')" LangfuseSpan->>LangfuseSpan: "_set_otel_span_status_if_error()" LangfuseSpan->>OtelSpan: "set_status(StatusCode.ERROR)" LangfuseSpan-->>LangfuseClient: "return span instance" LangfuseClient-->>User: "return span" User->>LangfuseSpan: "update(level='ERROR')" LangfuseSpan->>LangfuseSpan: "_set_otel_span_status_if_error()" LangfuseSpan->>OtelSpan: "set_status(StatusCode.ERROR)" LangfuseSpan-->>User: "return self" User->>LangfuseSpan: "end()" LangfuseSpan->>OtelSpan: "end()" LangfuseSpan-->>User: "return self"