[TOC] ## 概述 原生 Jaeger 仅支持将数据持久化到 cassandra 和 elasticsearch 中 ## 启动 jaeger docker `docker run -d -p 6831:6831/udp -p 16686:16686 jaegertracing/all-in-one:latest` UI 界面为 http://127.0.0.1:16686 ## 安装 jaeger-client-go `go get github.com/uber/jaeger-client-go/...` ## 教程 ### 启动一个 trace ``` func Init(service string) (opentracing.Tracer, io.Closer) { cfg := &config.Configuration{ ServiceName: service, Sampler: &config.SamplerConfig{ Type: "const", //全部采样 Param: 1, //1 开启全部采样,0 关闭全部采样,可通过 环境变量 JAEGER_SAMPLER_PARAM 控制 }, Reporter: &config.ReporterConfig{ LogSpans: true, //LocalAgentHostPort: "127.0.0.1:6831", }, } tracer, closer, err := cfg.NewTracer( config.Logger(log.StdLogger))//log.StdLogger 只要实现日志接口即可 if err != nil { panic(fmt.Sprintf("ERROR: cannot init Jaeger: %v\n", err)) } return tracer, closer } //调用 func main(){ //初始化,只能在main 函数中初始化,因为需要 defer tracer, closer := Init("jaeger-demo11") defer closer.Close() //StartspanFromContext创建新span时会用到 opentracing.SetGlobalTracer(tracer) } ``` ### 从全局获取一个 span ``` //[推荐方案]通过 opentracing 即可全局调用 span := opentracing.StartSpan("no_context") defer span.Finish() //等同于 tracer := opentracing.GlobalTracer() parentSpan := tracer.StartSpan("parent") defer parentSpan.Finish() ``` ### 创建子跨度 ``` parentSpan := opentracing.StartSpan("parent") defer parentSpan.Finish() ... childSpan := opentracing.StartSpan("child",opentracing.ChildOf(parentSpan.Context())) defer childSpan.finish() ```