DEV Community

Cover image for OpenTelemetry tail sampling before Dynatrace: control trace cost and volume
Span Observer
Span Observer

Posted on Originally published at span.observer

OpenTelemetry tail sampling before Dynatrace: control trace cost and volume

Ten thousand requests per second, all traced, all exported: the ingest bill climbs and 99% of traces describe fast, successful requests nobody will ever read. OpenTelemetry tail sampling solves this in the Collector: it waits for each trace to finish, keeps the errors and slow requests, and samples only normal traffic.

Goal: by the end of this tutorial, you have a Collector applying tail sampling policies before exporting to Dynatrace, a two-tier architecture that guarantees all spans of a trace reach the same instance, and a method to check what is kept. The approach applies to Dynatrace SaaS and Dynatrace Managed, since everything happens in the Collector, before the OTLP endpoint. The DQL verification queries apply to SaaS; on Managed, verification happens in the distributed traces view.

This tutorial assumes a Collector is already running; if not, start with the OpenTelemetry Collector on Kubernetes.

The problem: too many traces, not the right ones

Head sampling, decided at the start of the request in the SDK, is blind: it drops a trace before knowing whether it will end in error. At a 10% rate, you lose 90% of your rare errors, exactly the ones on-call needs to cut MTTR. Tail sampling decides afterwards, with the full trace in view.

Prerequisites

  • The otelcol-contrib Collector distribution, or a distribution that includes the tail_sampling processor and the loadbalancing exporter. Check the component list of the Dynatrace Collector distribution in its documentation before choosing it. In recent Collector contrib versions, loadbalancing and spanmetrics are named load_balancing and span_metrics; the old names, used below, remain accepted as deprecated aliases.
  • A Dynatrace token with the openTelemetryTrace.ingest scope.
  • Applications sending 100% of their spans to the Collector (SDK sampler parentbased_always_on).

How OpenTelemetry tail sampling works

The tail_sampling processor buffers spans by trace_id. After a delay (decision_wait), it evaluates policies. If at least one policy says « keep », the whole trace is exported. Two practical consequences:

  • Collector memory grows with throughput and wait time.
  • All spans of a trace must reach the same Collector instance. With several replicas behind a standard load balancer, spans scatter and decisions become inconsistent.

Setup steps

Step 1: a first tier that routes by trace_id

The first tier (agent or routing gateway) receives OTLP and distributes spans with the loadbalancing exporter, routing on traceID:

# collector-routing.yaml
receivers:
  otlp:
    protocols:
      grpc:
        endpoint: 0.0.0.0:4317
      http:
        endpoint: 0.0.0.0:4318

exporters:
  loadbalancing:
    routing_key: traceID
    protocol:
      otlp:
        tls:
          insecure: true
    resolver:
      dns:
        hostname: otel-sampler-headless.observability.svc.cluster.local
        port: 4317

service:
  pipelines:
    traces:
      receivers: [otlp]
      exporters: [loadbalancing]
Enter fullscreen mode Exit fullscreen mode

Critical line: routing_key: traceID. The DNS resolver points to a headless Kubernetes service so the exporter sees every second-tier pod.

Step 2: a second tier that samples

# collector-sampler.yaml
receivers:
  otlp:
    protocols:
      grpc:
        endpoint: 0.0.0.0:4317

processors:
  memory_limiter:
    check_interval: 1s
    limit_percentage: 80
    spike_limit_percentage: 20
  tail_sampling:
    decision_wait: 10s
    num_traces: 100000
    expected_new_traces_per_sec: 2000
    policies:
      - name: errors
        type: status_code
        status_code:
          status_codes: [ERROR]
      - name: slow
        type: latency
        latency:
          threshold_ms: 1000
      - name: payment-always
        type: string_attribute
        string_attribute:
          key: service.name
          values: [payment-api]
      - name: baseline
        type: probabilistic
        probabilistic:
          sampling_percentage: 5
  batch: {}

exporters:
  otlphttp/dynatrace:
    endpoint: "https://<environment-id>.live.dynatrace.com/api/v2/otlp"
    headers:
      Authorization: "Api-Token ${env:DT_API_TOKEN}"

service:
  pipelines:
    traces:
      receivers: [otlp]
      processors: [memory_limiter, tail_sampling, batch]
      exporters: [otlphttp/dynatrace]
Enter fullscreen mode Exit fullscreen mode

Reading the policies: they are combined with OR. A trace is kept if it contains an error, or lasts over one second, or touches payment-api, or falls into the random 5%. On Managed, replace the endpoint with https://<your-domain>/e/<environment-id>/api/v2/otlp.

Critical settings:

  • decision_wait must exceed the duration of your longest traces; otherwise late spans arrive after the decision and form incomplete traces.
  • num_traces caps the number of traces held in memory. Size it from throughput × decision_wait, with headroom.
  • memory_limiter comes first: it protects the Collector from traffic spikes.

Step 3: preserve metrics before sampling

After sampling, counting spans in Dynatrace underestimates real traffic: your throughput and error-rate SLIs become wrong. Compute RED metrics before sampling, in the first tier, with the spanmetrics connector, in delta temporality:

connectors:
  spanmetrics:
    aggregation_temporality: "AGGREGATION_TEMPORALITY_DELTA"
    histogram:
      explicit:
        buckets: [50ms, 100ms, 250ms, 500ms, 1s, 2s]

service:
  pipelines:
    traces:
      receivers: [otlp]
      exporters: [loadbalancing, spanmetrics]
    metrics:
      receivers: [spanmetrics]
      exporters: [otlphttp/dynatrace]
Enter fullscreen mode Exit fullscreen mode

Add the otlphttp/dynatrace definition shown above to this Collector. Delta temporality is mandatory for Dynatrace (see OTLP metrics temporality). Check in the Dynatrace documentation how the platform handles sampled spans for its own service statistics: do not assume automatic extrapolation.

Step 4: deploy and observe the Collector itself

Deploy the second tier as a StatefulSet or Deployment with a headless service, at least two replicas, and memory limits aligned with memory_limiter. Expose the Collector’s internal telemetry to track its behaviour.

How to check that it works

  1. Collector internal metrics: track the processor counters, for example otelcol_processor_tail_sampling_count_traces_sampled per policy and decision. Exact names vary by version: list them on the internal metrics endpoint.
  2. Guaranteed errors: trigger an error (an endpoint returning 500) and check that its trace arrives complete.
  3. Dynatrace SaaS: compare volume before and after, and the share of failed traces:

    fetch spans, from: now() - 1h
    | filter isNull(span.parent_id)
    | summarize traces = count(), errors = countIf(span.status_code == "error"),
        by: {service.name}
    

    The error share should be clearly higher than before: that is the intended effect.

  4. Dynatrace Managed: in the distributed traces view, filter on failed requests and check that traces are not truncated.

  5. Incomplete traces: if traces arrive without a root span, increase decision_wait or check the traceID routing.

Best practices

  • Keep 100% of errors and slow requests; sample only healthy traffic.
  • Do not combine aggressive head sampling with tail sampling: what the SDK drops never comes back.
  • Compute SLIs from unsampled metrics, then alert on burn rate (see SLO burn rate alerts).
  • If OneAgent also instruments some services, avoid duplicates: see OneAgent and OpenTelemetry duplicate spans.
  • Review policies after each incident, blamelessly: a missing trace is a configuration gap, not a team failure.

Frequently asked questions

Can tail sampling run on a single Collector?

Yes, if a single instance receives all spans. As soon as you run several replicas, routing by traceID becomes mandatory.

What value should I pick for decision_wait?

Slightly more than your p99 trace duration. Ten seconds suits many synchronous APIs; long asynchronous workflows need more, at the cost of memory.

Are logs and metrics sampled too?

No, the processor only handles traces. Logs correlated with a dropped trace are still ingested but point to a missing trace.

Conclusion

Tail sampling turns an expensive trace stream into a useful sample: every error, every slow request, a little normal traffic. The price is a two-tier architecture and memory. Start with simple policies, measure with the Collector’s internal metrics, then refine. Reference: the tail_sampling processor documentation and the Dynatrace OpenTelemetry documentation.

span.observer is an independent community blog, not affiliated with Dynatrace LLC. Dynatrace is a trademark of its owner.


Originally published on span.observer. French version: https://span.observer/fr/tail-sampling-opentelemetry-dynatrace/.

Top comments (0)