本文主要是介绍jaeger简单发送---链路追踪,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
jaeger
go发送单个span
package mainimport ("fmt""github.com/uber/jaeger-client-go"jaegercfg "github.com/uber/jaeger-client-go/config"
)func main() {cfg := jaegercfg.Configuration{Sampler: &jaegercfg.SamplerConfig{ //采样类型Type: jaeger.SamplerTypeConst,Param: 1,},Reporter: &jaegercfg.ReporterConfig{ //上报配置LogSpans: true, //打印日志LocalAgentHostPort: "192.168.0.102:6831",},//Disabled:ServiceName: "chengpeng", //服务名}fmt.Println("chengpeng", cfg)tracer, closer, err := cfg.NewTracer(jaegercfg.Logger(jaeger.StdLogger))if err != nil {panic(err)}defer closer.Close()span := tracer.StartSpan("go-grpc-web")defer span.Finish()
}
相关功能讲解
go发送多级嵌套span
上图tracer相同
package mainimport ("github.com/opentracing/opentracing-go""github.com/uber/jaeger-client-go""time"jaegercfg "github.com/uber/jaeger-client-go/config"
)func main() {//var a []opentracing.Tag//a = append(a, opentracing.Tag{Key: "chengpeng", Value: 123})cfg := jaegercfg.Configuration{Sampler: &jaegercfg.SamplerConfig{ //采样类型Type: jaeger.SamplerTypeConst,Param: 1,},Reporter: &jaegercfg.ReporterConfig{ //上报配置LogSpans: true, //打印日志LocalAgentHostPort: "192.168.0.102:6831", //当前ip},//Disabled:ServiceName: "chengpeng", //服务名}tracer, closer, err := cfg.NewTracer(jaegercfg.Logger(jaeger.StdLogger))if err != nil {panic(err)}defer closer.Close()parentSpan := tracer.StartSpan("main")span := tracer.StartSpan("funcA", opentracing.ChildOf(parentSpan.Context()))time.Sleep(time.Millisecond * 500)span.Finish()time.Sleep(time.Millisecond * 50)span2 := tracer.StartSpan("funcB", opentracing.ChildOf(parentSpan.Context()))time.Sleep(time.Millisecond * 1000)span2.Finish()parentSpan.Finish()
}
go下通过grpc发送span
go-grpc-opentracing
package mainimport ("context""fmt""OldPackageTest/jaeger_test/otgrpc""github.com/opentracing/opentracing-go""github.com/uber/jaeger-client-go"jaegercfg "github.com/uber/jaeger-client-go/config""google.golang.org/grpc""OldPackageTest/grpc_test/proto"
)func main() {cfg := jaegercfg.Configuration{Sampler: &jaegercfg.SamplerConfig{Type: jaeger.SamplerTypeConst,Param: 1,},Reporter: &jaegercfg.ReporterConfig{LogSpans: true,LocalAgentHostPort: "192.168.0.104:6831",},ServiceName: "mxshop",}tracer, closer, err := cfg.NewTracer(jaegercfg.Logger(jaeger.StdLogger))if err != nil {panic(err)}opentracing.SetGlobalTracer(tracer)defer closer.Close()conn, err := grpc.Dial("127.0.0.1:50051", grpc.WithInsecure(), grpc.WithUnaryInterceptor(otgrpc.OpenTracingClientInterceptor(opentracing.GlobalTracer())))if err != nil {panic(err)}defer conn.Close()c := proto.NewGreeterClient(conn)r, err := c.SayHello(context.Background(), &proto.HelloRequest{Name: "bobby"})if err != nil {panic(err)}fmt.Println(r.Message)
}
修改资源绑定的代码
func OpenTracingClientInterceptor(tracer opentracing.Tracer, optFuncs ...Option) grpc.UnaryClientInterceptor {otgrpcOpts := newOptions()otgrpcOpts.apply(optFuncs...)return func(ctx context.Context,method string,req, resp interface{},cc *grpc.ClientConn,invoker grpc.UnaryInvoker,opts ...grpc.CallOption,) error {var err errorvar parentCtx opentracing.SpanContextif parent := opentracing.SpanFromContext(ctx); parent != nil {parentCtx = parent.Context()}//fmt.Println("chengpengtext", parentCtx)ginContext := ctx.Value("ginContext")switch ginContext.(type) {case *gin.Context:if itracer, ok := ginContext.(*gin.Context).Get("tracer"); ok {tracer = itracer.(opentracing.Tracer)}if parentSpan, ok := ginContext.(*gin.Context).Get("parentSpan"); ok {parentCtx = parentSpan.(*jaegerClient.Span).Context()}}//fmt.Println("chengpengtext1", ginContext)//fmt.Println("chengpengtext2", tracer)//fmt.Println("chengpengtext3", parentCtx)if otgrpcOpts.inclusionFunc != nil &&!otgrpcOpts.inclusionFunc(parentCtx, method, req, resp) {return invoker(ctx, method, req, resp, cc, opts...)}clientSpan := tracer.StartSpan(method,opentracing.ChildOf(parentCtx),ext.SpanKindRPCClient,gRPCComponentTag,)defer clientSpan.Finish()ctx = injectSpanContext(ctx, tracer, clientSpan)if otgrpcOpts.logPayloads {clientSpan.LogFields(log.Object("gRPC request", req))}err = invoker(ctx, method, req, resp, cc, opts...)if err == nil {if otgrpcOpts.logPayloads {clientSpan.LogFields(log.Object("gRPC response", resp))}} else {SetSpanTags(clientSpan, err, true)clientSpan.LogFields(log.String("event", "error"), log.String("message", err.Error()))}if otgrpcOpts.decorator != nil {otgrpcOpts.decorator(clientSpan, method, req, resp, err)}return err}
}
链路追踪
package middlewaresimport ("chengpeng_api/goods-web/global""fmt""github.com/gin-gonic/gin""github.com/uber/jaeger-client-go"jaegercfg "github.com/uber/jaeger-client-go/config"//"google.golang.org/grpc"
)// Trace 链路跟踪
func Trace() gin.HandlerFunc {return func(ctx *gin.Context) {cfg := jaegercfg.Configuration{Sampler: &jaegercfg.SamplerConfig{Type: jaeger.SamplerTypeConst,Param: 1,},Reporter: &jaegercfg.ReporterConfig{LogSpans: true,LocalAgentHostPort: fmt.Sprintf("%s:%d", global.ServerConfig.JaegerInfo.Host, global.ServerConfig.JaegerInfo.Port),},ServiceName: global.ServerConfig.JaegerInfo.Name,}tracer, closer, err := cfg.NewTracer(jaegercfg.Logger(jaeger.StdLogger))if err != nil {panic(err)}//opentracing.SetGlobalTracer(tracer) //设置为全局 这样使用就会出问题 不能放到同一个tracerdefer closer.Close()fmt.Println("chengpengTrace", ctx.Request.URL.Path) //每一个api都是tracestartSpan := tracer.StartSpan(ctx.Request.URL.Path) //起始的Spandefer startSpan.Finish()//放到ctx中 需要直接传递过去ctx.Set("tracer", tracer)ctx.Set("parentSpan", startSpan)ctx.Next()}
}
这样就会设置gin.conext中去,然后通过图片上的就可以放到context中
grpc的server端如何获取客户端
这篇关于jaeger简单发送---链路追踪的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!