102 lines
2.3 KiB
Markdown
102 lines
2.3 KiB
Markdown
---
|
|
title: Sampling
|
|
weight: 80
|
|
description: Reduce the amount of telemetry created
|
|
---
|
|
|
|
[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:
|
|
|
|
{{< tabpane text=true >}} {{% tab TypeScript %}}
|
|
|
|
```ts
|
|
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 %}}
|
|
|
|
```js
|
|
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 >}}
|
|
|
|
### Browser
|
|
|
|
You can also configure the TraceIdRatioBasedSampler in code. Here's an example
|
|
for browser apps:
|
|
|
|
{{< tabpane text=true >}} {{% tab TypeScript %}}
|
|
|
|
```ts
|
|
import {
|
|
WebTracerProvider,
|
|
TraceIdRatioBasedSampler,
|
|
} from '@opentelemetry/sdk-trace-web';
|
|
|
|
const samplePercentage = 0.1;
|
|
|
|
const provider = new WebTracerProvider({
|
|
sampler: new TraceIdRatioBasedSampler(samplePercentage),
|
|
});
|
|
```
|
|
|
|
{{% /tab %}} {{% tab JavaScript %}}
|
|
|
|
```js
|
|
const {
|
|
WebTracerProvider,
|
|
TraceIdRatioBasedSampler,
|
|
} = require('@opentelemetry/sdk-trace-web');
|
|
|
|
const samplePercentage = 0.1;
|
|
|
|
const provider = new WebTracerProvider({
|
|
sampler: new TraceIdRatioBasedSampler(samplePercentage),
|
|
});
|
|
```
|
|
|
|
{{% /tab %}} {{< /tabpane >}}
|