Span ends prematurely on context cancellation in OpenTelemetry SDK
0 reputation · 01 Dec 2022, 12:45 UTC
0 reputation · 01 Dec 2022, 12:45 UTC
When a request context is cancelled while an OpenTelemetry span is still active, the SDK is expected to end the span, but the exact moment of span completion relative to the cancellation signal varies between language implementations.
The OpenTelemetry Specification defines cancellation propagation but does not prescribe how instrumentation should record partially completed work, leaving it to each SDK to decide whether to keep or discard attributes set after cancellation.
Goal: determine whether the span’s end time aligns with context cancellation and whether any attributes added after the cancellation point are retained or dropped.
What guarantees does the SDK provide about span termination timing?
Which attributes, if any, are omitted when a span is cut short by cancellation?
How can users observe or configure this behavior consistently across different language SDKs?
33275 reputation · 02 Dec 2022, 00:24 UTC
The OpenTelemetry SDK guarantees that a span ends as soon as the Context it is bound to reaches a done state (cancelled or deadline exceeded). The SDK checks the context before finishing the span; if ctx.Err() is non‑nil, it invokes Span.End internally, so the span’s end time aligns with the moment the cancellation signal is observed.
The SDK ties a span’s lifetime to its parent Context. When the Context becomes done, the Span implementation treats that as a signal to finish the span, which matches the OTel specification that links span life‑cycle to context propagation.
Instrumentation code (or the Span constructor) reads ctx.Err(); if it returns an error, Span.End is called immediately. Any calls to SetAttribute, AddEvent, or similar that occur after this check are ignored because the span is already ended and no longer accepts modifications.
Are you creating spans manually or relying on automatic instrumentation? The answer determines whether you need to adjust Context propagation or configure a custom SpanProcessor.
Use comments to ask for clarification. Post a solution as an answer.
33,275 reputation · 01 Dec 2022, 14:25 UTC
It is important to clarify a distinction between the cancellation signal (the context state) and the span lifecycle (the End() call). In most OpenTelemetry SDK implementations, the SDK does not automatically monitor the context to force-close a span the moment a cancellation occurs.
Instead, the span typically remains open until the application logic reacts to the cancellation and explicitly calls span.End(). This has two practical implications for data integrity:
End() is called—are generally retained and exported.To accurately capture the cancellation point, it is recommended to manually record a Span Event or set the SpanStatus to Error immediately upon detecting ctx.Err() != nil before closing the span.