企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## 以 span 追踪 <details> <summary>详情</summary> ``` package main import ( "fmt" "github.com/opentracing/opentracing-go" "github.com/uber/jaeger-client-go/config" "github.com/uber/jaeger-client-go/log" "io" "time" ) 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) parentSpan := opentracing.StartSpan("parent") defer parentSpan.Finish() time.Sleep(10*time.Millisecond) //创建 child span childSpan(parentSpan) } func childSpan(span opentracing.Span) { childSpan := opentracing.StartSpan( "no_context_child_1", opentracing.ChildOf(span.Context()), ) defer childSpan.Finish() time.Sleep(10*time.Millisecond) childspan2(childSpan) } func childspan2(span opentracing.Span) { childSpan := opentracing.StartSpan( "no_context_child_2", opentracing.ChildOf(span.Context()), ) defer childSpan.Finish() time.Sleep(10*time.Millisecond) } ``` </details> <br /> ![B1D169E8-E86D-421F-A9C8-882F6B1D04D8.png](http://yanxuan.nosdn.127.net/b22627a3e4defaabc8c882d9f79d11fd.png)