Skip to content

[processor/dynamic_sampling] honour incoming ot=th and ot=rv#49520

Open
MikeGoldsmith wants to merge 1 commit into
open-telemetry:mainfrom
MikeGoldsmith:mike/dynamicsampling-incoming-threshold
Open

[processor/dynamic_sampling] honour incoming ot=th and ot=rv#49520
MikeGoldsmith wants to merge 1 commit into
open-telemetry:mainfrom
MikeGoldsmith:mike/dynamicsampling-incoming-threshold

Conversation

@MikeGoldsmith

@MikeGoldsmith MikeGoldsmith commented Jul 7, 2026

Copy link
Copy Markdown
Member

Description

Implements the "Read incoming ot=th" alpha item from the dynamic_sampling tracking issue.

  • Reads any incoming W3C tracestate on accumulated spans and picks up:
    • the strictest observed ot=th as the upstream sampling threshold;
    • ot=rv as the randomness value when present, falling back to the last 7 bytes of the trace ID otherwise.
  • Combines the rule-selected sampler's rate N with the upstream threshold under consistent probability sampling semantics: emitted threshold T_ours is chosen so that 1/N of the surviving upstream traffic passes, giving an effective absolute keep probability of P(rand > T_up) / N. Downstream metric consumers reading ot=th reconstruct the correct adjusted count for the original population without additional configuration.
  • Preserves UpdateTValueWithSampling's spec-mandated behaviour: a stricter incoming threshold is never lowered.
  • Adds a counter processor_dynamic_sampling_incoming_tracestate_unparseable to observe cases where the incoming tracestate fails to parse on the emit path.

Known limitation: the adaptive samplers (ema_dynamic, ema_throughput, windowed_throughput) still pick their rate from observed input, which under upstream sampling is a skewed slice of the population. Emitted thresholds remain metric-accurate downstream, but per-key rate math is biased. Follow-up: #49517.

Link to tracking issue

Related: #49311

Testing

  • Unit tests for combineThreshold (rate 1 passthrough, no-upstream 10x, upstream+10x composition).
  • Integration test asserting emitted ot=th reflects composed effective threshold when upstream ot=th is present.
  • Integration test asserting late-arriving spans with a stricter incoming ot=th retain that threshold rather than being lowered by the cached decision.
  • Integration test covering unparseable incoming tracestate (counter path).
  • go test -race ./... clean.

Documentation

Authorship

  • I, a human, wrote this pull request description myself.

The processor honours any incoming `ot=th` (sampling threshold) and `ot=rv` (explicit randomness) already set on incoming spans by an upstream sampler, such as an SDK head sampler or a probabilistic collector processor:

- **Randomness.** If an incoming span carries `ot=rv`, that value is used to make the sampling decision. Otherwise the last 7 bytes of the trace ID are used, per the consistent probability spec.
- **Threshold composition.** The rule-selected sampler picks a rate `N`; the emitted `ot=th` is composed on top of any upstream threshold `T_up` so the effective absolute keep probability is `P(rand > T_up) / N`. Downstream metric consumers reading `ot=th` reconstruct the correct adjusted count for the original population without additional configuration.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the same as the "equalizing" mode documented in
https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/processor/probabilisticsamplerprocessor#equalizing?

The way this reads there's a conditional probability formula lurking somewhere (true?).


### Known limitation: adaptive-sampler rate bias under upstream sampling

The adaptive samplers (`ema_dynamic`, `ema_throughput`, `windowed_throughput`) select their rate from the traffic they observe, assuming that input is a uniform sample of the underlying population. When an upstream stage has already sampled, the input reaching the adaptive sampler is skewed toward higher randomness values, and per-key rate math based on observed volume is biased. The emitted `ot=th` is still metric-accurate downstream (via threshold composition above); the caveat is that the rate the adaptive sampler *targets* is "of the input reaching me," not "of the original population." Follow-up in [#49517](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/49517).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Cool)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants