Skip to content

Best practices for Span lifecycle for Streaming APIs #5759

Open
@surbhigarg92

Description

@surbhigarg92

I’m looking for best practices around span lifecycle management in Node.js SDK using OpenTelemetry.

We have a streaming API where we create a new span at the start of the stream. Currently, we end the span in the end event handler. However, in some edge cases (e.g., unhandled errors or early termination), the end event is not emitted. To ensure the span is always ended, we’ve also added span.end() in the finish event handler.

This approach works, but when the OpenTelemetry trace debugger is enabled, we get error Error: span is already ended

Is it safe to call span.end() multiple times? I am able to handle this by adding a check (span as any).ended === false but since ended property is not exposed, I am not sure if this is the right way ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions