sampler.type
和sampler.param
属性选择采样类型.Jaeger库支持以下采样器:
sampler.type=const
)采样器始终对所有traces做出相同的决定。
它要么采样所有跟踪(sampler.param=1
),要么都不采样(sampler.param=0
)。
sampler.type=probabilistic
)采样器做出随机采样决策,
采样概率等于sampler.param
属性的值。例如,在sampler.param=0.1
的情况下,将在10条迹线中大约采样1条。
sampler.type=ratelimiting
)采样器使用漏斗速率限制器来确保以一定的恒定速率对轨迹进行采样。
例如,当sampler.param=2.0
时,它将以每秒2条迹线的速率对请求进行采样。
sampler.type=remote
,这也是默认值),采样器会向Jaeger代理咨询有关在当前服务中使用的适当采样策略。
1. jaeger-client-go 默认轮训通过http协议从jaeger-agent 读取采样率, 举个例子:
curl http://127.0.0.1:5778/sampling?service=jaeger4go {"strategyType":"PROBABILISTIC","probabilisticSampling":{"samplingRate":0.1}}%
代码主要在sampler_remote.go 文件
// UpdateSampler forces the sampler to fetch sampling strategy from backend server. // This function is called automatically on a timer, but can also be safely called manually, e.g. from tests. func (s *RemotelyControlledSampler) UpdateSampler() { res, err := s.samplingFetcher.Fetch(s.serviceName) if err != nil { s.metrics.SamplerQueryFailure.Inc(1) s.logger.Infof("failed to fetch sampling strategy: %v", err) return } strategy, err := s.samplingParser.Parse(res) if err != nil { s.metrics.SamplerUpdateFailure.Inc(1) s.logger.Infof("failed to parse sampling strategy response: %v", err) return } s.Lock() defer s.Unlock() s.metrics.SamplerRetrieved.Inc(1) if err := s.updateSamplerViaUpdaters(strategy); err != nil { s.metrics.SamplerUpdateFailure.Inc(1) s.logger.Infof("failed to handle sampling strategy response %+v. Got error: %v", res, err) return } s.metrics.SamplerUpdated.Inc(1) }
2. jaeger-agent 通过5778 提供http 服务 ,jaeger-client-go 每次请求jaeger-agent 的 http://ip:5778/sampling 地址,jaeger-agent 以grpc方式请求jaeger-collector
NewHTTPServer in github.com/jaegertracing/jaeger/cmd/agent/app/httpserver/srv.go //注册http sampling 地址
HTTPServerConfiguration.getHTTPServer in github.com/jaegertracing/jaeger/cmd/agent/app/builder.go //RegisterRoutes
*Builder.CreateAgent in github.com/jaegertracing/jaeger/cmd/agent/app/builder.go //getHTTPServer
func(*cobra.Command, []string) error in github.com/jaegertracing/jaeger/cmd/agent/main.go //CreateAgent
注册http sampling 地址后,每次请求本地jaeger-agent的http://127.0.0.1:5778/sampling地址,
jaeger-agent grpc请求jaeger-collector
// GetSamplingStrategy returns sampling strategies from collector. func (s *SamplingManager) GetSamplingStrategy(ctx context.Context, serviceName string) (*sampling.SamplingStrategyResponse, error) { r, err := s.client.GetSamplingStrategy(ctx, &api_v2.SamplingStrategyParameters{ServiceName: serviceName}) if err != nil { return nil, err } return jaeger.ConvertSamplingResponseFromDomain(r) }
3. jaeger-collector 可以本地通过json文件读取采样率,但是json文件更新,jaeger-collector 不会动态加载json 文件
由于 jaeger-collector 不会动态加载json 文件 ,所以通过jaeger-collector 控制jaeger-client-go 的采样率是不行的
既然jaeger-client-go 可以http 请求jaeger-agent ,那我我们也可以指定自己的配置中心
cfg := jaegercfg.Configuration{ ServiceName: servicename, Sampler: &jaegercfg.SamplerConfig{ Type: jaeger.SamplerTypeRemote, SamplingServerURL: "http://themaster:5778/sampling", }, Reporter: &jaegercfg.ReporterConfig{ LogSpans: false, BufferFlushInterval: 1 * time.Second, }, }