Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
2149a713d5 | |||
9b553cfa62 | |||
65ac3aff56 | |||
e9933df477 | |||
905910e8ce | |||
7948652293 | |||
54d9857bb9 | |||
1e9abb9905 | |||
6b9edf3593 | |||
742b86f128 | |||
474fc506dd |
9
go.mod
9
go.mod
@@ -1,10 +1,11 @@
|
||||
module go.unistack.org/micro-tracer-opentracing/v3
|
||||
|
||||
go 1.20
|
||||
go 1.21
|
||||
|
||||
toolchain go1.22.2
|
||||
|
||||
require (
|
||||
github.com/opentracing/opentracing-go v1.2.0
|
||||
go.unistack.org/micro/v3 v3.10.43
|
||||
go.opentelemetry.io/otel v1.28.0
|
||||
go.unistack.org/micro/v3 v3.10.81
|
||||
)
|
||||
|
||||
require github.com/stretchr/testify v1.8.1 // indirect
|
||||
|
22
go.sum
22
go.sum
@@ -1,21 +1,23 @@
|
||||
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/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
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.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.43 h1:GjUgu1lSKrhIov67jZQg6hUVUw0xZ6+ub8s6JRu6uj4=
|
||||
go.unistack.org/micro/v3 v3.10.43/go.mod h1:CSmEf5ddmft94MyKHnUSMM0W5dpmmTVbgImbgQWV5Ak=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
go.opentelemetry.io/otel v1.26.0 h1:LQwgL5s/1W7YiiRwxf03QGnWLb2HW4pLiAhaA5cZXBs=
|
||||
go.opentelemetry.io/otel v1.26.0/go.mod h1:UmLkJHUAidDval2EICqBMbnAd0/m2vmpf/dAM+fvFs4=
|
||||
go.opentelemetry.io/otel v1.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo=
|
||||
go.opentelemetry.io/otel v1.28.0/go.mod h1:q68ijF8Fc8CnMHKyzqL6akLO46ePnjkgfIMIjUIX9z4=
|
||||
go.unistack.org/micro/v3 v3.10.77 h1:nn731y84q3S8a4UgpVdC/wN9cs30ruZeH81rKw6XcUc=
|
||||
go.unistack.org/micro/v3 v3.10.77/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
|
||||
go.unistack.org/micro/v3 v3.10.81 h1:zHttMUEwL/f9GtrNAQOk8bK0NrEwAuKeUW8DEoywgJI=
|
||||
go.unistack.org/micro/v3 v3.10.81/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
ot "github.com/opentracing/opentracing-go"
|
||||
otlog "github.com/opentracing/opentracing-go/log"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.unistack.org/micro/v3/metadata"
|
||||
"go.unistack.org/micro/v3/tracer"
|
||||
rutil "go.unistack.org/micro/v3/util/reflect"
|
||||
@@ -48,6 +49,15 @@ type spanContext interface {
|
||||
|
||||
func (t *otTracer) Start(ctx context.Context, name string, opts ...tracer.SpanOption) (context.Context, tracer.Span) {
|
||||
options := tracer.NewSpanOptions(opts...)
|
||||
|
||||
if len(options.Labels)%2 != 0 {
|
||||
options.Labels = options.Labels[:len(options.Labels)-1]
|
||||
}
|
||||
|
||||
for _, fn := range t.opts.ContextAttrFuncs {
|
||||
options.Labels = append(options.Labels, fn(ctx)...)
|
||||
}
|
||||
|
||||
var span ot.Span
|
||||
switch options.Kind {
|
||||
case tracer.SpanKindUnspecified:
|
||||
@@ -93,6 +103,8 @@ type otSpan struct {
|
||||
opts tracer.SpanOptions
|
||||
status tracer.SpanStatus
|
||||
statusMsg string
|
||||
labels []interface{}
|
||||
finished bool
|
||||
}
|
||||
|
||||
func (os *otSpan) TraceID() string {
|
||||
@@ -117,41 +129,50 @@ func (os *otSpan) Tracer() tracer.Tracer {
|
||||
}
|
||||
|
||||
func (os *otSpan) Finish(opts ...tracer.SpanOption) {
|
||||
if len(os.opts.Labels)%2 != 0 {
|
||||
os.opts.Labels = os.opts.Labels[:len(os.opts.Labels)-1]
|
||||
if os.finished {
|
||||
return
|
||||
}
|
||||
os.opts.Labels = tracer.UniqLabels(os.opts.Labels)
|
||||
for idx := 0; idx < len(os.opts.Labels); idx += 2 {
|
||||
k, ok := os.opts.Labels[idx].(string)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
v, ok := os.opts.Labels[idx+1].(string)
|
||||
if !ok {
|
||||
v = fmt.Sprintf("%v", os.opts.Labels[idx+1])
|
||||
}
|
||||
switch k {
|
||||
case "err":
|
||||
os.status = tracer.SpanStatusError
|
||||
os.statusMsg = v
|
||||
case "error":
|
||||
continue
|
||||
case "X-Request-Id", "x-request-id":
|
||||
os.span.SetTag("x-request-id", v)
|
||||
case "rpc.call", "rpc.call_type", "rpc.flavor", "rpc.service", "rpc.method",
|
||||
"sdk.database", "db.statement", "db.args", "db.query", "db.method",
|
||||
"messaging.destination.name", "messaging.source.name", "messaging.operation":
|
||||
os.span.SetTag(k, v)
|
||||
default:
|
||||
os.span.LogKV(k, v)
|
||||
|
||||
options := os.opts
|
||||
|
||||
options.Status = os.status
|
||||
options.StatusMsg = os.statusMsg
|
||||
options.Labels = append(options.Labels, os.labels...)
|
||||
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
if !options.Record {
|
||||
return
|
||||
}
|
||||
|
||||
if len(options.Labels)%2 != 0 {
|
||||
options.Labels = options.Labels[:len(options.Labels)-1]
|
||||
}
|
||||
|
||||
l := len(options.Labels)
|
||||
for idx := 0; idx < l; idx++ {
|
||||
switch lt := options.Labels[idx].(type) {
|
||||
case attribute.KeyValue:
|
||||
os.span.SetTag(string(lt.Key), lt.Value.AsInterface())
|
||||
case string:
|
||||
if l > idx+1 {
|
||||
os.span.SetTag(lt, options.Labels[idx+1])
|
||||
idx++
|
||||
}
|
||||
}
|
||||
}
|
||||
if os.status == tracer.SpanStatusError {
|
||||
|
||||
if options.Status == tracer.SpanStatusError {
|
||||
os.span.SetTag("error", true)
|
||||
os.span.LogKV("error", os.statusMsg)
|
||||
os.span.LogKV("error", options.StatusMsg)
|
||||
}
|
||||
os.span.SetTag("span.kind", os.opts.Kind)
|
||||
|
||||
os.span.SetTag("span.kind", options.Kind)
|
||||
os.span.Finish()
|
||||
|
||||
os.finished = true
|
||||
}
|
||||
|
||||
func (os *otSpan) AddEvent(name string, opts ...tracer.EventOption) {
|
||||
@@ -179,7 +200,7 @@ func (os *otSpan) Kind() tracer.SpanKind {
|
||||
}
|
||||
|
||||
func (os *otSpan) AddLabels(labels ...interface{}) {
|
||||
os.opts.Labels = append(os.opts.Labels, labels...)
|
||||
os.labels = append(os.labels, labels...)
|
||||
}
|
||||
|
||||
func NewTracer(opts ...tracer.Option) *otTracer {
|
||||
|
@@ -2,6 +2,7 @@ package opentracing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/opentracing/opentracing-go/mocktracer"
|
||||
@@ -30,3 +31,31 @@ func TestTraceID(t *testing.T) {
|
||||
t.Fatalf("invalid span span id %#+v", v)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTraceTags(t *testing.T) {
|
||||
md := metadata.New(1)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
ctx = metadata.NewIncomingContext(ctx, md)
|
||||
|
||||
mtr := mocktracer.New()
|
||||
tr := NewTracer(Tracer(mtr))
|
||||
if err := tr.Init(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var sp tracer.Span
|
||||
|
||||
ctx, sp = tr.Start(ctx, "test", tracer.WithSpanLabels("key", "val", "odd"))
|
||||
sp.Finish(tracer.WithSpanLabels("xkey", "xval"))
|
||||
_ = ctx
|
||||
msp := mtr.FinishedSpans()[0]
|
||||
|
||||
if "val" != fmt.Sprintf("%v", msp.Tags()["key"]) {
|
||||
t.Fatal("mock span invalid")
|
||||
}
|
||||
|
||||
if "xval" != fmt.Sprintf("%v", msp.Tags()["xkey"]) {
|
||||
t.Fatalf("mock span invalid %#+v", msp)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user