opentelemetry.io/content/en/docs/languages/go/sampling.md

46 lines
1.9 KiB
Markdown

---
title: Sampling
weight: 80
---
[Sampling](/docs/concepts/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`](https://pkg.go.dev/go.opentelemetry.io/otel/sdk/trace#Sampler) can
be set on the tracer provider using the
[`WithSampler`](https://pkg.go.dev/go.opentelemetry.io/otel/sdk/trace#WithSampler)
option, as follows:
```go
provider := trace.NewTracerProvider(
trace.WithSampler(trace.AlwaysSample()),
)
```
[`AlwaysSample`](https://pkg.go.dev/go.opentelemetry.io/otel/sdk/trace#AlwaysSample)
and
[`NeverSample`](https://pkg.go.dev/go.opentelemetry.io/otel/sdk/trace#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`](https://pkg.go.dev/go.opentelemetry.io/otel/sdk/trace#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`](https://pkg.go.dev/go.opentelemetry.io/otel/sdk/trace#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.