nohup ./jaeger-agent --reporter.grpc.host-port={{接入点}} --agent.tags=token={{token}}
--agent.tags
替换为 --jaeger.tags
。opentracing-contrib/go-stdlib/nethttp
依赖。github.com/opentracing-contrib/go-stdlib/nethttp
dv1.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))}
本页内容是否解决了您的问题?