feat(api): add random-trace-id flag to TraceFlags - #142
Open
harshitt13 wants to merge 1 commit into
Open
harshitt13 wants to merge 1 commit into
harshitt13 wants to merge 1 commit into
Conversation
Signed-off-by: harshitt13 <find.harshitkushwaha@gmail.com>
harshitt13
requested review from
michaelbushe and
robert-northmind
as code owners
September 13, 2026 23:39
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.
Refs #104
What
Adds
RANDOM_FLAG(0x02),isRandomandwithRandomtoTraceFlags, mirroring the existing sampled pair.The flags byte already survived propagation via
asByte, so an incoming02was re-emitted as02by accident of the whole byte being copied. What was missing was the ability to express the bit: nothing could ask whether an incoming trace ID is declared random, and the SDK had no supported way to set it on a root span context.W3C Trace Context Level 2 makes preserving the bit across a continued trace a MUST. https://www.w3.org/TR/trace-context-2/#random-trace-id-flag
Notes
withRandomdoes not verify randomness and cannot. Setting the flag is an assertion by the caller; enforcing it belongs to the SDK, which only sets it for a conforming ID generator. Documented on the method.TraceFlagsis a bit set and thatflags == TraceFlags.sampledis wrong once a second bit exists.Added, notFixed. Nothing changed for existing callers, so this is new public surface under Keep a Changelog. Per the conventions comment the spec citation carries the audit provenance, and fix(api): Context-driven span creation and root span support (#83, #84) #118 set the precedent of an audit item landing its additive surface underAdded.TraceFlags.randomconstant. The class doc argues against having named whole-value constants to compare against.fromStringleniency ([BUG] Trace API: TraceFlags.fromString coerces invalid input to 0x00 (API half) #92), and the mutablenoneandsampledstatics, which I filed separately as #NNN.Follow-up: whole-value comparisons in tests
Grepping
== TraceFlagsandequals(TraceFlags...)turns up 9 sites. Only 4 should change.Wrong model, assert "is sampled" via whole-value equality:
span_context_test.dartlines 73, 119, 168, 169.Legitimate, leave alone, round-trip assertions where the whole byte is the subject:
span_context_test.dartlines 56, 254, 261, 281, andspan_context_create_test.dart:36.The criterion is what the assertion is about. A round trip on
withTraceFlagsasserts the setter stored the byte you handed it. A sampling check asserts a predicate, and equality only answers it while no other bit is set.The clearest case is line 150, a test named
'trace flags indicates sampling state'whose entire subject is the sampling predicate, tested by whole-value equality. Three of the four also carry a comment reading// Use accessor method instead of isSampled propertydirectly above an==comparison, which has it backwards.All 4 are inert today: they build their flags explicitly, so no random bit reaches them. They fail once root spans start emitting
03.git log -Lputs them in the initial revision, so it is original habit rather than a later sweep, which means the SDK repo likely carries the same idiom from the same authorship. That is the urgent half, since that is where #132 makes root spans03for real.Testing
Covers all four combinations of the two bits, both orders of application, that each
with*preserves the other bit, and round trips throughfromString/toString.