Menu
Datadog Blog·August 21, 2026

Optimizing Trace Volume with OpenTelemetry Tail-Based Sampling

This article explores tail-based sampling in OpenTelemetry, a crucial technique for managing observability costs and data volume in distributed systems. It details how to configure the OpenTelemetry Collector to intelligently filter traces based on their attributes after they've been collected, ensuring that important traces (e.g., errors, slow requests) are retained while noisy or less critical ones are dropped. This approach is vital for maintaining performance visibility without incurring excessive APM expenses.

Read original on Datadog Blog

In distributed systems, observability data, particularly traces, can generate an enormous volume of information. While comprehensive tracing is beneficial for debugging and performance analysis, the sheer volume can lead to significant storage and processing costs. Sampling is a fundamental strategy to mitigate this, but its implementation profoundly impacts the effectiveness of your monitoring.

Head-Based vs. Tail-Based Sampling

Traditionally, head-based sampling decides whether to sample a trace at its origin (the first service it touches). While simple, this approach has a significant drawback: you don't know the full context or outcome of the transaction until it's complete. A trace might be sampled at the head, only to reveal later that it was a critical error or a very slow request, or conversely, a mundane successful request might be kept unnecessarily.

ℹ️

The Challenge of Distributed Tracing Costs

The distributed nature of modern applications means a single request can span dozens of services. Without intelligent sampling, collecting every span for every request quickly becomes cost-prohibitive. Tail-based sampling offers a more nuanced solution by making decisions based on the complete trace.

Implementing Tail-Based Sampling with OpenTelemetry Collector

Tail-based sampling addresses the limitations of head-based sampling by deferring the sampling decision until the entire trace is collected. This allows for more intelligent filtering based on attributes of the completed trace, such as error status, latency, or specific service tags. The OpenTelemetry Collector provides processors to enable this, acting as an intermediary to gather spans, reconstruct traces, and then apply sampling rules before exporting.

yaml
receivers:
  otlp:
    protocols:
      grpc:
      http:
processors:
  tail_sampling:
    decision_wait: 10s
    num_traces: 100000
    expected_new_traces_per_sec: 1000
    policies:
      - name: error-policy
        type: status_code
        status_code:
          status_codes: [ERROR, UNSET]
      - name: high-latency-policy
        type: latency
        latency:
          threshold_ms: 500
      - name: composite-policy
        type: composite
        composite:
          max_number_of_traces: 1000
          selector_type: or
          sub_policy_type: and
          sub_policies:
            - type: tag
              tag:
                key: 'service.name'
                value: 'critical-service'
            - type: probabilistic
              probabilistic:
                sampling_percentage: 1
          default_policy:
            type: always_sample
exporters:
  otlp:
    endpoint: "your-apm-provider:4317"
service:
  pipelines:
    traces:
      receivers: [otlp]
      processors: [tail_sampling]
      exporters: [otlp]

This configuration snippet illustrates a `tail_sampling` processor in the OpenTelemetry Collector. It defines multiple policies: an `error-policy` to always sample traces with errors, a `high-latency-policy` to sample slow traces, and a `composite-policy` for more complex scenarios, such as sampling a percentage of traces from a 'critical-service'. These policies allow for granular control over which traces are considered important and should be retained for analysis, striking a balance between observability and cost efficiency.

observabilitytracingOpenTelemetrysamplingAPMdistributed tracingcost optimizationmonitoring

Comments

Loading comments...