From 59283b64c980329224376d339fb022867b60dc29 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Fri, 1 Sep 2023 08:08:34 +0300 Subject: [PATCH] fixup span and tags Signed-off-by: Vasiliy Tolstov --- go.mod | 9 +- go.sum | 24 +++--- opentracing.go | 201 +++++++++++++++++++++++++++++++------------- opentracing_test.go | 8 +- 4 files changed, 162 insertions(+), 80 deletions(-) diff --git a/go.mod b/go.mod index 6271228..873b564 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,10 @@ module go.unistack.org/micro-tracer-opentracing/v3 -go 1.16 +go 1.20 require ( - github.com/davecgh/go-spew v1.1.1 // indirect github.com/opentracing/opentracing-go v1.2.0 - github.com/stretchr/testify v1.5.1 // indirect - go.unistack.org/micro/v3 v3.10.24 - gopkg.in/yaml.v2 v2.4.0 // indirect + go.unistack.org/micro/v3 v3.10.25 ) + +require github.com/stretchr/testify v1.8.1 // indirect diff --git a/go.sum b/go.sum index 32298c8..51b5b7a 100644 --- a/go.sum +++ b/go.sum @@ -1,25 +1,21 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/imdario/mergo v0.3.14/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY= -github.com/imdario/mergo v0.3.15/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY= github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs= github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= -github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/silas/dag v0.0.0-20211117232152-9d50aa809f35/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= -github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -go.unistack.org/micro/v3 v3.10.19 h1:JtPacglETxHyb+GiuMdfxqGtnWSTYXPQm6PlDQfNIl8= -go.unistack.org/micro/v3 v3.10.19/go.mod h1:XIArw29f0b3uvF4cq96X/nQt2f0J2OGnjh8J+DBbC0s= -go.unistack.org/micro/v3 v3.10.24 h1:gIi4r0vUSpNLs+uRb4KKEXBLkE8iNp6ZnJsI5FV42j0= -go.unistack.org/micro/v3 v3.10.24/go.mod h1:aywPekJP0n07xvmDgj+Si3VPmQzGMfj6tkHk/6kjXhU= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +go.unistack.org/micro/v3 v3.10.25 h1:A0epdZHOqjnXx103wwFhPKgmvVVbScvfbmn3HmHz1wE= +go.unistack.org/micro/v3 v3.10.25/go.mod h1:ALkeXpqChYDjx8KPi7tz9mmIyOnob6nlNswsg8BnZjQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= -gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/opentracing.go b/opentracing.go index de0a813..a528124 100644 --- a/opentracing.go +++ b/opentracing.go @@ -3,9 +3,10 @@ package opentracing import ( "context" "errors" + "fmt" - "github.com/opentracing/opentracing-go" - "github.com/opentracing/opentracing-go/log" + ot "github.com/opentracing/opentracing-go" + otlog "github.com/opentracing/opentracing-go/log" "go.unistack.org/micro/v3/metadata" "go.unistack.org/micro/v3/tracer" ) @@ -14,24 +15,24 @@ var _ tracer.Tracer = &otTracer{} type otTracer struct { opts tracer.Options - tracer opentracing.Tracer + tracer ot.Tracer } -func (ot *otTracer) Name() string { - return ot.opts.Name +func (t *otTracer) Name() string { + return t.opts.Name } -func (ot *otTracer) Flush(ctx context.Context) error { +func (t *otTracer) Flush(ctx context.Context) error { return nil } -func (ot *otTracer) Init(opts ...tracer.Option) error { +func (t *otTracer) Init(opts ...tracer.Option) error { for _, o := range opts { - o(&ot.opts) + o(&t.opts) } - if tr, ok := ot.opts.Context.Value(tracerKey{}).(opentracing.Tracer); ok { - ot.tracer = tr + if tr, ok := t.opts.Context.Value(tracerKey{}).(ot.Tracer); ok { + t.tracer = tr } else { return errors.New("Tracer option missing") } @@ -39,32 +40,31 @@ func (ot *otTracer) Init(opts ...tracer.Option) error { return nil } -func (ot *otTracer) Start(ctx context.Context, name string, opts ...tracer.SpanOption) (context.Context, tracer.Span) { +func (t *otTracer) Start(ctx context.Context, name string, opts ...tracer.SpanOption) (context.Context, tracer.Span) { options := tracer.NewSpanOptions(opts...) - var span opentracing.Span + var span ot.Span switch options.Kind { - case tracer.SpanKindInternal, tracer.SpanKindUnspecified: - ctx, span = ot.startSpanFromContext(ctx, name) + case tracer.SpanKindUnspecified: + ctx, span = t.startSpanFromAny(ctx, name) + case tracer.SpanKindInternal: + ctx, span = t.startSpanFromContext(ctx, name) case tracer.SpanKindClient, tracer.SpanKindProducer: - ctx, span = ot.startSpanFromOutgoingContext(ctx, name) + ctx, span = t.startSpanFromOutgoingContext(ctx, name) case tracer.SpanKindServer, tracer.SpanKindConsumer: - ctx, span = ot.startSpanFromIncomingContext(ctx, ot.tracer, name) + ctx, span = t.startSpanFromIncomingContext(ctx, name) } - return ctx, &otSpan{span: span, opts: options} + sp := &otSpan{span: span, opts: options} + return tracer.NewSpanContext(ctx, sp), sp } type otSpan struct { - span opentracing.Span + span ot.Span opts tracer.SpanOptions status tracer.SpanStatus statusMsg string } func (os *otSpan) SetStatus(st tracer.SpanStatus, msg string) { - switch st { - case tracer.SpanStatusError: - os.span.SetTag("error", true) - } os.status = st os.statusMsg = msg } @@ -78,18 +78,37 @@ func (os *otSpan) Tracer() tracer.Tracer { } func (os *otSpan) Finish(opts ...tracer.SpanOption) { - if len(os.opts.Labels) > 0 { - os.span.LogKV(os.opts.Labels...) + if len(os.opts.Labels)%2 != 0 { + os.opts.Labels = os.opts.Labels[:len(os.opts.Labels)-1] + } + for idx := 0; idx < len(os.opts.Labels); idx += 2 { + switch os.opts.Labels[idx] { + case "err": + os.status = tracer.SpanStatusError + os.statusMsg = fmt.Sprintf("%v", os.opts.Labels[idx+1]) + case "error": + continue + case "X-Request-Id", "x-request-id": + os.span.SetTag("x-request-id", os.opts.Labels[idx+1]) + case "rpc.call", "rpc.call_type", "rpc.flavor", "span.kind", "sdk.database", "db.statement", "db.args", "args", "db.query", "query", "method": + os.span.SetTag(fmt.Sprintf("%v", os.opts.Labels[idx]), fmt.Sprintf("%v", os.opts.Labels[idx+1])) + default: + os.span.LogKV(os.opts.Labels[idx], os.opts.Labels[idx+1]) + } + } + if os.status == tracer.SpanStatusError { + os.span.SetTag("error", true) + os.span.LogKV("error", os.statusMsg) } os.span.Finish() } func (os *otSpan) AddEvent(name string, opts ...tracer.EventOption) { - os.span.LogFields(log.Event(name)) + os.span.LogFields(otlog.Event(name)) } func (os *otSpan) Context() context.Context { - return opentracing.ContextWithSpan(context.Background(), os.span) + return ot.ContextWithSpan(context.Background(), os.span) } func (os *otSpan) SetName(name string) { @@ -113,75 +132,141 @@ func NewTracer(opts ...tracer.Option) *otTracer { return &otTracer{opts: options} } -func spanFromContext(ctx context.Context) opentracing.Span { - return opentracing.SpanFromContext(ctx) +func spanFromContext(ctx context.Context) ot.Span { + return ot.SpanFromContext(ctx) } -func (ot *otTracer) startSpanFromContext(ctx context.Context, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span) { - if parentSpan := opentracing.SpanFromContext(ctx); parentSpan != nil { - opts = append(opts, opentracing.ChildOf(parentSpan.Context())) +func (t *otTracer) startSpanFromAny(ctx context.Context, name string, opts ...ot.StartSpanOption) (context.Context, ot.Span) { + if tracerSpan, ok := tracer.SpanFromContext(ctx); ok && tracerSpan != nil { + return t.startSpanFromContext(ctx, name, opts...) + } + + if otSpan := ot.SpanFromContext(ctx); otSpan != nil { + return t.startSpanFromContext(ctx, name, opts...) + } + + if md, ok := metadata.FromIncomingContext(ctx); ok && md != nil { + return t.startSpanFromIncomingContext(ctx, name, opts...) + } + + if md, ok := metadata.FromOutgoingContext(ctx); ok && md != nil { + return t.startSpanFromOutgoingContext(ctx, name, opts...) + } + + return t.startSpanFromContext(ctx, name, opts...) +} + +func (t *otTracer) startSpanFromContext(ctx context.Context, name string, opts ...ot.StartSpanOption) (context.Context, ot.Span) { + var parentSpan ot.Span + if tracerSpan, ok := tracer.SpanFromContext(ctx); ok && tracerSpan != nil { + if sp, ok := tracerSpan.(*otSpan); ok { + parentSpan = sp.span + } + } + if parentSpan == nil { + if otSpan := ot.SpanFromContext(ctx); otSpan != nil { + parentSpan = otSpan + } + } + + if parentSpan != nil { + opts = append(opts, ot.ChildOf(parentSpan.Context())) } md := metadata.New(1) - sp := ot.tracer.StartSpan(name, opts...) - if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(md)); err != nil { + sp := t.tracer.StartSpan(name, opts...) + if err := sp.Tracer().Inject(sp.Context(), ot.TextMap, ot.TextMapCarrier(md)); err != nil { return nil, nil } - ctx = opentracing.ContextWithSpan(ctx, sp) + ctx = ot.ContextWithSpan(ctx, sp) return ctx, sp } -func (ot *otTracer) startSpanFromOutgoingContext(ctx context.Context, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span) { - var parentCtx opentracing.SpanContext +func (t *otTracer) startSpanFromOutgoingContext(ctx context.Context, name string, opts ...ot.StartSpanOption) (context.Context, ot.Span) { + var parentSpan ot.Span + if tracerSpan, ok := tracer.SpanFromContext(ctx); ok && tracerSpan != nil { + if sp, ok := tracerSpan.(*otSpan); ok { + parentSpan = sp.span + } + } + if parentSpan == nil { + if otSpan := ot.SpanFromContext(ctx); otSpan != nil { + parentSpan = otSpan + } + } md, ok := metadata.FromOutgoingContext(ctx) - if ok && md != nil { - if spanCtx, err := ot.tracer.Extract(opentracing.TextMap, opentracing.TextMapCarrier(md)); err == nil && ok { - parentCtx = spanCtx - } - } - if parentCtx != nil { - opts = append(opts, opentracing.ChildOf(parentCtx)) + if parentSpan != nil { + opts = append(opts, ot.ChildOf(parentSpan.Context())) + } else { + var parentCtx ot.SpanContext + + if ok && md != nil { + if spanCtx, err := t.tracer.Extract(ot.TextMap, ot.TextMapCarrier(md)); err == nil && ok { + parentCtx = spanCtx + } + } + + if parentCtx != nil { + opts = append(opts, ot.ChildOf(parentCtx)) + } } nmd := metadata.Copy(md) - sp := ot.tracer.StartSpan(name, opts...) - if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(nmd)); err != nil { + sp := t.tracer.StartSpan(name, opts...) + if err := sp.Tracer().Inject(sp.Context(), ot.TextMap, ot.TextMapCarrier(nmd)); err != nil { return nil, nil } - ctx = metadata.NewOutgoingContext(opentracing.ContextWithSpan(ctx, sp), nmd) + ctx = metadata.NewOutgoingContext(ot.ContextWithSpan(ctx, sp), nmd) return ctx, sp } -func (ot *otTracer) startSpanFromIncomingContext(ctx context.Context, tracer opentracing.Tracer, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span) { - var parentCtx opentracing.SpanContext +func (t *otTracer) startSpanFromIncomingContext(ctx context.Context, name string, opts ...ot.StartSpanOption) (context.Context, ot.Span) { + var parentSpan ot.Span + if tracerSpan, ok := tracer.SpanFromContext(ctx); ok && tracerSpan != nil { + if sp, ok := tracerSpan.(*otSpan); ok { + parentSpan = sp.span + } + } + if parentSpan == nil { + if otSpan := ot.SpanFromContext(ctx); otSpan != nil { + parentSpan = otSpan + } + } md, ok := metadata.FromIncomingContext(ctx) - if ok && md != nil { - if spanCtx, err := tracer.Extract(opentracing.TextMap, opentracing.TextMapCarrier(md)); err == nil { - parentCtx = spanCtx - } - } - if parentCtx != nil { - opts = append(opts, opentracing.ChildOf(parentCtx)) + if parentSpan != nil { + opts = append(opts, ot.ChildOf(parentSpan.Context())) + } else { + var parentCtx ot.SpanContext + + if ok && md != nil { + if spanCtx, err := t.tracer.Extract(ot.TextMap, ot.TextMapCarrier(md)); err == nil { + parentCtx = spanCtx + } + } + + if parentCtx != nil { + opts = append(opts, ot.ChildOf(parentCtx)) + } } nmd := metadata.Copy(md) - sp := tracer.StartSpan(name, opts...) - if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(nmd)); err != nil { + sp := t.tracer.StartSpan(name, opts...) + if err := sp.Tracer().Inject(sp.Context(), ot.TextMap, ot.TextMapCarrier(nmd)); err != nil { return nil, nil } - ctx = metadata.NewIncomingContext(opentracing.ContextWithSpan(ctx, sp), nmd) + ctx = metadata.NewIncomingContext(ot.ContextWithSpan(ctx, sp), nmd) return ctx, sp } diff --git a/opentracing_test.go b/opentracing_test.go index 00e20a7..f6c0104 100644 --- a/opentracing_test.go +++ b/opentracing_test.go @@ -5,7 +5,7 @@ import ( "sync" "testing" - opentracing "github.com/opentracing/opentracing-go" + "github.com/opentracing/opentracing-go" "go.unistack.org/micro/v3/metadata" ) @@ -20,14 +20,16 @@ func TestStartSpanFromIncomingContext(t *testing.T) { ctx = metadata.NewIncomingContext(ctx, md) tracer := opentracing.GlobalTracer() - ot := &otTracer{tracer: tracer} g.Add(8000) cherr := make(chan error) for i := 0; i < 8000; i++ { go func() { defer g.Done() - _, sp := ot.startSpanFromIncomingContext(ctx, tracer, "test") + _, sp, err := startSpanFromIncomingContext(ctx, tracer, "test") + if err != nil { + cherr <- err + } sp.Finish() }() } -- 2.45.2