OpenTelemetry个性化采样-根据特定Header决定是否采样
版权声明 本站原创文章 由 萌叔 发表 转载请注明 萌叔 | http://vearne.cc 建议阅读,萌叔2024年的文章 OpenTelemetry原理及实战 1.前言 统一术语: 采样: sample,在OpenTelemetry的语境下,是指收集tracing(包含span)信息,并上报 对于订单类的服务而言,由于服务特别重要,QPS不高,全量采样问题不大。 但是对于内容型的服务,比如微博、抖音服务,这类读多写少的服务,全量采样成本巨大, 不仅不现实,也没有必要。此时当然可以考虑按照百分比采样, 另一种更为保守的做法,只收集异常的请求(客户端收集,指标告警,客服报障),进行流量重放, 并同时采集tracing信息。 2.根据特定Header决定是否采样 这时候我们需要特定的标识表明这个请求需要强制采样。 以HTTP请求为例,可以给定特殊的Header X-Force-Trace: 1 2.1 上游服务 OpenTelemetry的SDK预留了Sampler接口,用于定义个性化的采样需求(是否采样) // Sampler decides whether a trace should be sampled and exported. type Sampler interface { // DO NOT CHANGE: any modification will not be backwards compatible and // must never be done outside of a new major release. // ShouldSample returns a SamplingResult based on a decision made from the // passed parameters. ShouldSample(parameters SamplingParameters) SamplingResult // DO NOT CHANGE: any modification will not be backwards compatible and // must never be done outside of a new major release. // Description returns information describing the Sampler. Description() string // DO NOT CHANGE: any modification will not be backwards compatible and // must never be done outside of a new major release. } 完整代码见 vearne/otel-test ...