Merge pull request #5175 from systay/tracing-clean

Use interfaces instead of functions
This commit is contained in:
Sugu Sougoumarane 2019-09-10 18:53:24 -07:00 коммит произвёл GitHub
Родитель a9bd92593d 9db6df7057
Коммит 1f66f10fb6
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 29 добавлений и 21 удалений

Просмотреть файл

@ -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
}

Просмотреть файл

@ -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
}