Merge pull request #27 from unistack-org/fixes

implement tracer.Tracer interface
This commit is contained in:
Василий Толстов 2023-01-16 23:43:40 +03:00 committed by GitHub
commit 1566ccf0d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,10 +1,10 @@
// Package opentracing provides wrappers for OpenTracing
package opentracing package opentracing
import ( import (
"context" "context"
opentracing "github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"go.unistack.org/micro/v3/metadata" "go.unistack.org/micro/v3/metadata"
"go.unistack.org/micro/v3/tracer" "go.unistack.org/micro/v3/tracer"
) )
@ -13,6 +13,7 @@ var _ tracer.Tracer = &opentracingTracer{}
type opentracingTracer struct { type opentracingTracer struct {
opts tracer.Options opts tracer.Options
tracer opentracing.Tracer
} }
func (ot *opentracingTracer) Name() string { func (ot *opentracingTracer) Name() string {
@ -20,11 +21,49 @@ 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...)
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) {
return nil, nil ctx, span, _ := StartSpanFromIncomingContext(ctx, ot.tracer, name)
return ctx, &opentracingSpan{span: span}
}
type opentracingSpan struct {
span opentracing.Span
labels []interface{}
}
func (os *opentracingSpan) Tracer() tracer.Tracer {
return &opentracingTracer{tracer: os.span.Tracer()}
}
func (os *opentracingSpan) Finish(opts ...tracer.SpanOption) {
if len(os.labels) > 0 {
os.span.LogKV(os.labels...)
}
os.span.Finish()
}
func (os *opentracingSpan) AddEvent(name string, opts ...tracer.EventOption) {
os.span.LogFields(log.Event(name))
}
func (os *opentracingSpan) Context() context.Context {
return tracer.NewSpanContext(context.Background(), os)
}
func (os *opentracingSpan) SetName(name string) {
os.span = os.span.SetOperationName(name)
}
func (os *opentracingSpan) SetLabels(labels ...interface{}) {
os.labels = labels
}
func (os *opentracingSpan) AddLabels(labels ...interface{}) {
os.labels = append(os.labels, labels...)
} }
func NewTracer(opts ...tracer.Option) *opentracingTracer { func NewTracer(opts ...tracer.Option) *opentracingTracer {