Skip to content

xsampling

omeyang edited this page Sep 17, 2026 · 1 revision

pkg/observability/xsampling

稳定性:Stable · 覆盖率:97.9% · 源码:pkg/observability/xsampling

采样策略。公开 API 已冻结(Stable)。

用途

  • 高吞吐服务限制 trace/log 采样率
  • 错误请求强制全采,正常请求按比例采

快速上手

rate, _ := xsampling.NewRateSampler(0.1)  // 10% 采样
count, _ := xsampling.NewCountSampler(100) // 每 100 次采一次
combined, _ := xsampling.Any(rate, count)  // OR 组合
_ = combined.ShouldSample(ctx)

关键类型与函数

名称 说明
Sampler interface ShouldSample(ctx context.Context) bool
Always() / Never() 全采 / 全不采
NewRateSampler / NewCountSampler / NewKeyBasedSampler 基础策略,均返回 (T, error)
All / Any / NewCompositeSampler 组合策略(ModeAND / ModeOR),均返回 (*CompositeSampler, error)

设计要点

  • 策略组合固定:CompositeSampler 按传入顺序短路求值,任一子策略返回 true 即采样
  • 可拼接:基础策略 + 组合 sampler

相关

Clone this wiki locally