SdkSpanBuilder drops invalid parent TraceState when IdGenerator.generatesRandomTraceIds() is true

Open
#9,062 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

@Eliot5566 is already working on this.

Since Nov 26, 2026.

  • #2 by @Eliot5566 — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
java

Research direction

Read SdkSpanBuilder.startSpan() around lines 201–215 and 258, then inspect CompositeSampler.shouldSample() around lines 50 and 95. Trace how an invalid parent SpanContext with a TraceState reaches the sampler when random trace IDs are enabled; the issue is done when the initial state remains observable to the sampler and is preserved in the resulting span. The issue cites related specification discussions in open-telemetry/opentelemetry-specification#5471 and #5472.

Written by the indexing model from the issue text.

Description

Bug SDK
Problem

In SdkSpanBuilder.startSpan(), when creating a root span (!parentSpanContext.isValid()) with a random IdGenerator (idGenerator.generatesRandomTraceIds() == true), parentContextForSampler replaces the Span in parentContext with RANDOM_TRACE_ID_PRIMORDIAL_SPAN, whose TraceState is TraceState.getDefault().

If a caller attaches an invalid SpanContext carrying initial TraceState (for example, ot=rv:... per Do not overwrite explicit randomness) to parentContext:

  • Sampler.shouldSample() cannot observe parentSpanContext.getTraceState() through parentContextForSampler (even though SdkSpanBuilder.java:258 passes parentSpanContext.getTraceState() to samplingResult.getUpdatedTraceState(...)).
  • CompositeSampler.shouldSample() reads traceState from parentContext (now empty) and reuses it in getUpdatedTraceState() (line 95), losing the caller's initial TraceState.

When idGenerator.generatesRandomTraceIds() is false, parentContextForSampler is left unchanged and the caller's TraceState is preserved.

Note: Upstream spec clarification on ShouldSample root TraceFlags and root-span TraceState is also tracked in open-telemetry/opentelemetry-specification#5471 and open-telemetry/opentelemetry-specification#5472.

Dominant language
Java
Stars
2.5k
Forks
1.1k
Avg merge
5d 11h
Merged PRs (30d)
41

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from open-telemetry/opentelemetry-java

All issues in open-telemetry/opentelemetry-java

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.