add ability to pass opentracing.Tracer #28

Merged
vtolstov merged 2 commits from fixup into v3 2023-01-17 09:23:12 +03:00
5 changed files with 32 additions and 10 deletions

2
go.mod
View File

@ -4,6 +4,6 @@ go 1.16
require ( require (
github.com/opentracing/opentracing-go v1.2.0 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 gopkg.in/yaml.v2 v2.4.0 // indirect
) )

4
go.sum
View File

@ -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= 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.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-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.1 h1:MbtPj7ueTw4x6lL2Ok2ez70ORyGXBWhxizO5fQsnAA4=
go.unistack.org/micro/v3 v3.10.0/go.mod h1:gI4RkJKHLPW7KV6h4+ZBOZD997MRvFRXMPQIHpozikI= 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-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 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= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=

View File

@ -2,6 +2,7 @@ package opentracing
import ( import (
"context" "context"
"errors"
"github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log" "github.com/opentracing/opentracing-go/log"
@ -21,12 +22,21 @@ func (ot *opentracingTracer) Name() string {
} }
func (ot *opentracingTracer) Init(opts ...tracer.Option) error { 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 return nil
} }
func (ot *opentracingTracer) Start(ctx context.Context, name string, opts ...tracer.SpanOption) (context.Context, tracer.Span) { 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} return ctx, &opentracingSpan{span: span}
} }
@ -75,9 +85,9 @@ func spanFromContext(ctx context.Context) opentracing.Span {
return opentracing.SpanFromContext(ctx) 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. // 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 var parentCtx opentracing.SpanContext
md, ok := metadata.FromIncomingContext(ctx) md, ok := metadata.FromIncomingContext(ctx)
@ -106,9 +116,9 @@ func StartSpanFromOutgoingContext(ctx context.Context, tracer opentracing.Tracer
return ctx, sp, nil 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. // 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 var parentCtx opentracing.SpanContext
// Find parent span. // Find parent span.

View File

@ -26,7 +26,7 @@ func TestStartSpanFromIncomingContext(t *testing.T) {
for i := 0; i < 8000; i++ { for i := 0; i < 8000; i++ {
go func() { go func() {
defer g.Done() defer g.Done()
_, sp, err := StartSpanFromIncomingContext(ctx, tracer, "test") _, sp, err := startSpanFromIncomingContext(ctx, tracer, "test")
if err != nil { if err != nil {
cherr <- err cherr <- err
} }

12
options.go Normal file
View File

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