采样
减少生成的遥测数据量
采样是限制系统生成跟踪数量的过程。JavaScript SDK 提供了几种 头部采样器。
默认行为
默认情况下,所有 Span 都被采样,因此 100% 的跟踪都被采样。如果您不需要管理数据量,则无需设置采样器。
TraceIDRatioBasedSampler
在采样时,最常用的头部采样器是 TraceIdRatioBasedSampler。它以确定性的方式采样您作为参数传入的跟踪的百分比。
环境变量
您可以通过环境变量配置 TraceIdRatioBasedSampler
export OTEL_TRACES_SAMPLER="traceidratio"
export OTEL_TRACES_SAMPLER_ARG="0.1"
这会告诉 SDK 对 Span 进行采样,以便只创建 10% 的跟踪。
Node.js
您也可以在代码中配置 TraceIdRatioBasedSampler。以下是 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),
});
浏览器
您也可以在代码中配置 TraceIdRatioBasedSampler。以下是浏览器应用的示例
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),
});