Sampling is a process that restricts the amount of spans that are generated by a system. The exact sampler you should use depends on your specific needs, but in general you should make a decision 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 WithSampler option, as follows:

provider := trace.NewTracerProvider(

AlwaysSample and NeverSample are self-explanatory values. AlwaysSample means that every span is sampled, while NeverSample means that no span is sampled. When you’re getting started, or in a development environment, use AlwaysSample.

Other samplers include:

  • TraceIDRatioBased, which samples a fraction of spans, based on the fraction given to the sampler. If you set .5, half of all the spans are sampled.
  • ParentBased, is a sampler decorator which behaves differently, based on the parent of the span. If the span has no parent, the decorated sampler is used to make sampling decision based on the parent of the span. By default, ParentBased samples spans that have parents that were sampled, and doesn’t sample spans whose parents were not sampled.

By default, the tracer provider uses a ParentBased sampler with the AlwaysSample sampler.

When in a production environment, consider using the ParentBased sampler with the TraceIDRatioBased sampler.

Last modified September 28, 2023: Refine Go: Sampling (#3318) (bbb4ef0c)