ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ## http 形式访问 <details> <summary>client.go</summary> ``` package main import ( "fmt" "github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go/ext" "github.com/uber/jaeger-client-go/config" "github.com/uber/jaeger-client-go/log" "io" "net/http" ) 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) clientSpan := opentracing.StartSpan("parent2") defer clientSpan.Finish() url := "http://localhost:8082/publish" req, _ := http.NewRequest("GET", url, nil) ext.HTTPUrl.Set(clientSpan, url) ext.HTTPMethod.Set(clientSpan, "GET") // Inject the client span context into the headers tracer.Inject(clientSpan.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header)) resp, _ := http.DefaultClient.Do(req) fmt.Printf("%+v\n", resp.Header) } ``` </details> <br /> <details> <summary>server.go</summary> ``` package main import ( "github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go/ext" "log" "net/http" ) func main() { tracer := opentracing.GlobalTracer() http.HandleFunc("/publish", func(w http.ResponseWriter, r *http.Request) { // 从标题中提取上下文 spanCtx, _ := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(r.Header)) serverSpan := tracer.StartSpan("server", ext.RPCServerOption(spanCtx)) defer serverSpan.Finish() }) log.Fatal(http.ListenAndServe(":8082", nil)) } ``` </details> <br /> ![7162E9CB-1465-4E0B-ABC7-7880A24F8157.png](http://yanxuan.nosdn.127.net/0b8614fa904d3ae7e5060323a70856ee.png)