fix: Correct spec-compliance of API Tracer.start_span#1905
Merged
Conversation
The spec states: > The API MUST return a non-recording Span with the SpanContext in the parent Context (whether explicitly given or implicit current). If the Span in the parent Context is already non-recording, it SHOULD be returned directly without instantiating a new Span. If the parent Context contains no Span, an empty non-recording Span MUST be returned instead (i.e., having a SpanContext with all-zero Span and Trace IDs, empty Tracestate, and unsampled TraceFlags). With new tests that match the spec, the current implementation fails with: ``` OpenTelemetry::Trace::Tracer::#start_span#test_0002_returns a non-recording span with the parent span context if the parent span is recording [test/opentelemetry/trace/tracer_test.rb:97]: Expected @context=#<OpenTelemetry::Trace::SpanContext:0x0000000120e17140 @trace_id="E\xC6d\xD5S+h\x8B\xC6u\xE6\xD5\ePQ\xDD", @span_id="\xE2\xB3\x01\x99\xEC\xA0\xC1\x81", @trace_flags=#<OpenTelemetry::Trace::TraceFlags:0x00000001052a50c0 @flags=0>, @tracestate=#<OpenTelemetry::Trace::Tracestate:0x000000012085fdd8 @hash={}>, @Remote=false>> to not be equal to @context=#<OpenTelemetry::Trace::SpanContext:0x0000000120e17140 @trace_id="E\xC6d\xD5S+h\x8B\xC6u\xE6\xD5\ePQ\xDD", @span_id="\xE2\xB3\x01\x99\xEC\xA0\xC1\x81", @trace_flags=#<OpenTelemetry::Trace::TraceFlags:0x00000001052a50c0 @flags=0>, @tracestate=#<OpenTelemetry::Trace::Tracestate:0x000000012085fdd8 @hash={}>, @Remote=false>>. ``` The new implementation correctly passes this test (and the old tests). Shopify has been running with a similar patch in production for several years, based on a belief that the spec was incorrect - instead the _implementation_ was wrong.
kaylareopelle
approved these changes
Aug 27, 2025
kaylareopelle
left a comment
Contributor
There was a problem hiding this comment.
Thanks for catching this and sharing Shopify's patch!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The spec states:
With new tests that match the spec, the current implementation fails with:
The new implementation correctly passes this test (and the old tests). Shopify has been running with a similar patch in production for several years, based on a belief that the spec was incorrect - instead the implementation was wrong 🤦 .
Context: IIRC the API Tracer was written assuming that it would be used only when no SDK was present, however that is not necessarily true (anyone can create a no-op API Tracer instance even if the SDK TracerProvider is installed) and the spec for
TracerConfigstates:The obvious implementation of
TracerConfigwould delegate to an API Tracer instance when a Tracer is disabled.