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

Add sampling doc for JS #2482

Merged
merged 10 commits into from
Mar 13, 2023
94 changes: 94 additions & 0 deletions content/en/docs/instrumentation/js/sampling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
title: Sampling
weight: 9
---

[Sampling](/docs/concepts/sampling/) is a process that restricts the amount of
traces that are generated by a system. The JavaScript SDK offers several
[head samplers](/docs/concepts/sampling#head-sampling).

## 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.

## TraceIDRatioBasedSampler

When sampling, the most common head sampler to use is the
TraceIdRatioBasedSampler. It deterministically samples a percentage of traces
that you pass in as a parameter.

### Environment Variables

You can configure the TraceIdRatioBasedSampler with environment variables:

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

This tells the SDK to sample spans such that only 10% of traces get created.

### Node.js

You can also configure the TraceIdRatioBasedSampler in code. Here's an example
for Node.js:

<!-- prettier-ignore-start -->
{{< tabpane lang=shell persistLang=false >}}

{{< tab TypeScript >}}
import { TraceIdRatioBasedSampler } from '@opentelemetry/sdk-trace-node';

const samplePercentage = 0.1;

const sdk = new NodeSDK({
// Other SDK configuration parameters go here
sampler: new TraceIdRatioBasedSampler(samplePercentage),
});
{{< /tab >}}

{{< tab JavaScript >}}
const { TraceIdRatioBasedSampler } = require('@opentelemetry/sdk-trace-node');

const samplePercentage = 0.1;

const sdk = new NodeSDK({
// Other SDK configuration parameters go here
sampler: new TraceIdRatioBasedSampler(samplePercentage),
});
{{< /tab >}}

{{< /tabpane >}}
<!-- prettier-ignore-end -->

### Browser

You can also configure the TraceIdRatioBasedSampler in code. Here's an example
for browser apps:

<!-- prettier-ignore-start -->
{{< tabpane lang=shell persistLang=false >}}

{{< tab TypeScript >}}
import { WebTracerProvider, TraceIdRatioBasedSampler } from '@opentelemetry/sdk-trace-web';

const samplePercentage = 0.1;

const provider = new WebTracerProvider({
sampler: new TraceIdRatioBasedSampler(samplePercentage),
});
{{< /tab >}}

{{< tab JavaScript >}}
const { WebTracerProvider, TraceIdRatioBasedSampler } = require('@opentelemetry/sdk-trace-web');

const samplePercentage = 0.1;

const provider = new WebTracerProvider({
sampler: new TraceIdRatioBasedSampler(samplePercentage),
});
{{< /tab >}}

{{< /tabpane >}}
<!-- prettier-ignore-end -->