Skip to content

Conversation

@hassiebp
Copy link
Contributor

@hassiebp hassiebp commented Oct 1, 2025

Important

Sets OTEL span status to ERROR for Langfuse spans marked as ERROR, ensuring consistency with OpenTelemetry.

  • Behavior:
    • Adds _set_otel_span_status_if_error() in LangfuseObservationWrapper to set OTEL span status to ERROR when Langfuse level is ERROR.
    • Calls _set_otel_span_status_if_error() in __init__() and update() methods of LangfuseObservationWrapper.
  • Tests:
    • Adds tests in tests/test_otel.py to verify OTEL span status is set to ERROR for spans and generations with level='ERROR'.
    • Tests cover span creation, updates, and different observation types.
  • Misc:
    • Imports Status and StatusCode from opentelemetry.trace.status in langfuse/_client/span.py.

This description was created by Ellipsis 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_error in the StatefulSpanClient class 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
Filename Score Overview
langfuse/_client/span.py 5/5 Adds OTEL span status synchronization for ERROR level spans with proper error handling
tests/test_otel.py 4/5 Adds comprehensive test coverage for error handling across different observation types

Confidence score: 5/5

  • This PR is safe to merge with minimal risk
  • Score reflects well-implemented feature with comprehensive testing and proper error handling
  • No files require special attention

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"
Loading

@hassiebp hassiebp enabled auto-merge (squash) October 1, 2025 10:07
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@hassiebp hassiebp merged commit 64e812f into main Oct 1, 2025
11 checks passed
@hassiebp hassiebp deleted the otel-status-for-error-spans branch October 1, 2025 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants