nohup ./jaeger-agent --reporter.grpc.host-port={{collectorRPCHostPort}} --agent.tags=token={{token}}
opentracing-contrib/go-gin
依赖。github.com/opentracing-contrib/go-gin
v0.0.0-20201220185307-1dd2273433a4
cfg := &jaegerConfig.Configuration{ServiceName: ginServerName, //对其发起请求的的调用链,叫什么服务Sampler: &jaegerConfig.SamplerConfig{ //采样策略的配置,详情见4.1.1Type: "const",Param: 1,},Reporter: &jaegerConfig.ReporterConfig{ //配置客户端如何上报trace信息,所有字段都是可选的LogSpans: true,LocalAgentHostPort: endPoint,},//Token配置Tags: []opentracing.Tag{ //设置tag,token等信息可存于此opentracing.Tag{Key: "token", Value: token}, //设置token},}tracer, closer, err := cfg.NewTracer(jaegerConfig.Logger(jaeger.StdLogger)) //根据配置得到tracer
r := gin.Default()//传入tracerr.Use(ginhttp.Middleware(tracer))
HTTP + HttpMethod + URL
可以分析到具体接口,接口主要 URL 应是参数名,不是具体参数值。 具体用法如下:
正确:/user/{id}
, 错误:/user/1
r.Use(ginhttp.Middleware(tracer, ginhttp.OperationNameFunc(func(r *http.Request) string {return fmt.Sprintf("testtestheling HTTP %s %s", r.Method, r.URL.String())})))
// Copyright © 2019-2020 Tencent Co., Ltd.// This file is part of tencent project.// Do not copy, cite, or distribute without the express// permission from Cloud Monitor group.package gindemoimport ("fmt""github.com/gin-gonic/gin""github.com/opentracing-contrib/go-gin/ginhttp""github.com/opentracing/opentracing-go""github.com/uber/jaeger-client-go"jaegerConfig "github.com/uber/jaeger-client-go/config""net/http")// 服务名 服务唯一标示,服务指标聚合过滤依据。const ginServerName = "demo-gin-server"// StartServerfunc StartServer() {//初始化jaeger,得到tracercfg := &jaegerConfig.Configuration{ServiceName: ginServerName, //对其发起请求的的调用链,叫什么服务Sampler: &jaegerConfig.SamplerConfig{ //采样策略的配置,详情见4.1.1Type: "const",Param: 1,},Reporter: &jaegerConfig.ReporterConfig{ //配置客户端如何上报trace信息,所有字段都是可选的LogSpans: true,LocalAgentHostPort: endPoint,},//Token配置Tags: []opentracing.Tag{ //设置tag,token等信息可存于此opentracing.Tag{Key: "token", Value: token}, //设置token},}tracer, closer, err := cfg.NewTracer(jaegerConfig.Logger(jaeger.StdLogger)) //根据配置得到tracerif err != nil {panic(fmt.Sprintf("ERROR: fail init Jaeger: %v\\n", err))}defer closer.Close()r := gin.Default()//这里说明一下,官方默认 OperationName 是 HTTP + HttpMethod,//建议使用 HTTP + HttpMethod + URL 可以分析到具体接口,具体用法如下//PS:Restful 接口主要URL应该是参数名,不是具体参数值。 如: 正确:/user/{id}, 错误:/user/1r.Use(ginhttp.Middleware(tracer, ginhttp.OperationNameFunc(func(r *http.Request) string {return fmt.Sprintf("HTTP %s %s", r.Method, r.URL.String())})))r.GET("/ping", func(c *gin.Context) {c.JSON(200, gin.H{"message": "pong",})})r.Run() // 监听 0.0.0.0:8080}
opentracing-contrib/go-stdlib/nethttp
依赖。github.com/opentracing-contrib/go-stdlib/nethttp
v1.0.0
cfg := &jaegerConfig.Configuration{ServiceName: ginClientName, //对其发起请求的的调用链,叫什么服务Sampler: &jaegerConfig.SamplerConfig{ //采样策略的配置,详情见4.1.1Type: "const",Param: 1,},Reporter: &jaegerConfig.ReporterConfig{ //配置客户端如何上报trace信息,所有字段都是可选的LogSpans: true,LocalAgentHostPort: endPoint,},//Token配置Tags: []opentracing.Tag{ //设置tag,token等信息可存于此opentracing.Tag{Key: "token", Value: token}, //设置token},}tracer, closer, err := cfg.NewTracer(jaegerConfig.Logger(jaeger.StdLogger)) //根据配置得到tracer
span := tracer.StartSpan("CallDemoServer") //构建spanctx := opentracing.ContextWithSpan(context.Background(), span) //将span的引用放入conext中
//构建http的请求req, err := http.NewRequest(http.MethodGet,fmt.Sprintf("http://localhost%s/ping", ginPort),nil,)req = req.WithContext(ctx)//构建带tracer的请求req, ht := nethttp.TraceRequest(tracer, req)
httpClient := &http.Client{Transport: &nethttp.Transport{}} //初始化http客户端res, err := httpClient.Do(req)//..省略err判断body, err := ioutil.ReadAll(res.Body)//..省略err判断log.Printf(" %s recevice: %s\\n", clientServerName, string(body))
// Copyright © 2019-2020 Tencent Co., Ltd.// This file is part of tencent project.// Do not copy, cite, or distribute without the express// permission from Cloud Monitor group.package gindemoimport ("context""fmt""github.com/opentracing-contrib/go-stdlib/nethttp""github.com/opentracing/opentracing-go""github.com/opentracing/opentracing-go/ext"opentracingLog "github.com/opentracing/opentracing-go/log""github.com/uber/jaeger-client-go"jaegerConfig "github.com/uber/jaeger-client-go/config""io/ioutil""log""net/http")const (// 服务名 服务唯一标示,服务指标聚合过滤依据。ginClientName = "demo-gin-client"ginPort = ":8080"endPoint = "xxxxx:6831" // 本地agent地址token = "abc")// StartClient gin client 也是标准的 http client.func StartClient() {cfg := &jaegerConfig.Configuration{ServiceName: ginClientName, //对其发起请求的的调用链,叫什么服务Sampler: &jaegerConfig.SamplerConfig{ //采样策略的配置,详情见4.1.1Type: "const",Param: 1,},Reporter: &jaegerConfig.ReporterConfig{ //配置客户端如何上报trace信息,所有字段都是可选的LogSpans: true,LocalAgentHostPort: endPoint,},//Token配置Tags: []opentracing.Tag{ //设置tag,token等信息可存于此opentracing.Tag{Key: "token", Value: token}, //设置token},}tracer, closer, err := cfg.NewTracer(jaegerConfig.Logger(jaeger.StdLogger)) //根据配置得到tracerdefer closer.Close()if err != nil {panic(fmt.Sprintf("ERROR: fail init Jaeger: %v\\n", err))}//构建span,并将span放入context中span := tracer.StartSpan("CallDemoServer")ctx := opentracing.ContextWithSpan(context.Background(), span)defer span.Finish()// 构建http请求req, err := http.NewRequest(http.MethodGet,fmt.Sprintf("http://localhost%s/ping", ginPort),nil,)if err != nil {HandlerError(span, err)return}// 构建带tracer的请求req = req.WithContext(ctx)req, ht := nethttp.TraceRequest(tracer, req)defer ht.Finish()// 初始化http客户端httpClient := &http.Client{Transport: &nethttp.Transport{}}// 发起请求res, err := httpClient.Do(req)if err != nil {HandlerError(span, err)return}defer res.Body.Close()body, err := ioutil.ReadAll(res.Body)if err != nil {HandlerError(span, err)return}log.Printf(" %s recevice: %s\\n", ginClientName, string(body))}// HandlerError handle error to span.func HandlerError(span opentracing.Span, err error) {span.SetTag(string(ext.Error), true)span.LogKV(opentracingLog.Error(err))}
本页内容是否解决了您的问题?