Signed-off-by: Andres Taylor <antaylor@squareup.com>
This commit is contained in:
Andres Taylor 2019-08-19 13:06:52 +02:00
Родитель b845c016ee
Коммит 8d174a8101
4 изменённых файлов: 25 добавлений и 22 удалений

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

@ -23,16 +23,16 @@ import (
"google.golang.org/grpc"
)
type fakeSpanFactory struct{}
type fakeTracingServer struct{}
func (fakeSpanFactory) New(Span, string) Span { return fakeSpan{} }
func (fakeSpanFactory) NewClientSpan(parent Span, serviceName, label string) Span { return fakeSpan{} }
func (fakeSpanFactory) FromContext(context.Context) (Span, bool) { return nil, false }
func (fakeSpanFactory) NewFromString(parent, label string) (Span, error) { return fakeSpan{}, nil }
func (fakeSpanFactory) NewContext(parent context.Context, _ Span) context.Context { return parent }
func (fakeSpanFactory) AddGrpcServerOptions(addInterceptors func(s grpc.StreamServerInterceptor, u grpc.UnaryServerInterceptor)) {
func (fakeTracingServer) New(Span, string) Span { return fakeSpan{} }
func (fakeTracingServer) NewClientSpan(parent Span, serviceName, label string) Span { return fakeSpan{} }
func (fakeTracingServer) FromContext(context.Context) (Span, bool) { return nil, false }
func (fakeTracingServer) NewFromString(parent, label string) (Span, error) { return fakeSpan{}, nil }
func (fakeTracingServer) NewContext(parent context.Context, _ Span) context.Context { return parent }
func (fakeTracingServer) AddGrpcServerOptions(addInterceptors func(s grpc.StreamServerInterceptor, u grpc.UnaryServerInterceptor)) {
}
func (fakeSpanFactory) AddGrpcClientOptions(addInterceptors func(s grpc.StreamClientInterceptor, u grpc.UnaryClientInterceptor)) {
func (fakeTracingServer) AddGrpcClientOptions(addInterceptors func(s grpc.StreamClientInterceptor, u grpc.UnaryClientInterceptor)) {
}
// fakeSpan implements Span with no-op methods.
@ -43,6 +43,6 @@ func (fakeSpan) Annotate(string, interface{}) {}
func init() {
tracingBackendFactories["noop"] = func(_ string) (tracingService, io.Closer, error) {
return fakeSpanFactory{}, &nilCloser{}, nil
return fakeTracingServer{}, &nilCloser{}, nil
}
}

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

@ -90,11 +90,10 @@ func (jf openTracingService) NewFromString(parent, label string) (Span, error) {
func (jf openTracingService) FromContext(ctx context.Context) (Span, bool) {
innerSpan := opentracing.SpanFromContext(ctx)
if innerSpan != nil {
return openTracingSpan{otSpan: innerSpan}, true
} else {
if innerSpan == nil {
return nil, false
}
return openTracingSpan{otSpan: innerSpan}, true
}
// NewContext is part of an interface implementation

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

@ -45,9 +45,9 @@ type Span interface {
// NewSpan creates a new Span with the currently installed tracing plugin.
// If no tracing plugin is installed, it returns a fake Span that does nothing.
func NewSpan(inCtx context.Context, label string) (Span, context.Context) {
parent, _ := spanFactory.FromContext(inCtx)
span := spanFactory.New(parent, label)
outCtx := spanFactory.NewContext(inCtx, span)
parent, _ := currentTracer.FromContext(inCtx)
span := currentTracer.New(parent, label)
outCtx := currentTracer.NewContext(inCtx, span)
return span, outCtx
}
@ -61,12 +61,12 @@ func AnnotateSQL(span Span, sql string) {
// FromContext returns the Span from a Context if present. The bool return
// value indicates whether a Span was present in the Context.
func FromContext(ctx context.Context) (Span, bool) {
return spanFactory.FromContext(ctx)
return currentTracer.FromContext(ctx)
}
// NewContext returns a context based on parent with a new Span value.
func NewContext(parent context.Context, span Span) context.Context {
return spanFactory.NewContext(parent, span)
return currentTracer.NewContext(parent, span)
}
// CopySpan creates a new context from parentCtx, with only the trace span
@ -78,12 +78,14 @@ func CopySpan(parentCtx, spanCtx context.Context) context.Context {
return parentCtx
}
// AddGrpcServerOptions adds GRPC interceptors that read the parent span from the grpc packets
func AddGrpcServerOptions(addInterceptors func(s grpc.StreamServerInterceptor, u grpc.UnaryServerInterceptor)) {
spanFactory.AddGrpcServerOptions(addInterceptors)
currentTracer.AddGrpcServerOptions(addInterceptors)
}
// AddGrpcClientOptions adds GRPC interceptors that add parent information to outgoing grpc packets
func AddGrpcClientOptions(addInterceptors func(s grpc.StreamClientInterceptor, u grpc.UnaryClientInterceptor)) {
spanFactory.AddGrpcClientOptions(addInterceptors)
currentTracer.AddGrpcClientOptions(addInterceptors)
}
// tracingService is an interface for creating spans or extracting them from Contexts.
@ -107,12 +109,14 @@ type tracingService interface {
AddGrpcClientOptions(addInterceptors func(s grpc.StreamClientInterceptor, u grpc.UnaryClientInterceptor))
}
// TracerFactory creates a tracing service for the service provided. It's important to close the provided io.Closer
// object to make sure that all spans are sent to the backend before the process exits.
type TracerFactory func(serviceName string) (tracingService, io.Closer, error)
// tracingBackendFactories should be added to by a plugin during init() to install itself
var tracingBackendFactories = make(map[string]TracerFactory)
var spanFactory tracingService = fakeSpanFactory{}
var currentTracer tracingService = fakeTracingServer{}
var (
tracingServer = flag.String("tracer", "noop", "tracing service to use")
@ -131,7 +135,7 @@ func StartTracing(serviceName string) io.Closer {
return &nilCloser{}
}
spanFactory = tracer
currentTracer = tracer
log.Infof("successfully started tracing with [%s]", *tracingServer)

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

@ -37,7 +37,7 @@ func TestFakeSpan(t *testing.T) {
span2.Annotate("key", 42)
span2.Finish()
span3, ctx := NewSpan(ctx, "label")
span3, _ := NewSpan(ctx, "label")
span3.Annotate("key", 42)
span3.Finish()
}