Compare commits

..

No commits in common. "v3" and "v3.10.13" have entirely different histories.
v3 ... v3.10.13

4 changed files with 38 additions and 192 deletions

8
go.mod
View File

@ -1,11 +1,9 @@
module go.unistack.org/micro-tracer-opentracing/v3 module go.unistack.org/micro-tracer-opentracing/v3
go 1.21 go 1.20
toolchain go1.22.2
require ( require (
github.com/opentracing/opentracing-go v1.2.0 github.com/opentracing/opentracing-go v1.2.0
go.opentelemetry.io/otel v1.28.0 go.opentelemetry.io/otel v1.24.0
go.unistack.org/micro/v3 v3.10.81 go.unistack.org/micro/v3 v3.10.43
) )

14
go.sum
View File

@ -1,19 +1,15 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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 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 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 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= 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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 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.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo=
go.opentelemetry.io/otel v1.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo= go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo=
go.opentelemetry.io/otel v1.28.0/go.mod h1:q68ijF8Fc8CnMHKyzqL6akLO46ePnjkgfIMIjUIX9z4= go.unistack.org/micro/v3 v3.10.43 h1:GjUgu1lSKrhIov67jZQg6hUVUw0xZ6+ub8s6JRu6uj4=
go.unistack.org/micro/v3 v3.10.81 h1:zHttMUEwL/f9GtrNAQOk8bK0NrEwAuKeUW8DEoywgJI= go.unistack.org/micro/v3 v3.10.43/go.mod h1:CSmEf5ddmft94MyKHnUSMM0W5dpmmTVbgImbgQWV5Ak=
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 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -3,14 +3,14 @@ package opentracing
import ( import (
"context" "context"
"errors" "errors"
"strings" "fmt"
ot "github.com/opentracing/opentracing-go" ot "github.com/opentracing/opentracing-go"
otlog "github.com/opentracing/opentracing-go/log" otlog "github.com/opentracing/opentracing-go/log"
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
"go.unistack.org/micro/v3/metadata" "go.unistack.org/micro/v3/metadata"
"go.unistack.org/micro/v3/tracer" "go.unistack.org/micro/v3/tracer"
"go.unistack.org/micro/v3/util/sort" rutil "go.unistack.org/micro/v3/util/reflect"
) )
var _ tracer.Tracer = &otTracer{} var _ tracer.Tracer = &otTracer{}
@ -42,27 +42,13 @@ func (t *otTracer) Init(opts ...tracer.Option) error {
return nil return nil
} }
type otSpanContext interface { type spanContext interface {
TraceID() idStringer
SpanID() idStringer
}
type jSpanContext interface {
TraceID() idStringer TraceID() idStringer
SpanID() idStringer SpanID() idStringer
} }
func (t *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...) 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 var span ot.Span
switch options.Kind { switch options.Kind {
case tracer.SpanKindUnspecified: case tracer.SpanKindUnspecified:
@ -78,40 +64,17 @@ func (t *otTracer) Start(ctx context.Context, name string, opts ...tracer.SpanOp
sp := &otSpan{span: span, opts: options} sp := &otSpan{span: span, opts: options}
spctx := span.Context() spctx := span.Context()
if v, ok := spctx.(spanContext); ok {
var traceID, spanID, parentID string sp.traceID = v.TraceID().String()
sp.spanID = v.SpanID().String()
if v, ok := spctx.(otSpanContext); ok {
traceID = v.TraceID().String()
spanID = v.SpanID().String()
} else { } else {
carrier := make(map[string]string, 1) if val, err := rutil.StructFieldByName(spctx, "TraceID"); err == nil {
_ = span.Tracer().Inject(spctx, ot.TextMap, ot.TextMapCarrier(carrier)) sp.traceID = fmt.Sprintf("%v", val)
for k, v := range carrier { }
switch k { if val, err := rutil.StructFieldByName(spctx, "SpanID"); err == nil {
case "mockpfx-ids-sampled": sp.spanID = fmt.Sprintf("%v", val)
continue
case "mockpfx-ids-spanid":
spanID = v
case "mockpfx-ids-traceid":
traceID = v
default: // reasonable default
p := strings.Split(v, ":")
traceID = p[0]
spanID = p[1]
parentID = p[2]
case "uber-trace-id": // jaeger trace span
p := strings.Split(v, ":")
traceID = p[0]
spanID = p[1]
parentID = p[2]
} }
} }
}
sp.traceID = traceID
sp.spanID = spanID
sp.parentID = parentID
return tracer.NewSpanContext(ctx, sp), sp return tracer.NewSpanContext(ctx, sp), sp
} }
@ -128,12 +91,9 @@ type otSpan struct {
span ot.Span span ot.Span
spanID string spanID string
traceID string traceID string
parentID string
opts tracer.SpanOptions opts tracer.SpanOptions
status tracer.SpanStatus status tracer.SpanStatus
statusMsg string statusMsg string
labels []interface{}
finished bool
} }
func (os *otSpan) TraceID() string { func (os *otSpan) TraceID() string {
@ -144,10 +104,6 @@ func (os *otSpan) SpanID() string {
return os.spanID return os.spanID
} }
func (os *otSpan) ParentID() string {
return os.parentID
}
func (os *otSpan) SetStatus(st tracer.SpanStatus, msg string) { func (os *otSpan) SetStatus(st tracer.SpanStatus, msg string) {
os.status = st os.status = st
os.statusMsg = msg os.statusMsg = msg
@ -162,52 +118,30 @@ func (os *otSpan) Tracer() tracer.Tracer {
} }
func (os *otSpan) Finish(opts ...tracer.SpanOption) { func (os *otSpan) Finish(opts ...tracer.SpanOption) {
if os.finished {
return
}
options := os.opts options := os.opts
options.Status = os.status
options.StatusMsg = os.statusMsg
options.Labels = append(options.Labels, os.labels...)
for _, o := range opts { for _, o := range opts {
o(&options) o(&options)
} }
if !options.Record {
return
}
if len(options.Labels)%2 != 0 {
options.Labels = options.Labels[:len(options.Labels)-1]
}
os.opts.Labels = sort.Uniq(os.opts.Labels)
l := len(options.Labels) l := len(options.Labels)
for idx := 0; idx < l; idx++ { for idx := 0; idx < len(options.Labels); idx++ {
switch lt := options.Labels[idx].(type) { switch lt := options.Labels[idx].(type) {
case attribute.KeyValue: case attribute.KeyValue:
os.span.SetTag(string(lt.Key), lt.Value.AsInterface()) os.span.SetTag(string(lt.Key), lt.Value.AsInterface())
case string: case string:
if l > idx+1 { if l < idx+1 {
os.span.SetTag(lt, options.Labels[idx+1]) os.span.SetTag(lt, options.Labels[idx+1])
idx++ idx++
} }
} }
} }
if options.Status == tracer.SpanStatusError { if os.status == tracer.SpanStatusError {
os.span.SetTag("error", true) os.span.SetTag("error", true)
os.span.LogKV("error", options.StatusMsg) os.span.LogKV("error", os.statusMsg)
} }
os.span.SetTag("span.kind", options.Kind) os.span.SetTag("span.kind", options.Kind)
os.span.Finish() os.span.Finish()
os.finished = true
} }
func (os *otSpan) AddEvent(name string, opts ...tracer.EventOption) { func (os *otSpan) AddEvent(name string, opts ...tracer.EventOption) {
@ -235,7 +169,18 @@ func (os *otSpan) Kind() tracer.SpanKind {
} }
func (os *otSpan) AddLabels(labels ...interface{}) { func (os *otSpan) AddLabels(labels ...interface{}) {
os.labels = append(os.labels, labels...) l := len(labels)
for idx := 0; idx < len(labels); idx++ {
switch lt := 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, labels[idx+1])
idx++
}
}
}
} }
func NewTracer(opts ...tracer.Option) *otTracer { func NewTracer(opts ...tracer.Option) *otTracer {
@ -324,15 +269,11 @@ func (t *otTracer) startSpanFromOutgoingContext(ctx context.Context, name string
} }
nmd := metadata.Copy(md) nmd := metadata.Copy(md)
smd := metadata.New(1)
sp := t.tracer.StartSpan(name, opts...) sp := t.tracer.StartSpan(name, opts...)
if err := sp.Tracer().Inject(sp.Context(), ot.TextMap, ot.TextMapCarrier(smd)); err != nil { if err := sp.Tracer().Inject(sp.Context(), ot.TextMap, ot.TextMapCarrier(nmd)); err != nil {
return nil, nil return nil, nil
} }
for k, v := range smd {
nmd.Set(k, v)
}
ctx = metadata.NewOutgoingContext(ot.ContextWithSpan(ctx, sp), nmd) ctx = metadata.NewOutgoingContext(ot.ContextWithSpan(ctx, sp), nmd)
@ -371,15 +312,11 @@ func (t *otTracer) startSpanFromIncomingContext(ctx context.Context, name string
} }
nmd := metadata.Copy(md) nmd := metadata.Copy(md)
smd := metadata.New(1)
sp := t.tracer.StartSpan(name, opts...) sp := t.tracer.StartSpan(name, opts...)
if err := sp.Tracer().Inject(sp.Context(), ot.TextMap, ot.TextMapCarrier(smd)); err != nil { if err := sp.Tracer().Inject(sp.Context(), ot.TextMap, ot.TextMapCarrier(nmd)); err != nil {
return nil, nil return nil, nil
} }
for k, v := range smd {
nmd.Set(k, v)
}
ctx = metadata.NewIncomingContext(ot.ContextWithSpan(ctx, sp), nmd) ctx = metadata.NewIncomingContext(ot.ContextWithSpan(ctx, sp), nmd)

View File

@ -2,17 +2,14 @@ package opentracing
import ( import (
"context" "context"
"fmt"
"testing" "testing"
"github.com/opentracing/opentracing-go/mocktracer" "github.com/opentracing/opentracing-go/mocktracer"
// jconfig "github.com/uber/jaeger-client-go/config"
"go.unistack.org/micro/v3/logger/slog"
"go.unistack.org/micro/v3/metadata" "go.unistack.org/micro/v3/metadata"
"go.unistack.org/micro/v3/tracer" "go.unistack.org/micro/v3/tracer"
) )
func TestNoopTraceID(t *testing.T) { func TestTraceID(t *testing.T) {
md := metadata.New(1) md := metadata.New(1)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
@ -32,86 +29,4 @@ func TestNoopTraceID(t *testing.T) {
if v := sp.SpanID(); v != "44" { if v := sp.SpanID(); v != "44" {
t.Fatalf("invalid span span id %#+v", v) t.Fatalf("invalid span span id %#+v", v)
} }
l := slog.NewLogger()
if err := l.Init(); err != nil {
t.Fatal(err)
}
l.Info(ctx, "msg")
}
/*
func TestRealTraceID(t *testing.T) {
md := metadata.New(1)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx = metadata.NewIncomingContext(ctx, md)
jcfg := &jconfig.Configuration{
ServiceName: "test",
Sampler: &jconfig.SamplerConfig{
Type: "const",
Param: 1,
},
Reporter: &jconfig.ReporterConfig{
LogSpans: true,
QueueSize: 100,
},
}
jtr, closer, err := jcfg.NewTracer()
if err != nil {
t.Fatal(err)
}
defer closer.Close()
tr := NewTracer(Tracer(jtr))
if err := tr.Init(); err != nil {
t.Fatal(err)
}
var sp tracer.Span
ctx, sp = tr.Start(ctx, "test")
if v := sp.TraceID(); v != "43" {
t.Fatalf("invalid span trace id %#+v", v)
}
if v := sp.SpanID(); v != "44" {
t.Fatalf("invalid span span id %#+v", v)
}
l := slog.NewLogger()
if err := l.Init(); err != nil {
t.Fatal(err)
}
l.Info(ctx, "msg")
}
*/
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)
}
} }