title | weight | description |
---|---|---|
Sampling |
80 |
Reduce the amount of telemetry created |
Sampling is a process that restricts the amount of traces that are generated by a system. The JavaScript SDK offers several head samplers.
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.
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.
You can configure the TraceIdRatioBasedSampler with environment variables:
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.
You can also configure the TraceIdRatioBasedSampler in code. Here's an example for Node.js:
{{< tabpane text=true >}} {{% 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 >}}
You can also configure the TraceIdRatioBasedSampler in code. Here's an example for browser apps:
{{< tabpane text=true >}} {{% 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 >}}