Note: This is an experimental package under active development. New releases may include breaking changes.
This package provides implementations of composite samplers that propagate sampling information across a trace. These samplers provide the implementation for (the experimental) Consistent Probability Sampling specification.
To get started you will need to install a compatible OpenTelemetry SDK.
This module exports samplers that follow the general behavior of the standard SDK samplers, but ensuring it is consistent across a trace by using the tracestate header. Notably, the tracestate can be examined in exported spans to reconstruct population metrics.
import {
createCompositeSampler,
createComposableAlwaysOffSampler,
createComposableAlwaysOnSampler,
createComposableParentThresholdSampler,
createComposableTraceIDRatioBasedSampler,
createComposableRuleBasedSampler
} from '@opentelemetry/sampler-composite';
// never sample
const sampler = createCompositeSampler(createComposableAlwaysOffSampler());
// always sample
const sampler = createCompositeSampler(createComposableAlwaysOnSampler());
// follow the parent, or otherwise sample with a probability if root
const sampler = createCompositeSampler(
createComposableParentThresholdSampler(createComposableTraceIDRatioBasedSampler(0.3)));
// An example of a rule-based sampler implementing the example at
// https://opentelemetry.io/docs/specs/otel/trace/sdk/#composablerulebased
const isHealthCheck = (_ctx, _traceId, _name, _kind, attrs, _links) => {
return attrs['http.route'] === '/healthcheck';
};
const isCheckout = (_ctx, _traceId, _name, _kind, attrs, _links) => {
return attrs['http.route'] === '/checkout';
};
const sampler = createCompositeSampler(
createComposableParentThresholdSampler( // Honour sampling flag in `traceparent` header.
// Otherwise...
createComposableRuleBasedSampler([
// ...never sample `/healthcheck` requests.
[isHealthCheck, createComposableAlwaysOffSampler()],
// ...always sample `/checkout` requests.
[isCheckout, createComposableAlwaysOnSampler()],
// ...sample 10% of all other requests.
[() => true, createComposableTraceIDRatioBasedSampler(0.1)]
])
)
);
Apache 2.0 - See LICENSE for more information.