Skip to content

[CPLAT-13467] tracing: add honor_parent_sampling_decision to the OpenTelemetry tracer - #31

Open
ku524 wants to merge 7 commits into
release/v1.39.2-sendbird-customfrom
honor-parent-sampling-decision
Open

ku524 wants to merge 7 commits into
release/v1.39.2-sendbird-customfrom
honor-parent-sampling-decision

Conversation

@ku524

@ku524 ku524 commented Nov 30, 2026 •

Copy link
Copy Markdown

JIRA: https://sendbird.atlassian.net/browse/CPLAT-13467

Summary

Adds an opt-in OpenTelemetry tracer field, honor_parent_sampling_decision (default false). When it is true, a span that continues a valid parent span context keeps the parent's sampled flag (or the configured sampler's decision) if the request was only not selected by random_sampling. This follows the design proposed upstream in envoyproxy#49638.

Why

  • Since Envoy 1.39 (upstream tracing: make otel tracer respect envoy tracing decision envoyproxy/envoy#45830), Driver::startSpan applies the HCM tracing decision as a final veto (applyTracingDecision sets sampled=false), also when a sampled parent context was extracted.
  • Our Istio Telemetry uses randomSamplingPercentage: 0. So every sidecar hop rewrites the propagated x-sendbird-traceparent flag from 01 to 00, and the services behind it drop their spans.
  • This breaks soda traces such as ws to ws-backend. In preprod, a request with x-sendbird-traceparent: ...-01 loses sampling after one sidecar. Sent directly to the app, the flag is honored.

Change

  • HCM: ConnectionManagerUtility::mutateTracingRequestHeader now returns the trace reason plus parent_sampling_allowed. It is true only when the request has no x-client-trace-id, has a well-formed request ID, was not selected by random_sampling, and passes overall_sampling. To compute this, overall_sampling is also evaluated for requests not selected by random_sampling. It only reads runtime and changes no header.
  • The HCM computes the signal only when the tracer reads it: Tracing::Tracer and Tracing::Driver get usesParentSamplingAllowed() (default false), TracerImpl forwards it, and the OpenTelemetry driver returns the config field. Other tracers and the option-off path therefore do no extra runtime lookup.
  • Request ID form: RequestIDExtension gets isValidRequestId(). Its default keeps today's rule (getInteger() succeeds), and the UUID extension checks the full 8-4-4-4-12 hex form. getInteger() alone reads only the last 8 hex digits, so an ID such as xxxxxxxx-xxxx-xxxx-xxxx-xxxx09f48a28 would otherwise count as valid.
  • The HCM keeps the flag in ActiveStream::State and sets Tracing::Decision::parent_sampling_allowed (new field, default false) in traceRequest(), only when the reason is NotTraceable. A health check gets reason HealthCheck, so it stays false.
  • Why not stream info or x-request-id: x-request-id only encodes a Reason and propagates across hops, but this signal is per hop. Adding it to StreamInfo would change a public interface and every implementation and mock. Only span start reads the signal, so the stream state is enough.
  • Route refresh: the decision is taken once, when the span starts. A span that continues a parent context ignores later route refreshes, as in Envoy 1.38 and as the base branch already does for spans with useLocalDecision() == false. refreshTracing() keeps the base behavior and does not compute the signal.
  • Internal redirect: the recreated stream does not mutate tracing headers again (unchanged), so parent_sampling_allowed stays false and the base veto applies. recreateStream() matches the base branch.
  • OpenTelemetry driver: with the field true, a valid W3C traceparent (checked with Tracing::isValidTraceParent(), so version ff is rejected), reason NotTraceable and parent_sampling_allowed, the span skips the veto. The non-honor path still uses the existing extractor. Root spans (no parent context) are not changed.
  • Explicit rejections still veto: overall_sampling, client_sampling for x-client-trace-id, missing or invalid request ID, and health checks.
  • Proto: the docs state that this build reads the parent from x-sendbird-traceparent (and x-sendbird-tracestate), not the standard traceparent header.
  • Proto: bool honor_parent_sampling_decision = 9; in OpenTelemetryConfig (next free field in this branch). Upstream opentelemetry: option to honor a sampled parent context when a request is only not selected by random_sampling envoyproxy/envoy#49638 proposes 13, so the number can conflict if upstream adds other fields first. Changelog fragment added under changelogs/current/new_features/.
flowchart TD
  A[Request with a valid parent span context] --> B{HCM decision traced?}
  B -- yes --> S[Keep parent or sampler decision]
  B -- no --> C{honor_parent_sampling_decision set?}
  C -- no --> V[Veto: span unsampled, flag 00 injected]
  C -- yes --> D{reason NotTraceable and parent_sampling_allowed?}
  D -- no --> V
  D -- yes --> S
  subgraph HCM sets parent_sampling_allowed only when
    H1[No x-client-trace-id]
    H2[Valid request ID]
    H3[Not selected by random_sampling]
    H4[Passes overall_sampling]
    H5[Not a health check]
  end
Loading

How to enable

tracing:
  provider:
    name: envoy.tracers.opentelemetry
    typed_config:
      "@type": type.googleapis.com/envoy.config.trace.v3.OpenTelemetryConfig
      honor_parent_sampling_decision: true

Istio does not expose this field. It needs an EnvoyFilter patch on the OpenTelemetry provider config, or a change in istiod.

Testing

Not compiled or tested locally. The local Docker VM ran out of memory while building the test targets, so this PR relies on CI. Formatted with clang-format 21.1.8 on the changed hunks; the pinned format check has not been run.

Codex review rounds:

  • Round 1 (fixed in afd1317, refresh part removed in round 4, redirect part removed in round 6): route refresh did not re-apply an overall_sampling rejection to a span that kept its parent's decision; internal redirects lost the signal; the honor branch accepted a traceparent with version ff. Declined: a separate request ID format check.
  • Round 2 (fixed in 4562da1): the request ID format check, accepted after all, with a defaulted isValidRequestId() like the round 1 span hook; proto docs now name x-sendbird-traceparent; the extra overall_sampling lookup now runs only when the tracer reads the signal.
  • Round 3 (fixed in e1499da, refresh part later removed in round 4): with the option on, a refresh that rejects the request now also vetoes a parent span first traced by random sampling; direct Abseil dependency added to the UUID extension. Declined: an RFC variant check in isValidRequestId(), because Envoy's own UUID handling never checks the variant, and clients or proxies that send UUID-shaped IDs with another variant would silently lose parent sampling; the shape check already blocks the parse-only IDs raised in rounds 1 and 2.
  • Round 4 (2e9946f): refresh handling removed; the decision is fixed at span start like 1.38. Reason: each review round found another refresh edge case (restoring a decision, the disableLocalDecision() contract, upstream child spans), and the diff kept growing.
  • Round 5 (fixed in 2fdb7af, later removed in round 6): an internal redirect lost a first-pass client_sampling rejection when the original route removed x-client-trace-id. The recreated stream now inherits the first-pass result.
  • Round 6 (01d39a6): internal redirect support removed; a redirected stream keeps the base veto. Reason: rounds 5 and 6 each found another redirect edge case (random selection on the target route not reflected, health check status lost on recreate). Declined: the UUID variant check (same reason as round 3), and W3C higher-version traceparent with extension fields, which is pre-existing extractor behavior on the base branch and out of scope.

New and updated tests:

  • conn_manager_utility_test: allowed when only not selected by random sampling; not allowed for overall sampling rejection (both before and after selection), selection by random sampling, client_sampling rejection of x-client-trace-id, missing or invalid request ID, and use_request_id_for_trace_sampling: false. Existing tests are unchanged, because the extra read only happens when the tracer reads the signal.
  • conn_manager_impl_test: ParentSamplingAllowedReachesTracer (parameterized on overall sampling) checks the decision passed to the tracer.
  • conn_manager_utility_test: ParentSamplingNotComputedWhenTracerDoesNotUseIt (no overall_sampling lookup) and ParentSamplingNotAllowedWithMalformedRequestId.
  • request_id/uuid/config_test: IsValidRequestId. tracer_impl_test: UsesParentSamplingAllowedFollowsDriver. conn_manager_impl_test: ParentSamplingAllowedNotComputedForTracerThatIgnoresIt.
  • conn_manager_impl_test: InternalRedirectDoesNotAllowParentSampling (the redirected stream's decision has parent_sampling_allowed == false).
  • opentelemetry_tracer_impl_test: version ff parent is vetoed;
  • opentelemetry_tracer_impl_test: option off vetoes a sampled parent; option on keeps parent 01 (span exported, injected flag 01) and parent 00; vetoes when the HCM does not allow it or the reason is HealthCheck; root spans unchanged; with the always_on sampler the sampler decision is kept and still vetoed when not allowed.

Known limits and open questions

  • An internally redirected request does not keep the parent's decision; the base veto applies.
  • A route refresh to a route whose overall_sampling rejects the request does not unsample a span that kept its parent's decision.
  • Gateway exposure: with the field on at an ingress gateway, an external client can force sampling by sending a -01 parent. Enable it only on sidecars or where the parent context is trusted.
  • Health checks are marked by the health check filter after the span starts. With the field on, a health check request that carries a sampled parent can be sampled.
  • Three existing tests in opentelemetry_tracer_impl_test.cc (UseLocalDecisionFalse, PropagatedSpanWithAlwaysOnSamplerStillHonorsEnvoyDecision, AlwaysOnSamplerExportsWhenEnvoyDecisionAllowsTracing) set a literal traceparent header, but this fork reads x-sendbird-traceparent. They may already fail on the base branch. This PR does not change them; CI will show.
  • Changelog: [CPLAT-13577] redis: close the upstream connection when a reply type does not match the pending request #29 did not add an entry for a fork patch. This PR adds one because the field is a user-facing API addition. It can be removed if the fork prefers.

AI assistance

The change and the tests were prepared with AI assistance (Claude Code).

…Telemetry tracer

Since envoyproxy#45830 the OpenTelemetry driver applies the HCM tracing decision as a
final veto, also when a sampled parent span context is extracted. With
random_sampling below 100 percent, every hop rewrites a sampled parent to
unsampled.

The HCM now reports whether a request was only not selected by
random_sampling (no x-client-trace-id, valid request ID, passed
overall_sampling). It carries this as Tracing::Decision::parent_sampling_allowed.
The new opt-in honor_parent_sampling_decision field lets the OpenTelemetry
tracer keep the parent's or the sampler's decision in that case only.
Default behavior is unchanged.

Follows the design proposed in envoyproxy#49638.
@upwind-code-us

upwind-code-us Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Upwind Code Scan

Upwind code scan | ⏭️ Skipped

No scans ran: this PR changes nothing Upwind scans.

Last updated: 01d39a6 · 2026-11-30 08:15 UTC

…g_decision

- Re-apply the refreshed HCM decision to a span that kept its parent's
  decision. Spans get a defaulted refreshTracingDecision() hook; only the
  OpenTelemetry span that took the honor branch acts on it.
- Compute parent_sampling_allowed for internally created streams against
  the target route without mutating the tracing headers again.
- Require a valid W3C traceparent before the parent overrides the Envoy
  decision.
…g_decision

- Require a well-formed request ID before allowing the parent's decision.
  RequestIDExtension gets isValidRequestId(), defaulting to getInteger();
  the UUID extension checks the full UUID form.
- Compute parent_sampling_allowed only when the tracer reads it, through a
  defaulted usesParentSamplingAllowed() on Tracer and Driver, so other
  tracers and the option-off path do no extra runtime lookup.
- Document that this build reads the x-sendbird-traceparent header.
…g_decision

- With the option on, apply a refreshed not-traced decision to every
  parent-context span the driver started, not only to spans that took the
  honor branch. Option off is unchanged.
- Declare the direct Abseil strings dependency of the UUID extension.
Remove the route refresh handling added in review rounds 1 and 3. A span
that continues a parent context now ignores later route refreshes, as in
Envoy 1.38 and as the base branch already does for spans that do not use
the local decision. refreshTracing() keeps the base behavior.
…oss internal redirects

The recreated stream inherits the original stream's parent_sampling_allowed
and only narrows it for the target route. A client_sampling rejection now
survives a route that removed x-client-trace-id.
…nt_sampling_decision

An internally created stream does not mutate tracing headers again, so it
now keeps the default veto. This removes the redirect helper and the
recreateStream() copy; recreateStream() matches the base branch.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant