官方地址https://www.jaegertracing.io/

安装

https://www.jaegertracing.io/download/

All in One安装

多合一是用于快速本地测试的可执行文件,具有内存存储组件,可启动Jaeger UI,收集器,查询和代理。

docker安装

  1. $ docker run -d --name jaeger \
  2. -e COLLECTOR_ZIPKIN_HTTP_PORT=9411 \
  3. -p 5775:5775/udp \
  4. -p 6831:6831/udp \
  5. -p 6832:6832/udp \
  6. -p 5778:5778 \
  7. -p 16686:16686 \
  8. -p 14268:14268 \
  9. -p 14250:14250 \
  10. -p 9411:9411 \
  11. jaegertracing/all-in-one:1.17

UI端口是16686 例如本地http://localhost:16686/
image.png

二进制文件安装

二进制文件下载地址https://github.com/jaegertracing/jaeger/releases/
image.png

  1. jaeger-all-in-one --collector.zipkin.http-port=9411

容器需要暴露的端口

Port Protocol Component Function
5775 UDP agent accept zipkin.thrift over compact thrift protocol (deprecated, used by legacy clients only)
6831 UDP agent accept jaeger.thrift over compact thrift protocol
6832 UDP agent accept jaeger.thrift over binary thrift protocol
5778 HTTP agent serve configs
16686 HTTP query serve frontend
14268 HTTP collector accept jaeger.thrift directly from clients
14250 HTTP collector accept model.proto
9411 HTTP collector Zipkin compatible endpoint (optional)

Go客户端

Github地址:https://github.com/jaegertracing/jaeger-client-go

config设定
  1. cfg := &config.Configuration{
  2. Sampler: &config.SamplerConfig{
  3. Type: samplerType,
  4. Param: samplerParam,
  5. },
  6. Reporter: &config.ReporterConfig{
  7. LogSpans: true,
  8. },
  9. }

其中关于SamplerConfig的Type可以选择

  • const,全量采集。param采样率设置0,1 分别对应打开和关闭
  • probabilistic ,概率采集。param默认万份之一,0~1之间取值,
  • rateLimiting ,限速采集。param每秒采样的个数
  • remote 动态采集策略。param值于probabilistic的参数一样。在收到实际值之前的初始采样率。改值可以通过环境变量的JAEGER_SAMPLER_PARAM设定
    生成jaeger tracer
    1. func (c Configuration) NewTracer(options ...Option) (opentracing.Tracer, io.Closer, error)
  1. func initTraceConfig() (opentracing.Tracer, io.Closer) {
  2. cfg:= &config.Configuration{
  3. ServiceName: "hello trace",
  4. Sampler: &config.SamplerConfig{
  5. Type: jaeger.SamplerTypeConst,
  6. Param: 1,
  7. },
  8. Reporter: &config.ReporterConfig{
  9. LogSpans: true,
  10. LocalAgentHostPort: "127.0.0.1:6831",
  11. },
  12. }
  13. tracer, closer, err := cfg.NewTracer(config.Logger(jaeger.StdLogger))
  14. if err != nil {
  15. panic(fmt.Sprintf("ERROR: cannot init Jaeger: %v\n", err))
  16. }
  17. opentracing.SetGlobalTracer(tracer)
  18. return tracer, closer
  19. }

opentracing.SetGlobalTracer(t) 方法执行会将jaeger tracer注册到全局,接下来只需要使用opentracing 的标准API便可以了。 如果不想使用jaeger了,想替换成其他分布式追踪工具,只需要工具支持opentracing标准,并将main函数的SetGlobalTracer操作替换即可,其他文件都不需要更改。

设置为全局的单例tracer
  1. func SetGlobalTracer(tracer Tracer)

生成开始一个Span
  1. StartSpan(operationName string, opts ...StartSpanOption) Span

返回span的SpanContext的reference
  1. func ContextWithSpan(ctx context.Context, span Span) context.Context

生成子Span
  1. func StartSpanFromContext(ctx context.Context, operationName string, opts ...StartSpanOption) (Span, context.Context)

记录关于Span相关的key:value数据
  1. LogFields(fields ...log.Field)

到此如果只需要追踪在同一process的链路就已经可以了。如果希望能够追踪不同进程中的链路例如,客户端通过http请求服务端,服务端回应整个链路的追踪需要用到以下的处理。

使用Inject和Extract通过RPC calls传递span context

Client端