Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
f9dad57eb2 | |||
59283b64c9 | |||
4805482921 | |||
c2225cb92a |
6
go.mod
6
go.mod
@@ -1,10 +1,10 @@
|
||||
module go.unistack.org/micro-tracer-opentracing/v4
|
||||
module go.unistack.org/micro-tracer-opentracing/v3
|
||||
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/opentracing/opentracing-go v1.2.0
|
||||
go.unistack.org/micro/v4 v4.0.13
|
||||
go.unistack.org/micro/v3 v3.10.25
|
||||
)
|
||||
|
||||
require github.com/stretchr/testify v1.8.3 // indirect
|
||||
require github.com/stretchr/testify v1.8.1 // indirect
|
||||
|
16
go.sum
16
go.sum
@@ -1,13 +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/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.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=
|
||||
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.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=
|
||||
|
@@ -7,9 +7,8 @@ import (
|
||||
|
||||
ot "github.com/opentracing/opentracing-go"
|
||||
otlog "github.com/opentracing/opentracing-go/log"
|
||||
"go.unistack.org/micro/v4/metadata"
|
||||
"go.unistack.org/micro/v4/options"
|
||||
"go.unistack.org/micro/v4/tracer"
|
||||
"go.unistack.org/micro/v3/metadata"
|
||||
"go.unistack.org/micro/v3/tracer"
|
||||
)
|
||||
|
||||
var _ tracer.Tracer = &otTracer{}
|
||||
@@ -27,7 +26,7 @@ func (t *otTracer) Flush(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *otTracer) Init(opts ...options.Option) error {
|
||||
func (t *otTracer) Init(opts ...tracer.Option) error {
|
||||
for _, o := range opts {
|
||||
o(&t.opts)
|
||||
}
|
||||
@@ -41,7 +40,7 @@ func (t *otTracer) Init(opts ...options.Option) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *otTracer) Start(ctx context.Context, name string, opts ...options.Option) (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 ot.Span
|
||||
switch options.Kind {
|
||||
@@ -65,25 +64,6 @@ type otSpan struct {
|
||||
statusMsg string
|
||||
}
|
||||
|
||||
type spanContext interface {
|
||||
TraceID() fmt.Stringer
|
||||
SpanID() fmt.Stringer
|
||||
}
|
||||
|
||||
func (os *otSpan) TraceID() string {
|
||||
if spanctx, ok := os.span.Context().(spanContext); ok {
|
||||
return spanctx.TraceID().String()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (os *otSpan) SpanID() string {
|
||||
if spanctx, ok := os.span.Context().(spanContext); ok {
|
||||
return spanctx.SpanID().String()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (os *otSpan) SetStatus(st tracer.SpanStatus, msg string) {
|
||||
os.status = st
|
||||
os.statusMsg = msg
|
||||
@@ -97,15 +77,10 @@ func (os *otSpan) Tracer() tracer.Tracer {
|
||||
return &otTracer{tracer: os.span.Tracer()}
|
||||
}
|
||||
|
||||
func (os *otSpan) AddLogs(kv ...interface{}) {
|
||||
os.span.LogKV(kv...)
|
||||
}
|
||||
|
||||
func (os *otSpan) Finish(opts ...options.Option) {
|
||||
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]
|
||||
}
|
||||
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":
|
||||
@@ -115,10 +90,8 @@ func (os *otSpan) Finish(opts ...options.Option) {
|
||||
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])
|
||||
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])
|
||||
}
|
||||
@@ -127,11 +100,10 @@ func (os *otSpan) Finish(opts ...options.Option) {
|
||||
os.span.SetTag("error", true)
|
||||
os.span.LogKV("error", os.statusMsg)
|
||||
}
|
||||
os.span.SetTag("span.kind", os.opts.Kind)
|
||||
os.span.Finish()
|
||||
}
|
||||
|
||||
func (os *otSpan) AddEvent(name string, opts ...options.Option) {
|
||||
func (os *otSpan) AddEvent(name string, opts ...tracer.EventOption) {
|
||||
os.span.LogFields(otlog.Event(name))
|
||||
}
|
||||
|
||||
@@ -155,7 +127,7 @@ func (os *otSpan) AddLabels(labels ...interface{}) {
|
||||
os.opts.Labels = append(os.opts.Labels, labels...)
|
||||
}
|
||||
|
||||
func NewTracer(opts ...options.Option) *otTracer {
|
||||
func NewTracer(opts ...tracer.Option) *otTracer {
|
||||
options := tracer.NewOptions(opts...)
|
||||
return &otTracer{opts: options}
|
||||
}
|
||||
|
50
opentracing_test.go
Normal file
50
opentracing_test.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package opentracing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/opentracing/opentracing-go"
|
||||
"go.unistack.org/micro/v3/metadata"
|
||||
)
|
||||
|
||||
func TestStartSpanFromIncomingContext(t *testing.T) {
|
||||
md := metadata.New(2)
|
||||
md.Set("key", "val")
|
||||
|
||||
var g sync.WaitGroup
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
ctx = metadata.NewIncomingContext(ctx, md)
|
||||
|
||||
tracer := opentracing.GlobalTracer()
|
||||
|
||||
g.Add(8000)
|
||||
cherr := make(chan error)
|
||||
for i := 0; i < 8000; i++ {
|
||||
go func() {
|
||||
defer g.Done()
|
||||
_, sp, err := startSpanFromIncomingContext(ctx, tracer, "test")
|
||||
if err != nil {
|
||||
cherr <- err
|
||||
}
|
||||
sp.Finish()
|
||||
}()
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
default:
|
||||
g.Wait()
|
||||
close(cherr)
|
||||
case err, ok := <-cherr:
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
} else if !ok {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -2,11 +2,11 @@ package opentracing
|
||||
|
||||
import (
|
||||
"github.com/opentracing/opentracing-go"
|
||||
"go.unistack.org/micro/v4/options"
|
||||
"go.unistack.org/micro/v3/tracer"
|
||||
)
|
||||
|
||||
type tracerKey struct{}
|
||||
|
||||
func Tracer(ot opentracing.Tracer) options.Option {
|
||||
return options.ContextOption(tracerKey{}, ot)
|
||||
func Tracer(ot opentracing.Tracer) tracer.Option {
|
||||
return tracer.SetOption(tracerKey{}, ot)
|
||||
}
|
||||
|
Reference in New Issue
Block a user