add ability to pass opentracing.Tracer
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
1566ccf0d8
commit
b7b6d50dc6
@ -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.
|
||||||
|
@ -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
12
options.go
Normal 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)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user