-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature]: Add sampling documentation for java (#4199)
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
1 parent
f716e1b
commit 807fd51
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |