Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
eeef4515ab | |||
a53b3b08bf | |||
123023be06 | |||
c2f6dfd267 | |||
acd0034624 | |||
b32b60d136 | |||
66d1c9d811 | |||
a1838a8070 | |||
9084c3134c | |||
2fb5693d97 | |||
57e07c302c |
7
go.mod
7
go.mod
@@ -1,9 +1,10 @@
|
||||
module go.unistack.org/micro-tracer-opentracing/v3
|
||||
module go.unistack.org/micro-tracer-opentracing/v4
|
||||
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/opentracing/opentracing-go v1.2.0
|
||||
go.opentelemetry.io/otel v1.24.0
|
||||
go.unistack.org/micro/v3 v3.10.43
|
||||
go.unistack.org/micro/v4 v4.0.13
|
||||
)
|
||||
|
||||
require github.com/stretchr/testify v1.8.3 // indirect
|
||||
|
10
go.sum
10
go.sum
@@ -1,15 +1,13 @@
|
||||
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/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
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/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo=
|
||||
go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo=
|
||||
go.unistack.org/micro/v3 v3.10.43 h1:GjUgu1lSKrhIov67jZQg6hUVUw0xZ6+ub8s6JRu6uj4=
|
||||
go.unistack.org/micro/v3 v3.10.43/go.mod h1:CSmEf5ddmft94MyKHnUSMM0W5dpmmTVbgImbgQWV5Ak=
|
||||
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
|
||||
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
go.unistack.org/micro/v4 v4.0.13 h1:wyprk+KNAv/zR6bri6p6ucmXcJ9WyZ/vzru4zjPd8dA=
|
||||
go.unistack.org/micro/v4 v4.0.13/go.mod h1:ZDgU9931vm2l7X6RN/6UuwRIVp24GRdmQ7dKmegArk4=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
122
opentracing.go
122
opentracing.go
@@ -7,10 +7,9 @@ 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"
|
||||
"go.unistack.org/micro/v4/metadata"
|
||||
"go.unistack.org/micro/v4/options"
|
||||
"go.unistack.org/micro/v4/tracer"
|
||||
)
|
||||
|
||||
var _ tracer.Tracer = &otTracer{}
|
||||
@@ -28,7 +27,7 @@ func (t *otTracer) Flush(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *otTracer) Init(opts ...tracer.Option) error {
|
||||
func (t *otTracer) Init(opts ...options.Option) error {
|
||||
for _, o := range opts {
|
||||
o(&t.opts)
|
||||
}
|
||||
@@ -42,12 +41,7 @@ func (t *otTracer) Init(opts ...tracer.Option) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type spanContext interface {
|
||||
TraceID() 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 ...options.Option) (context.Context, tracer.Span) {
|
||||
options := tracer.NewSpanOptions(opts...)
|
||||
var span ot.Span
|
||||
switch options.Kind {
|
||||
@@ -60,48 +54,34 @@ func (t *otTracer) Start(ctx context.Context, name string, opts ...tracer.SpanOp
|
||||
case tracer.SpanKindServer, tracer.SpanKindConsumer:
|
||||
ctx, span = t.startSpanFromIncomingContext(ctx, name)
|
||||
}
|
||||
|
||||
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()
|
||||
} 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)
|
||||
}
|
||||
}
|
||||
|
||||
return tracer.NewSpanContext(ctx, sp), sp
|
||||
}
|
||||
|
||||
type idStringer struct {
|
||||
s string
|
||||
}
|
||||
|
||||
func (s idStringer) String() string {
|
||||
return s.s
|
||||
}
|
||||
|
||||
type otSpan struct {
|
||||
span ot.Span
|
||||
spanID string
|
||||
traceID string
|
||||
opts tracer.SpanOptions
|
||||
status tracer.SpanStatus
|
||||
statusMsg string
|
||||
}
|
||||
|
||||
type spanContext interface {
|
||||
TraceID() fmt.Stringer
|
||||
SpanID() fmt.Stringer
|
||||
}
|
||||
|
||||
func (os *otSpan) TraceID() string {
|
||||
return os.traceID
|
||||
if spanctx, ok := os.span.Context().(spanContext); ok {
|
||||
return spanctx.TraceID().String()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (os *otSpan) SpanID() string {
|
||||
return os.spanID
|
||||
if spanctx, ok := os.span.Context().(spanContext); ok {
|
||||
return spanctx.SpanID().String()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (os *otSpan) SetStatus(st tracer.SpanStatus, msg string) {
|
||||
@@ -117,41 +97,44 @@ func (os *otSpan) Tracer() tracer.Tracer {
|
||||
return &otTracer{tracer: os.span.Tracer()}
|
||||
}
|
||||
|
||||
func (os *otSpan) Finish(opts ...tracer.SpanOption) {
|
||||
options := os.opts
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
func (os *otSpan) AddLogs(kv ...interface{}) {
|
||||
os.span.LogKV(kv...)
|
||||
}
|
||||
|
||||
l := len(options.Labels)
|
||||
for idx := 0; idx < len(options.Labels); 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++
|
||||
}
|
||||
func (os *otSpan) Finish(opts ...options.Option) {
|
||||
if len(os.opts.Labels)%2 != 0 {
|
||||
os.opts.Labels = os.opts.Labels[:len(os.opts.Labels)-1]
|
||||
}
|
||||
os.opts.Labels = tracer.UniqLabels(os.opts.Labels)
|
||||
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", "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(fmt.Sprintf("%v", os.opts.Labels[idx]), 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.SetTag("span.kind", options.Kind)
|
||||
os.span.SetTag("span.kind", os.opts.Kind)
|
||||
os.span.Finish()
|
||||
}
|
||||
|
||||
func (os *otSpan) AddEvent(name string, opts ...tracer.EventOption) {
|
||||
func (os *otSpan) AddEvent(name string, opts ...options.Option) {
|
||||
os.span.LogFields(otlog.Event(name))
|
||||
}
|
||||
|
||||
func (os *otSpan) AddLogs(kv ...interface{}) {
|
||||
os.span.LogKV(kv...)
|
||||
}
|
||||
|
||||
func (os *otSpan) Context() context.Context {
|
||||
return ot.ContextWithSpan(context.Background(), os.span)
|
||||
}
|
||||
@@ -169,25 +152,18 @@ func (os *otSpan) Kind() tracer.SpanKind {
|
||||
}
|
||||
|
||||
func (os *otSpan) AddLabels(labels ...interface{}) {
|
||||
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++
|
||||
}
|
||||
}
|
||||
}
|
||||
os.opts.Labels = append(os.opts.Labels, labels...)
|
||||
}
|
||||
|
||||
func NewTracer(opts ...tracer.Option) *otTracer {
|
||||
func NewTracer(opts ...options.Option) *otTracer {
|
||||
options := tracer.NewOptions(opts...)
|
||||
return &otTracer{opts: options}
|
||||
}
|
||||
|
||||
func spanFromContext(ctx context.Context) ot.Span {
|
||||
return ot.SpanFromContext(ctx)
|
||||
}
|
||||
|
||||
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...)
|
||||
|
@@ -1,32 +0,0 @@
|
||||
package opentracing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/opentracing/opentracing-go/mocktracer"
|
||||
"go.unistack.org/micro/v3/metadata"
|
||||
"go.unistack.org/micro/v3/tracer"
|
||||
)
|
||||
|
||||
func TestTraceID(t *testing.T) {
|
||||
md := metadata.New(1)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
ctx = metadata.NewIncomingContext(ctx, md)
|
||||
|
||||
tr := NewTracer(Tracer(mocktracer.New()))
|
||||
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)
|
||||
}
|
||||
}
|
@@ -2,11 +2,11 @@ package opentracing
|
||||
|
||||
import (
|
||||
"github.com/opentracing/opentracing-go"
|
||||
"go.unistack.org/micro/v3/tracer"
|
||||
"go.unistack.org/micro/v4/options"
|
||||
)
|
||||
|
||||
type tracerKey struct{}
|
||||
|
||||
func Tracer(ot opentracing.Tracer) tracer.Option {
|
||||
return tracer.SetOption(tracerKey{}, ot)
|
||||
func Tracer(ot opentracing.Tracer) options.Option {
|
||||
return options.ContextOption(tracerKey{}, ot)
|
||||
}
|
||||
|
Reference in New Issue
Block a user