From b7b6d50dc6d0dc731593860570e96895598d4d5e Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 17 Jan 2023 08:43:04 +0300 Subject: [PATCH] add ability to pass opentracing.Tracer Signed-off-by: Vasiliy Tolstov --- opentracing.go | 22 ++++++++++++++++------ opentracing_test.go | 2 +- options.go | 12 ++++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 options.go diff --git a/opentracing.go b/opentracing.go index 49c126b..1c924fd 100644 --- a/opentracing.go +++ b/opentracing.go @@ -2,6 +2,7 @@ package opentracing import ( "context" + "errors" "github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go/log" @@ -21,12 +22,21 @@ func (ot *opentracingTracer) Name() string { } func (ot *opentracingTracer) Init(opts ...tracer.Option) error { - ot.opts = tracer.NewOptions(opts...) + for _, o := range opts { + o(&ot.opts) + } + + if tr, ok := ot.opts.Context.Value(tracerKey{}).(opentracing.Tracer); ok { + ot.tracer = tr + } else { + return errors.New("Tracer option missing") + } + return nil } func (ot *opentracingTracer) Start(ctx context.Context, name string, opts ...tracer.SpanOption) (context.Context, tracer.Span) { - ctx, span, _ := StartSpanFromIncomingContext(ctx, ot.tracer, name) + ctx, span, _ := startSpanFromIncomingContext(ctx, ot.tracer, name) return ctx, &opentracingSpan{span: span} } @@ -75,9 +85,9 @@ func spanFromContext(ctx context.Context) opentracing.Span { return opentracing.SpanFromContext(ctx) } -// StartSpanFromOutgoingContext returns a new span with the given operation name and options. If a span +// startSpanFromOutgoingContext returns a new span with the given operation name and options. If a span // is found in the context, it will be used as the parent of the resulting span. -func StartSpanFromOutgoingContext(ctx context.Context, tracer opentracing.Tracer, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span, error) { +func startSpanFromOutgoingContext(ctx context.Context, tracer opentracing.Tracer, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span, error) { var parentCtx opentracing.SpanContext md, ok := metadata.FromIncomingContext(ctx) @@ -106,9 +116,9 @@ func StartSpanFromOutgoingContext(ctx context.Context, tracer opentracing.Tracer return ctx, sp, nil } -// StartSpanFromIncomingContext returns a new span with the given operation name and options. If a span +// startSpanFromIncomingContext returns a new span with the given operation name and options. If a span // is found in the context, it will be used as the parent of the resulting span. -func StartSpanFromIncomingContext(ctx context.Context, tracer opentracing.Tracer, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span, error) { +func startSpanFromIncomingContext(ctx context.Context, tracer opentracing.Tracer, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span, error) { var parentCtx opentracing.SpanContext // Find parent span. diff --git a/opentracing_test.go b/opentracing_test.go index 664d98c..0ad3e0e 100644 --- a/opentracing_test.go +++ b/opentracing_test.go @@ -26,7 +26,7 @@ func TestStartSpanFromIncomingContext(t *testing.T) { for i := 0; i < 8000; i++ { go func() { defer g.Done() - _, sp, err := StartSpanFromIncomingContext(ctx, tracer, "test") + _, sp, err := startSpanFromIncomingContext(ctx, tracer, "test") if err != nil { cherr <- err } diff --git a/options.go b/options.go new file mode 100644 index 0000000..bb0221e --- /dev/null +++ b/options.go @@ -0,0 +1,12 @@ +package opentracing + +import ( + "github.com/opentracing/opentracing-go" + "go.unistack.org/micro/v3/tracer" +) + +type tracerKey struct{} + +func Tracer(ot opentracing.Tracer) tracer.Option { + return tracer.SetOption(tracerKey{}, ot) +}