Skip to content

Preserve OTel trace contexts when updating baggage - #1615

Merged
shakuzen merged 7 commits into
mainfrom
gh-1602-baggage-tests
Oct 30, 2026
Merged

shakuzen merged 7 commits into
mainfrom
gh-1602-baggage-tests

Conversation

@shakuzen

@shakuzen shakuzen commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

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:

  • Store updated baggage in the target trace context's existing OTel Context, preserving its non-baggage entries.
  • Build the context to attach when 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: OtelBaggageManager reads 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

  • The three original span storage/activation tests fail against main and pass with this fix: two detect storing the unrelated current span, and one detects delayed activation restoring a stale span.
  • The deprecated-set retention regression also fails against main, retaining all 100 earlier trace contexts, and passes with the fix. The retention fixture keeps the last trace context and pooled worker alive; a span processor ensures the SDK creates valid, distinct spans.
  • Four additional precedence tests cover deprecated set, both baggage-scope overloads, and delayed activation. They check conflicting values, entries unique to each source, native current and retained baggage, and scope restoration. Reversing either precedence boundary makes all four fail.
  • Additional guard cases cover metadata precedence, distinct context entries with shared baggage, and restoring a preceding deprecated set through both value overloads.

Benchmarks

Compared main cc9207ba with this PR at d9ae69b2 using OtelBaggageScopeBenchmark. 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.

Scope API Existing entries Recording ns/op: main → PR B/op: main → PR
Current span 0 false 75.8 → 81.2 672 → 648
Current span 0 true 75.1 → 79.2 672 → 648
Current span 8 false 188.6 → 188.5 1,088 → 1,064
Current span 8 true 188.1 → 185.7 1,088 → 1,064
Explicit context 0 false 139.4 → 111.9 1,120 → 728
Explicit context 0 true 139.1 → 112.8 1,120 → 728
Explicit context 8 false 512.0 → 202.6 2,336 → 1,144
Explicit context 8 true 505.5 → 202.0 2,336 → 1,144

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.

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
shakuzen force-pushed the gh-1602-baggage-tests branch from b9ef52e to d376d9f Compare October 28, 2026 12:03
@shakuzen shakuzen modified the milestones: 1.6.x, 1.8.x Oct 28, 2026
@shakuzen shakuzen changed the title Do not store the current span in a trace context when setting baggage Preserve OTel trace contexts when updating baggage Oct 28, 2026
@shakuzen
shakuzen changed the base branch from 1.6.x to main October 28, 2026 12:03
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
shakuzen force-pushed the gh-1602-baggage-tests branch from d376d9f to 07504d6 Compare October 28, 2026 12:06
Signed-off-by: Tommy Ludwig <8924140+shakuzen@users.noreply.github.com>
Signed-off-by: Tommy Ludwig <8924140+shakuzen@users.noreply.github.com>
Signed-off-by: Tommy Ludwig <8924140+shakuzen@users.noreply.github.com>
@shakuzen
shakuzen added this pull request to stack #1637 October 30, 2026 13:12
@shakuzen shakuzen modified the milestones: 1.8.x, 1.8.0-RC2 Oct 30, 2026
@shakuzen shakuzen added bug A general bug performance Issues related to general performance labels Oct 30, 2026
@shakuzen
shakuzen merged commit 7e3b875 into main Oct 30, 2026
7 checks passed
@jonatan-ivanov
jonatan-ivanov deleted the gh-1602-baggage-tests branch November 21, 2026 01:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug A general bug module: bridge-otel performance Issues related to general performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant