Sampling

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.

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:

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:

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),
});
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),
});

Browser

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

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

const samplePercentage = 0.1;

const provider = new WebTracerProvider({
  sampler: new TraceIdRatioBasedSampler(samplePercentage),
});
const {
  WebTracerProvider,
  TraceIdRatioBasedSampler,
} = require('@opentelemetry/sdk-trace-web');

const samplePercentage = 0.1;

const provider = new WebTracerProvider({
  sampler: new TraceIdRatioBasedSampler(samplePercentage),
});