Skip to content

opentelemetry tracing provider doesn't respect client_sampling when both traceparent and x-client-trace-id header is present #35721

Closed

Description

@yuanguolang

Title: opentelemetry tracing provider doesn't respect client_sampling when both traceparent and x-client-trace-id header is present

Description:
Hi, we enabled opentelemtry tracing provider and we found out client_sampling in tracing is not applied if the incoming request has both traceparent and x-client-trace-id header. We also use x-request-id by default. Even if we set client_sampling to 0%, envoy still exports spans based on sample flag in parent context(traceparent) header. And envoy code seems align with the behavior. I wonder if this is the expected behavior, which means if envoy can extract the parent context from the incoming request, envoy will respect the sampling decision in the downstream, then no sampling decision will be applied.

in conn-manager, envoy startspan no matter what tracing decision made earlier:

active_span_ = connection_manager_.tracer().startSpan(
*this, *request_headers_, filter_manager_.streamInfo(), tracing_decision);

in opentelemetry tracer, if envoy can extract parent context, which is a valid traceparent header, it will start a span from parent context, ignoring tracing decision made earlier.

if (!extractor.propagationHeaderPresent()) {
// No propagation header, so we can create a fresh span with the given decision.
Tracing::SpanPtr new_open_telemetry_span =
tracer.startSpan(config, operation_name, stream_info.startTime(), tracing_decision);
new_open_telemetry_span->setSampled(tracing_decision.traced);
return new_open_telemetry_span;
} else {
// Try to extract the span context. If we can't, just return a null span.
absl::StatusOr<SpanContext> span_context = extractor.extractSpanContext();
if (span_context.ok()) {
return tracer.startSpan(config, operation_name, stream_info.startTime(),
span_context.value());
} else {

thank you!

Activity

  1. added
    triage
    on Aug 15, 2024
  2. adisuissa commented on Aug 15, 2024

    @adisuissa
    Contributor

    cc @AlexanderEllis @htuch as code owners.
    cc @wbpcode who has lots of tracing expertise.

  3. added and removed
    triage
    on Aug 15, 2024
  4. yuanguolang commented on Aug 15, 2024

    @yuanguolang
    Author

    Because in the envoy documentation(link), client_samping is:

    Target percentage of requests managed by this HTTP connection manager that will be force traced if the x-client-trace-id header is set.

    So I thought if the x-client-trace-id is present, no matter if parent context can be extracted, client_sampling should be respected. Please correct me if I understand it wrong here.

  5. github-actions commented on Oct 1, 2024

    @github-actions

    This issue has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in the next 7 days unless it is tagged "help wanted" or "no stalebot" or other activity occurs. Thank you for your contributions.

  6. added
    stale
    on Oct 1, 2024
  7. joaopgrassi commented on Oct 2, 2024

    @joaopgrassi
    Contributor

    When if (!extractor.propagationHeaderPresent()) { is true, meaning, there's no traceparent, Envoy invokes startSpan and sets the sampling decision that is already there, which should be the percentage one if you don't have anything else configured (a custom sampler in the OTel tracer). It will call this line https://github.com/envoyproxy/envoy/blob/main/source/extensions/tracers/opentelemetry/tracer.cc#L261 which should set whatever tracing decision Envoy made.

    Now, if the incoming request has a traceparent header, then the tracer respects that and uses whatever sampling decision was already recorded: https://github.com/envoyproxy/envoy/blob/main/source/extensions/tracers/opentelemetry/tracer.cc#L284. This is the expected behavior, otherwise you may break the entire trace chain.

    So, my conclusion is: When it's a root span, started by Envoy, the sampling percentage config should be applied and work. But when it's not a root span (meaning it is part of a existing trace), then Envoy simply respects the previous sampling decision which is the correct and expected behavior.

  8. removed
    stale
    on Oct 2, 2024
  9. github-actions commented on Nov 1, 2024

    @github-actions

    This issue has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in the next 7 days unless it is tagged "help wanted" or "no stalebot" or other activity occurs. Thank you for your contributions.

  10. added
    stale
    on Nov 1, 2024
  11. github-actions commented on Nov 8, 2024

    @github-actions

    This issue has been automatically closed because it has not had activity in the last 37 days. If this issue is still valid, please ping a maintainer and ask them to label it as "help wanted" or "no stalebot". Thank you for your contributions.

  12. removed
    stale
    on Jun 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions