trace 数据接入

监控 Agent 已经集成了 jaeger agent 的功能,你可以在程序中直接通过监控 Agent 上报 trace 数据, 开只需简单修改配置. 服务开启后, Agent 会在本机的localhost上监听UDP端口6831

localhost:6831

启用 Agent trace 功能

监控服务器中安装监控 agent并开启 trace 功能, 修改配置文件 /usr/local/monitor-agent/conf/monitor-agent.conf:

[trace]
        # 开启trace功能
        enable = true
		# 配置 jaeger colletor 地址
        collector = [ "collector.trace.xxx.com:14267" ]

关于 jaeger 相关资料可以参考: https://www.jaegertracing.io

抽样策略 (Sampling)

对于一个并发量比较高的业务,一个合适的采样策略显得尤为重要。我们不建议全量采样(const),因为这样可能会对本机造成一定的负载和性能消耗。

jaeger 有4中采样策略可供选择,建议在 2、3 中进行选择。

  • Constant (sampler.type=const) sampler always makes the same decision for all traces. It either samples all traces (sampler.param=1) or none of them (sampler.param=0).
  • Probabilistic (sampler.type=probabilistic) sampler makes a random sampling decision with the probability of sampling equal to the value of sampler.param property. For example, with sampler.param=0.1 approximately 1 in 10 traces will be sampled.
  • Rate Limiting (sampler.type=ratelimiting) sampler uses a leaky bucket rate limiter to ensure that traces are sampled with a certain constant rate. For example, when sampler.param=2.0 it will sample requests with the rate of 2 traces per second.
  • Remote (sampler.type=remote, which is also the default) sampler consults Jaeger agent for the appropriate sampling strategy to use in the current service. This allows controlling the sampling strategies in the services from a central configuration in Jaeger backend, or even dynamically (see Adaptive Sampling).

程序上报 trace 数据例子

package main

import (
        "time"

        opentracing "github.com/opentracing/opentracing-go"
        "github.com/opentracing/opentracing-go/log"
        "github.com/openzipkin/zipkin-go-opentracing/thrift/gen-go/zipkincore"

        "github.com/uber/jaeger-client-go"
        jaegerClientConfig "github.com/uber/jaeger-client-go/config"
)

func main() {
		cfg := jaegerClientConfig.Configuration{
				Sampler: &jaegerClientConfig.SamplerConfig{
						Type:  "const",
						Param: 1,
				},
				Reporter: &jaegerClientConfig.ReporterConfig{
						LogSpans:            true,
						BufferFlushInterval: 1 * time.Second,
						LocalAgentHostPort:  "localhost:6831",
				},
		}
		tracer, closer, _ := cfg.New(
				"bench-demo",
				jaegerClientConfig.Logger(jaeger.StdLogger),
		)

		tracer2(testTracer1(tracer)) // working
		// testTracer()
		closer.Close()

        select {}

}