diff --git a/go/trace/opentracing.go b/go/trace/opentracing.go index 6b47177d45..4413d808a1 100644 --- a/go/trace/opentracing.go +++ b/go/trace/opentracing.go @@ -41,20 +41,25 @@ func (js openTracingSpan) Annotate(key string, value interface{}) { var _ tracingService = (*openTracingService)(nil) +type tracer interface { + GetOpenTracingTracer() opentracing.Tracer + FromString(string) (opentracing.SpanContext, error) +} + type openTracingService struct { - Tracer opentracing.Tracer - fromString func(string) (opentracing.SpanContext, error) - toString func(Span) string + Tracer tracer } // AddGrpcServerOptions is part of an interface implementation func (jf openTracingService) AddGrpcServerOptions(addInterceptors func(s grpc.StreamServerInterceptor, u grpc.UnaryServerInterceptor)) { - addInterceptors(otgrpc.OpenTracingStreamServerInterceptor(jf.Tracer), otgrpc.OpenTracingServerInterceptor(jf.Tracer)) + ot := jf.Tracer.GetOpenTracingTracer() + addInterceptors(otgrpc.OpenTracingStreamServerInterceptor(ot), otgrpc.OpenTracingServerInterceptor(ot)) } // AddGrpcClientOptions is part of an interface implementation func (jf openTracingService) AddGrpcClientOptions(addInterceptors func(s grpc.StreamClientInterceptor, u grpc.UnaryClientInterceptor)) { - addInterceptors(otgrpc.OpenTracingStreamClientInterceptor(jf.Tracer), otgrpc.OpenTracingClientInterceptor(jf.Tracer)) + ot := jf.Tracer.GetOpenTracingTracer() + addInterceptors(otgrpc.OpenTracingStreamClientInterceptor(ot), otgrpc.OpenTracingClientInterceptor(ot)) } // NewClientSpan is part of an interface implementation @@ -68,21 +73,21 @@ func (jf openTracingService) NewClientSpan(parent Span, serviceName, label strin func (jf openTracingService) New(parent Span, label string) Span { var innerSpan opentracing.Span if parent == nil { - innerSpan = jf.Tracer.StartSpan(label) + innerSpan = jf.Tracer.GetOpenTracingTracer().StartSpan(label) } else { jaegerParent := parent.(openTracingSpan) span := jaegerParent.otSpan - innerSpan = jf.Tracer.StartSpan(label, opentracing.ChildOf(span.Context())) + innerSpan = jf.Tracer.GetOpenTracingTracer().StartSpan(label, opentracing.ChildOf(span.Context())) } return openTracingSpan{otSpan: innerSpan} } func (jf openTracingService) NewFromString(parent, label string) (Span, error) { - spanContext, err := jf.fromString(parent) + spanContext, err := jf.Tracer.FromString(parent) if err != nil { return nil, vterrors.Wrap(err, "failed to deserialize span context") } - innerSpan := jf.Tracer.StartSpan(label, opentracing.ChildOf(spanContext)) + innerSpan := jf.Tracer.GetOpenTracingTracer().StartSpan(label, opentracing.ChildOf(spanContext)) return openTracingSpan{otSpan: innerSpan}, nil } diff --git a/go/trace/plugin_jaeger.go b/go/trace/plugin_jaeger.go index abae1bfbe6..b8053a4e2c 100644 --- a/go/trace/plugin_jaeger.go +++ b/go/trace/plugin_jaeger.go @@ -82,20 +82,23 @@ func newJagerTracerFromEnv(serviceName string) (tracingService, io.Closer, error opentracing.SetGlobalTracer(tracer) - f1 := func(s string) (context opentracing.SpanContext, e error) { - // I don't understand why I have to do this and not just use `jaeger.ContextFromString` directly - return jaeger.ContextFromString(s) - } - - f2 := func(in Span) string { - otSpan := in.(*openTracingSpan) - jaegerSpanContext := otSpan.otSpan.Context().(*jaeger.SpanContext) - return jaegerSpanContext.String() - } - - return openTracingService{Tracer: tracer, fromString: f1, toString: f2}, closer, nil + return openTracingService{Tracer: &jaegerTracer{actual: tracer}}, closer, nil } func init() { tracingBackendFactories["opentracing-jaeger"] = newJagerTracerFromEnv } + +var _ tracer = (*jaegerTracer)(nil) + +type jaegerTracer struct { + actual opentracing.Tracer +} + +func (*jaegerTracer) FromString(s string) (opentracing.SpanContext, error) { + return jaeger.ContextFromString(s) +} + +func (jt *jaegerTracer) GetOpenTracingTracer() opentracing.Tracer { + return jt.actual +}