Sampling

Sampling is a process that restricts the amount of spans that are generated by a system. Which sampler to use depends on your needs. In general, decide which sampler to use at the start of a trace and allow the sampling decision to propagate to other services.

A sampler can be set on the tracer provider using the setSampler method, as follows:

import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.samplers.Sampler;

public class Example {
  public static void main(String[] args) {
    // Configure the tracer provider with the desired sampler
    SdkTracerProvider tracerProvider = SdkTracerProvider.builder()
      .setSampler(Sampler.alwaysOn()) // Set to always sample traces
      // or
      .setSampler(Sampler.alwaysOff()) // Set to never sample traces
      // or
      .setSampler(Sampler.traceIdRatioBased(0.5)) // Set to sample a fraction of traces
      .build();
  }
}

The alwaysOn value means that every span is sampled, while alwaysOff means that no span is sampled. When you’re getting started, or in a development environment, use alwaysOn.

Other samplers include:

  • traceIdRatioBased, which samples a fraction of spans, based on the fraction given to the sampler. If you set 0.5, half of all the spans are sampled.
  • parentBased, which uses the parent span to make sampling decisions, if present. By default, the tracer provider uses a parentBased sampler with the alwaysOn sampler.

When in a production environment, consider using the parentBased sampler with the traceIdRatioBased sampler.