nohup ./jaeger-agent --reporter.grpc.host-port={{endpoint}} --agent.tags=token={{token}}
--agent.tags
in the startup command with --jaeger.tags
.opentracing-contrib/go-stdlib/nethttp
dependency.github.com/opentracing-contrib/go-stdlib/nethttp
dv1.0.0
cfg := &jaegerConfig.Configuration{ServiceName: ginClientName, // Call trace of the target service. Enter the service name.Sampler: &jaegerConfig.SamplerConfig{ // Sampling policy configuration. See section 4.1.1 for details.Type: "const",Param: 1,},Reporter: &jaegerConfig.ReporterConfig{ // Configure how the client reports trace information. All fields are optional.LogSpans: true,LocalAgentHostPort: endPoint,},// Token configurationTags: []opentracing.Tag{ // Set the tag, where information such as token can be stored.opentracing.Tag{Key: "token", Value: token}, // Set the token},}tracer, closer, err := cfg.NewTracer(jaegerConfig.Logger(jaeger.StdLogger)) // Get the tracer based on the configuration
span := tracer.StartSpan("CallDemoServer") // Construct a spanctx := opentracing.ContextWithSpan(context.Background(), span) // Put the span reference in the context
// Construct an HTTP requestreq, err := http.NewRequest(http.MethodGet,fmt.Sprintf("http://localhost%s/ping", ginPort),nil,)req = req.WithContext(ctx)// Construct a request with the tracerreq, ht := nethttp.TraceRequest(tracer, req)
httpClient := &http.Client{Transport: &nethttp.Transport{}} // Initialize the HTTP clientres, err := httpClient.Do(req)// ... Error judgment is omitted.body, err := ioutil.ReadAll(res.Body)// ... Error judgment is omitted.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 (// Service name, which is the unique identifier of the service and the basis for service metric aggregation and filtering.ginClientName = "demo-gin-client"ginPort = ":8080"endPoint = "xxxxx:6831" // Local agent addresstoken = "abc")// The Gin client under StartClient is also a standard HTTP client.func StartClient() {cfg := &jaegerConfig.Configuration{ServiceName: ginClientName, // Call trace of the target service. Enter the service name.Sampler: &jaegerConfig.SamplerConfig{ // Sampling policy configuration. See section 4.1.1 for details.Type: "const",Param: 1,},Reporter: &jaegerConfig.ReporterConfig{ // Configure how the client reports trace information. All fields are optional.LogSpans: true,LocalAgentHostPort: endPoint,},// Token configurationTags: []opentracing.Tag{ // Set the tag, where information such as token can be stored.opentracing.Tag{Key: "token", Value: token}, // Set the token},}tracer, closer, err := cfg.NewTracer(jaegerConfig.Logger(jaeger.StdLogger)) // Get the tracer based on the configurationdefer closer.Close()if err != nil {panic(fmt.Sprintf("ERROR: fail init Jaeger: %v\\n", err))}// Construct a span and put it in the contextspan := tracer.StartSpan("CallDemoServer")ctx := opentracing.ContextWithSpan(context.Background(), span)defer span.Finish()// Construct an HTTP requestreq, err := http.NewRequest(http.MethodGet,fmt.Sprintf("http://localhost%s/ping", ginPort),nil,)if err != nil {HandlerError(span, err)return}// Construct a request with a tracerreq = req.WithContext(ctx)req, ht := nethttp.TraceRequest(tracer, req)defer ht.Finish()// Initialize the HTTP clienthttpClient := &http.Client{Transport: &nethttp.Transport{}}// Send the requestres, 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))}
Was this page helpful?