在上一篇文章中,我在 Grafana Cloud 中拍摄、保存和查看了 Opentelemetry 数据
。如果您使用免费版本的 Grafana Cloud,您每月可以获得大约 50GB 的日志和跟踪。如果是因为用户不多,所以不会积累太多trace(或者不记录日志)的服务,直接使用即可,但如果小规模引入的话,恐怕会积累太多日志并爆炸。
采样为什么需要抽样
上图中的所有圆圈(轨迹)无需保存。仅存储重要的跟踪(错误或太长的执行时间)和一些代表整体的样本(一些正常的跟踪)就足够了。
抽样类型
头部采样
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), });
SamplingSpanProcessor 实现
import { Context } from "@opentelemetry/api"; import { SpanProcessor, ReadableSpan, Span, } from "@opentelemetry/sdk-trace-node"; /** * Sampling span processor (including all error span and ratio of other spans) */ export class SamplingSpanProcessor implements SpanProcessor { constructor( private _spanProcessor: SpanProcessor, private _ratio: number ) {} /** * Forces to export all finished spans */ forceFlush(): Promise<void> { return this._spanProcessor.forceFlush(); } onStart(span: Span, parentContext: Context): void { this._spanProcessor.onStart(span, parentContext); } shouldSample(traceId: string): boolean { let accumulation = 0; for (let idx = 0; idx < traceId.length; idx++) { accumulation += traceId.charCodeAt(idx); } const cmp = (accumulation % 100) / 100; return cmp < this._ratio; } /** * Called when a {@link ReadableSpan} is ended, if the `span.isRecording()` * returns true. * @param span the Span that just ended. */ onEnd(span: ReadableSpan): void { // Only process spans that have an error status if (span.status.code === 2) { // Status code 0 means "UNSET", 1 means "OK", and 2 means "ERROR" this._spanProcessor.onEnd(span); } else { if (this.shouldSample(span.spanContext().traceId)) { this._spanProcessor.onEnd(span); } } } /** * Shuts down the processor. Called when SDK is shut down. This is an * opportunity for processor to do any cleanup required. */ async shutdown(): Promise<void> { return this._spanProcessor.shutdown(); } }导出
OtelSDK更新
spanProcessors: [ new SamplingSpanProcessor( new BatchSpanProcessor(traceExporter), samplePercentage ), ],
以上是NestJS + Opentelemetry(采样)的详细内容。更多信息请关注PHP中文网其他相关文章!