From 2abea05b41b1a452d9771dfa002b310769d34503 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 16 Jan 2023 23:41:56 +0300 Subject: [PATCH] update Signed-off-by: Vasiliy Tolstov --- opentracing.go | 47 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/opentracing.go b/opentracing.go index 960b715..49c126b 100644 --- a/opentracing.go +++ b/opentracing.go @@ -1,10 +1,10 @@ -// Package opentracing provides wrappers for OpenTracing package opentracing import ( "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/tracer" ) @@ -12,7 +12,8 @@ import ( var _ tracer.Tracer = &opentracingTracer{} type opentracingTracer struct { - opts tracer.Options + opts tracer.Options + tracer opentracing.Tracer } func (ot *opentracingTracer) Name() string { @@ -20,11 +21,49 @@ func (ot *opentracingTracer) Name() string { } func (ot *opentracingTracer) Init(opts ...tracer.Option) error { + ot.opts = tracer.NewOptions(opts...) return nil } 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 { -- 2.45.2