Skip to content

Commit

Permalink
[feature]: Add sampling documentation for java (#4199)
Browse files Browse the repository at this point in the history
Co-authored-by: opentelemetrybot <[email protected]>
Co-authored-by: Konstantinos Kolios <[email protected]>
Co-authored-by: Austin Parker <[email protected]>
Co-authored-by: Phillip Carter <[email protected]>
Co-authored-by: Fabrizio Ferri-Benedetti <[email protected]>
  • Loading branch information
6 people authored Apr 21, 2024
1 parent f716e1b commit 807fd51
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions content/en/docs/languages/java/sampling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
title: Sampling
weight: 80
---

[Sampling](/docs/concepts/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:

```java
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.

0 comments on commit 807fd51

Please sign in to comment.