Compare commits
23 Commits
Author | SHA1 | Date | |
---|---|---|---|
31350e8c21 | |||
|
d3d5925747 | ||
89f75e0431 | |||
9f6620676a | |||
e1f85545d9 | |||
e887edb475 | |||
2149a713d5 | |||
9b553cfa62 | |||
65ac3aff56 | |||
e9933df477 | |||
905910e8ce | |||
7948652293 | |||
54d9857bb9 | |||
1e9abb9905 | |||
6b9edf3593 | |||
742b86f128 | |||
474fc506dd | |||
c94af432a0 | |||
3f9704aef8 | |||
5224363bfc | |||
52ec3cf264 | |||
d2b9201423 | |||
6543196faf |
9
go.mod
9
go.mod
@ -1,10 +1,11 @@
|
|||||||
module go.unistack.org/micro-tracer-opentracing/v3
|
module go.unistack.org/micro-tracer-opentracing/v3
|
||||||
|
|
||||||
go 1.20
|
go 1.21
|
||||||
|
|
||||||
|
toolchain go1.22.2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/opentracing/opentracing-go v1.2.0
|
github.com/opentracing/opentracing-go v1.2.0
|
||||||
go.unistack.org/micro/v3 v3.10.25
|
go.opentelemetry.io/otel v1.28.0
|
||||||
|
go.unistack.org/micro/v3 v3.10.81
|
||||||
)
|
)
|
||||||
|
|
||||||
require github.com/stretchr/testify v1.8.1 // indirect
|
|
||||||
|
18
go.sum
18
go.sum
@ -1,21 +1,19 @@
|
|||||||
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/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 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/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.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.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
go.opentelemetry.io/otel v1.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo=
|
||||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
go.opentelemetry.io/otel v1.28.0/go.mod h1:q68ijF8Fc8CnMHKyzqL6akLO46ePnjkgfIMIjUIX9z4=
|
||||||
go.unistack.org/micro/v3 v3.10.25 h1:A0epdZHOqjnXx103wwFhPKgmvVVbScvfbmn3HmHz1wE=
|
go.unistack.org/micro/v3 v3.10.81 h1:zHttMUEwL/f9GtrNAQOk8bK0NrEwAuKeUW8DEoywgJI=
|
||||||
go.unistack.org/micro/v3 v3.10.25/go.mod h1:ALkeXpqChYDjx8KPi7tz9mmIyOnob6nlNswsg8BnZjQ=
|
go.unistack.org/micro/v3 v3.10.81/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
|
||||||
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 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
165
opentracing.go
165
opentracing.go
@ -3,12 +3,14 @@ package opentracing
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"strings"
|
||||||
|
|
||||||
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.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"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ tracer.Tracer = &otTracer{}
|
var _ tracer.Tracer = &otTracer{}
|
||||||
@ -40,8 +42,27 @@ func (t *otTracer) Init(opts ...tracer.Option) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type otSpanContext interface {
|
||||||
|
TraceID() idStringer
|
||||||
|
SpanID() idStringer
|
||||||
|
}
|
||||||
|
|
||||||
|
type jSpanContext 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 ...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:
|
||||||
@ -53,15 +74,78 @@ func (t *otTracer) Start(ctx context.Context, name string, opts ...tracer.SpanOp
|
|||||||
case tracer.SpanKindServer, tracer.SpanKindConsumer:
|
case tracer.SpanKindServer, tracer.SpanKindConsumer:
|
||||||
ctx, span = t.startSpanFromIncomingContext(ctx, name)
|
ctx, span = t.startSpanFromIncomingContext(ctx, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
sp := &otSpan{span: span, opts: options}
|
sp := &otSpan{span: span, opts: options}
|
||||||
|
|
||||||
|
spctx := span.Context()
|
||||||
|
|
||||||
|
var traceID, spanID, parentID string
|
||||||
|
|
||||||
|
if v, ok := spctx.(otSpanContext); ok {
|
||||||
|
traceID = v.TraceID().String()
|
||||||
|
spanID = v.SpanID().String()
|
||||||
|
} else {
|
||||||
|
carrier := make(map[string]string, 1)
|
||||||
|
_ = span.Tracer().Inject(spctx, ot.TextMap, ot.TextMapCarrier(carrier))
|
||||||
|
for k, v := range carrier {
|
||||||
|
switch k {
|
||||||
|
case "mockpfx-ids-sampled":
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type idStringer struct {
|
||||||
|
s string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s idStringer) String() string {
|
||||||
|
return s.s
|
||||||
|
}
|
||||||
|
|
||||||
type otSpan struct {
|
type otSpan struct {
|
||||||
span ot.Span
|
span ot.Span
|
||||||
|
spanID 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 {
|
||||||
|
return os.traceID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (os *otSpan) SpanID() string {
|
||||||
|
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) {
|
||||||
@ -78,35 +162,62 @@ func (os *otSpan) Tracer() tracer.Tracer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (os *otSpan) Finish(opts ...tracer.SpanOption) {
|
func (os *otSpan) Finish(opts ...tracer.SpanOption) {
|
||||||
if len(os.opts.Labels)%2 != 0 {
|
if os.finished {
|
||||||
os.opts.Labels = os.opts.Labels[:len(os.opts.Labels)-1]
|
return
|
||||||
}
|
}
|
||||||
for idx := 0; idx < len(os.opts.Labels); idx += 2 {
|
|
||||||
switch os.opts.Labels[idx] {
|
options := os.opts
|
||||||
case "err":
|
|
||||||
os.status = tracer.SpanStatusError
|
options.Status = os.status
|
||||||
os.statusMsg = fmt.Sprintf("%v", os.opts.Labels[idx+1])
|
options.StatusMsg = os.statusMsg
|
||||||
case "error":
|
options.Labels = append(options.Labels, os.labels...)
|
||||||
continue
|
|
||||||
case "X-Request-Id", "x-request-id":
|
for _, o := range opts {
|
||||||
os.span.SetTag("x-request-id", os.opts.Labels[idx+1])
|
o(&options)
|
||||||
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:
|
if !options.Record {
|
||||||
os.span.LogKV(os.opts.Labels[idx], os.opts.Labels[idx+1])
|
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)
|
||||||
|
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.SetTag("error", true)
|
||||||
os.span.LogKV("error", os.statusMsg)
|
os.span.LogKV("error", options.StatusMsg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
os.span.LogFields(otlog.Event(name))
|
os.span.LogFields(otlog.Event(name))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (os *otSpan) AddLogs(kv ...interface{}) {
|
||||||
|
os.span.LogKV(kv...)
|
||||||
|
}
|
||||||
|
|
||||||
func (os *otSpan) Context() context.Context {
|
func (os *otSpan) Context() context.Context {
|
||||||
return ot.ContextWithSpan(context.Background(), os.span)
|
return ot.ContextWithSpan(context.Background(), os.span)
|
||||||
}
|
}
|
||||||
@ -124,7 +235,7 @@ func (os *otSpan) Kind() tracer.SpanKind {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (os *otSpan) AddLabels(labels ...interface{}) {
|
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 {
|
func NewTracer(opts ...tracer.Option) *otTracer {
|
||||||
@ -132,10 +243,6 @@ func NewTracer(opts ...tracer.Option) *otTracer {
|
|||||||
return &otTracer{opts: options}
|
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) {
|
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 {
|
if tracerSpan, ok := tracer.SpanFromContext(ctx); ok && tracerSpan != nil {
|
||||||
return t.startSpanFromContext(ctx, name, opts...)
|
return t.startSpanFromContext(ctx, name, opts...)
|
||||||
@ -217,11 +324,15 @@ 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(nmd)); err != nil {
|
if err := sp.Tracer().Inject(sp.Context(), ot.TextMap, ot.TextMapCarrier(smd)); 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)
|
||||||
|
|
||||||
@ -260,11 +371,15 @@ 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(nmd)); err != nil {
|
if err := sp.Tracer().Inject(sp.Context(), ot.TextMap, ot.TextMapCarrier(smd)); 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)
|
||||||
|
|
||||||
|
@ -2,49 +2,116 @@ package opentracing
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"sync"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/opentracing/opentracing-go"
|
"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"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestStartSpanFromIncomingContext(t *testing.T) {
|
func TestNoopTraceID(t *testing.T) {
|
||||||
md := metadata.New(2)
|
md := metadata.New(1)
|
||||||
md.Set("key", "val")
|
|
||||||
|
|
||||||
var g sync.WaitGroup
|
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
ctx = metadata.NewIncomingContext(ctx, md)
|
ctx = metadata.NewIncomingContext(ctx, md)
|
||||||
|
|
||||||
tracer := opentracing.GlobalTracer()
|
tr := NewTracer(Tracer(mocktracer.New()))
|
||||||
|
if err := tr.Init(); err != nil {
|
||||||
g.Add(8000)
|
t.Fatal(err)
|
||||||
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 {
|
var sp tracer.Span
|
||||||
select {
|
|
||||||
default:
|
ctx, sp = tr.Start(ctx, "test")
|
||||||
g.Wait()
|
if v := sp.TraceID(); v != "43" {
|
||||||
close(cherr)
|
t.Fatalf("invalid span trace id %#+v", v)
|
||||||
case err, ok := <-cherr:
|
}
|
||||||
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
} else if !ok {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user