diff --git a/go.sum b/go.sum index 68e829a..c2a5e84 100644 --- a/go.sum +++ b/go.sum @@ -11,12 +11,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ 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.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= diff --git a/opentracing.go b/opentracing.go index 1bfaff7..1849a57 100644 --- a/opentracing.go +++ b/opentracing.go @@ -3,14 +3,13 @@ package opentracing import ( "context" "errors" - "fmt" + "strings" 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" ) var _ tracer.Tracer = &otTracer{} @@ -42,7 +41,12 @@ func (t *otTracer) Init(opts ...tracer.Option) error { return nil } -type spanContext interface { +type otSpanContext interface { + TraceID() idStringer + SpanID() idStringer +} + +type jSpanContext interface { TraceID() idStringer SpanID() idStringer } @@ -73,18 +77,35 @@ func (t *otTracer) Start(ctx context.Context, name string, opts ...tracer.SpanOp sp := &otSpan{span: span, opts: options} spctx := span.Context() - if v, ok := spctx.(spanContext); ok { - sp.traceID = v.TraceID().String() - sp.spanID = v.SpanID().String() + + var traceID, spanID, parentID string + + if v, ok := spctx.(otSpanContext); ok { + traceID = v.TraceID().String() + spanID = v.SpanID().String() } else { - if val, err := rutil.StructFieldByName(spctx, "TraceID"); err == nil { - sp.traceID = fmt.Sprintf("%v", val) - } - if val, err := rutil.StructFieldByName(spctx, "SpanID"); err == nil { - sp.spanID = fmt.Sprintf("%v", val) + carrier := make(map[string]string, 1) + _ = span.Tracer().Inject(spctx, ot.TextMap, ot.TextMapCarrier(carrier)) + for k, v := range carrier { + switch k { + 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 } @@ -100,6 +121,7 @@ type otSpan struct { span ot.Span spanID string traceID string + parentID string opts tracer.SpanOptions status tracer.SpanStatus statusMsg string diff --git a/opentracing_test.go b/opentracing_test.go index 4f8545f..0b0ce05 100644 --- a/opentracing_test.go +++ b/opentracing_test.go @@ -6,11 +6,13 @@ import ( "testing" "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/tracer" ) -func TestTraceID(t *testing.T) { +func TestNoopTraceID(t *testing.T) { md := metadata.New(1) ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -30,8 +32,62 @@ func TestTraceID(t *testing.T) { 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 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())