Repository navigation
Preserve OTel trace contexts when updating baggage - #1615
Merged
Merged
Conversation
This was referenced Oct 20, 2026
Prior to this change, OtelBaggageInScope stored the thread's current Context, with the new baggage, in the OtelTraceContext the baggage was set for. When another span was in scope, the trace context then held that span and its OtelTraceContext. The deprecated Baggage#set(TraceContext, String) never restores the stored Context, so calling it for each new span while the previous span was in scope made every trace context keep all the ones before it reachable. This change stores the baggage in the trace context's own Context instead. The stored baggage and the Context made current on the thread are unchanged. See gh-1602 Signed-off-by: Tommy Ludwig <8924140+shakuzen@users.noreply.github.com>
Without a span processor, newer OpenTelemetry SDK versions (e.g. the one on main) only create non-recording spans, and root spans are then all the same invalid span. OtelBaggageInScopeTests then can't tell the spans apart, and OtelBaggageRetentionTests runs on invalid spans. See gh-1602 Signed-off-by: Tommy Ludwig <8924140+shakuzen@users.noreply.github.com>
shakuzen
force-pushed
the
gh-1602-baggage-tests
branch
from
October 28, 2026 12:03
b9ef52e to
d376d9f
Compare
Prior to this change, OtelBaggageInScope#doSet captured the thread's current Context with the baggage, and makeCurrent() attached that snapshot later. When baggage was set with the deprecated Baggage#set(String) and made current later, for example in the scope of another span, makeCurrent() replaced the thread's current span with the one that was current when the baggage was set. This change makes makeCurrent() build the Context to attach from the thread's current Context when it is called, and doSet only stores the baggage in the trace context. The merge of the baggage is now in one place, and the context to restore when the scope is closed is recorded by whichever of doSet and makeCurrent stores into the trace context first. Closing a baggage scope that was not set with doSet restores that context, instead of storing null in the trace context. See gh-1602 Signed-off-by: Tommy Ludwig <8924140+shakuzen@users.noreply.github.com>
shakuzen
force-pushed
the
gh-1602-baggage-tests
branch
from
October 28, 2026 12:06
d376d9f to
07504d6
Compare
Signed-off-by: Tommy Ludwig <8924140+shakuzen@users.noreply.github.com>
Signed-off-by: Tommy Ludwig <8924140+shakuzen@users.noreply.github.com>
shakuzen
force-pushed
the
gh-1602-baggage-tests
branch
from
October 29, 2026 08:30
2548344 to
d9ae69b
Compare
Signed-off-by: Tommy Ludwig <8924140+shakuzen@users.noreply.github.com>
This was referenced Oct 30, 2026
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.
See #1602.
Setting baggage for trace context A while span B is current could copy B's span, bridge trace context, and other context entries into A. Repeated deprecated
Baggage#set(TraceContext, String)calls can consequently retain a chain of earlier trace contexts.This PR makes two corrections:
Context, preserving its non-baggage entries.makeCurrent()is called. Previously, setting baggage under span A and activating it later under B could make A current again by attaching the earlier snapshot.Setting/scoping baggage merges retained target baggage, then current baggage, then the entry being set (later sources win). This preserves the setter's precedence and also applies it to direct
makeCurrent()calls. Activation uses the baggage current at that time; scope closure restores the previous retained context.Note:
OtelBaggageManagerreads instead prefer retained trace-context values over current baggage. This existing difference is preserved here and is something to consider in a separate precedence-policy review.Validation
Benchmarks
Compared main
cc9207bawith this PR atd9ae69b2usingOtelBaggageScopeBenchmark. JMH 1.37, OTel 1.67.0, Java 25.0.4.1, Apple M5 Pro.Each operation opens/closes a span scope containing one baggage scope. Span creation is outside measurement. Both overloads target the current span; 0/8 existing baggage entries, empty tag-field lists, and recording/valid non-recording spans are covered.
The populated current-span path is close to main; the explicit-context path is about 60% faster with eight entries. The empty current-span path retains roughly 4–5 ns/op overhead in these runs.
Follow-ups
Tag-field matching and redundant explicit-context factory setup are being optimized separately in #1635.
Explicit baggage-handle binding remains in #1622. The facade injection-context policy and the manager's different read precedence remain separate decisions. When rebasing #1599, retain this PR's restoration logic and reconcile its overlapping
makeCurrent()changes.Targets main; maintenance backports can be evaluated separately.