nohup ./jaeger-agent --reporter.grpc.host-port={{collectorRPCHostPort}} --agent.tags=token={{token}}
opentracing-contrib/go-gin
on the server.github.com/opentracing-contrib/go-gin
v0.0.0-20201220185307-1dd2273433a4
cfg := &jaegerConfig.Configuration{ServiceName: ginServerName, // 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
r := gin.Default()// Pass in the tracerr.Use(ginhttp.Middleware(tracer))
/user/{id}
is correct, while /user/1
is incorrect.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")// Service name, which is the unique identifier of the service and the basis for service metric aggregation and filtering.const ginServerName = "demo-gin-server"// StartServerfunc StartServer() {// Initialize Jaeger to get the tracercfg := &jaegerConfig.Configuration{ServiceName: ginServerName, // 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 configurationif err != nil {panic(fmt.Sprintf("ERROR: fail init Jaeger: %v\\n", err))}defer closer.Close()r := gin.Default()// Note that the official default OperationName is HTTP + HttpMethod.// We recommend you use HTTP + HttpMethod + URL to analyze the specific API as follows:// Note: For RESTful APIs, URL should be the parameter name rather than the specific parameter value; for example, `/user/{id}` is correct, while `/user/1` is incorrect.r.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() // Listen on 0.0.0.0:8080}
opentracing-contrib/go-stdlib/nethttp
dependency.github.com/opentracing-contrib/go-stdlib/nethttp
v1.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?