Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update java sampling documentation page #4736

Merged
merged 5 commits into from
Jul 25, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 54 additions & 3 deletions content/en/docs/languages/java/sampling.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,57 @@ 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.

## Default behavior

By default, all spans are sampled, and thus, 100% of traces are sampled. If you
do not need to manage data volume, don't bother setting a sampler.
jaydeluca marked this conversation as resolved.
Show resolved Hide resolved

## Environment variables

You can configure the sampler with environment variables or system properties.
Reference the [configuration](/docs/languages/java/configuration/) documentation
for the available options.

For example, to configure the SDK to sample spans such that only 10% of traces
get created:

```shell
export OTEL_TRACES_SAMPLER="traceidratio"
export OTEL_TRACES_SAMPLER_ARG="0.1"
```

## Samplers

### ParentBasedSampler

When sampling, the ParentBasedSampler is most often used for
[head sampling](/docs/concepts/sampling/#head-sampling). It uses the sampling
decision of the Span’s parent, or the fact that there is no parent, to know
which secondary sampler to use.

If the parent span is sampled, the child span will also be sampled. Conversely,
if the parent span is not sampled, the child span will not be sampled either.
This ensures consistency in sampling decisions within a trace.

### TraceIDRatioBasedSampler

The TraceIdRatioBasedSampler deterministically samples a percentage of traces
that you pass in as a parameter.

This sampler is useful when you want to control the overall sampling rate across
all traces, regardless of their parent spans. It provides a consistent rate of
sampling for all traces initiated.

## Configuration in code

{{% alert title="Note" %}} The use of the
[Java agent](/docs/zero-code/java/agent/),
[Spring Boot Starter](/docs/zero-code/java/spring-boot-starter/), or
[SDK autoconfigure](/docs/languages/java/instrumentation/#autoconfiguration) is
generally recommended for controlling sampling, rather than setting it directly
in the code. Most users should find the default settings sufficient for their
needs. {{% /alert %}}

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

Expand Down Expand Up @@ -37,9 +88,9 @@ 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.
Currently, only the ratio of traces that are sampled can be relied on, not how
the sampled traces are determined. As such, it is recommended to only use this
sampler for root spans using `parentBased`.
jaydeluca marked this conversation as resolved.
Show resolved Hide resolved
- `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.