From b7b6d50dc6d0dc731593860570e96895598d4d5e Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 17 Jan 2023 08:43:04 +0300 Subject: [PATCH 1/2] 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) +} From 38d841d5b67f830c66ba21be444b88e1fb7cd201 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 17 Jan 2023 09:21:48 +0300 Subject: [PATCH 2/2] add ability to pass opentracing.Tracer Signed-off-by: Vasiliy Tolstov --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index f63c0ed..fdd581a 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,6 @@ go 1.16 require ( github.com/opentracing/opentracing-go v1.2.0 - go.unistack.org/micro/v3 v3.10.0 + go.unistack.org/micro/v3 v3.10.1 gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index b90eaa4..f97a005 100644 --- a/go.sum +++ b/go.sum @@ -73,8 +73,8 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1: github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y= go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= go.unistack.org/micro-proto/v3 v3.3.1/go.mod h1:cwRyv8uInM2I7EbU7O8Fx2Ls3N90Uw9UCCcq4olOdfE= -go.unistack.org/micro/v3 v3.10.0 h1:nPtk5Pfwk524HyezGtQ3m3vbK4LdvXqWLI7HgeilYOk= -go.unistack.org/micro/v3 v3.10.0/go.mod h1:gI4RkJKHLPW7KV6h4+ZBOZD997MRvFRXMPQIHpozikI= +go.unistack.org/micro/v3 v3.10.1 h1:MbtPj7ueTw4x6lL2Ok2ez70ORyGXBWhxizO5fQsnAA4= +go.unistack.org/micro/v3 v3.10.1/go.mod h1:gI4RkJKHLPW7KV6h4+ZBOZD997MRvFRXMPQIHpozikI= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=