在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
概述 example-hotrod jaeger-agent jaeger-all-in-one jaeger-collector jaeger-ingester jaeger-query
./jaeger-all-in-one
链接:https://pan.baidu.com/s/1v5gm7n0L7TGyejCmQrMh2g 提取码:x2p5
实例化 Tracer func NewJaegerTracer(serviceName string, jaegerHostPort string) (opentracing.Tracer, io.Closer, error) { cfg := &jaegerConfig.Configuration { Sampler: &jaegerConfig.SamplerConfig{ Type : "const", //固定采样 Param : 1, //1=全采样、0=不采样 }, Reporter: &jaegerConfig.ReporterConfig{ LogSpans : true, LocalAgentHostPort : jaegerHostPort, }, ServiceName: serviceName, } tracer, closer, err := cfg.NewTracer(jaegerConfig.Logger(jaeger.StdLogger)) if err != nil { panic(fmt.Sprintf("ERROR: cannot init Jaeger: %v\n", err)) } opentracing.SetGlobalTracer(tracer) return tracer, closer, err } HTTP 注入 injectErr := jaeger.Tracer.Inject(span.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header)) if injectErr != nil { log.Fatalf("%s: Couldn't inject headers", err) } HTTP 拦截 spCtx, err := opentracing.GlobalTracer().Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(c.Request.Header)) if err != nil { ParentSpan = Tracer.StartSpan(c.Request.URL.Path) defer ParentSpan.Finish() } else { ParentSpan = opentracing.StartSpan( c.Request.URL.Path, opentracing.ChildOf(spCtx), opentracing.Tag{Key: string(ext.Component), Value: "HTTP"}, ext.SpanKindRPCServer, ) defer ParentSpan.Finish() } gRPC 注入 func ClientInterceptor(tracer opentracing.Tracer, spanContext opentracing.SpanContext) grpc.UnaryClientInterceptor { return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { span := opentracing.StartSpan( "call gRPC", opentracing.ChildOf(spanContext), opentracing.Tag{Key: string(ext.Component), Value: "gRPC"}, ext.SpanKindRPCClient, ) defer span.Finish() md, ok := metadata.FromOutgoingContext(ctx) if !ok { md = metadata.New(nil) } else { md = md.Copy() } err := tracer.Inject(span.Context(), opentracing.TextMap, MDReaderWriter{md}) if err != nil { span.LogFields(log.String("inject-error", err.Error())) } newCtx := metadata.NewOutgoingContext(ctx, md) err = invoker(newCtx, method, req, reply, cc, opts...) if err != nil { span.LogFields(log.String("call-error", err.Error())) } return err } } gRPC 拦截 func serverInterceptor(tracer opentracing.Tracer) grpc.UnaryServerInterceptor { return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { md, ok := metadata.FromIncomingContext(ctx) if !ok { md = metadata.New(nil) } spanContext, err := tracer.Extract(opentracing.TextMap, MDReaderWriter{md}) if err != nil && err != opentracing.ErrSpanContextNotFound { grpclog.Errorf("extract from metadata err: %v", err) } else { span := tracer.StartSpan( info.FullMethod, ext.RPCServerOption(spanContext), opentracing.Tag{Key: string(ext.Component), Value: "gRPC"}, ext.SpanKindRPCServer, ) defer span.Finish() ParentContext = opentracing.ContextWithSpan(ctx, span) } return handler(ParentContext, req) } }
// 启动 Listen 服务 cd listen && go run main.go // 启动 Speak 服务 cd speak && go run main.go // 启动 Read 服务 cd read && go run main.go // 启动 Write 服务 cd write && go run main.go // 启动 Sing 服务 cd sing && go run main.go // 启动 go-gin-api 服务 cd go-gin-api && go run main.go
http://127.0.0.1:9999/jaeger_test
|
请发表评论