Compare commits

..

12 Commits

Author SHA1 Message Date
3f75c0e276 Обновить .github/workflows/pr.yml
Some checks failed
build / test (push) Successful in 1m27s
build / lint (push) Successful in 9m20s
codeql / analyze (go) (push) Failing after 13m42s
2024-03-13 08:45:01 +03:00
855709a753 Обновить .github/workflows/build.yml
Some checks failed
build / test (push) Has been cancelled
build / lint (push) Has been cancelled
codeql / analyze (go) (push) Has been cancelled
2024-03-13 08:44:39 +03:00
b9fdec821e fixup span tags and logs
Some checks failed
codeql / analyze (go) (push) Failing after 2m58s
build / test (push) Failing after 1m9s
build / lint (push) Successful in 9m19s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-12 12:38:05 +03:00
afd47c8edb add new micro tracer methods
Some checks failed
build / test (push) Failing after 1m10s
codeql / analyze (go) (push) Failing after 1m48s
build / lint (push) Successful in 9m15s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-06 12:48:00 +03:00
eeef4515ab Merge pull request 'update to latest micro' (#46) from updates into master
Some checks failed
build / test (push) Failing after 50s
build / lint (push) Successful in 19s
codeql / analyze (go) (push) Failing after 1m4s
Reviewed-on: #46
2023-10-17 00:04:28 +03:00
a53b3b08bf update to latest micro
Some checks failed
codeql / analyze (go) (pull_request) Failing after 1m19s
prbuild / test (pull_request) Failing after 54s
prbuild / lint (pull_request) Successful in 33s
autoapprove / autoapprove (pull_request) Failing after 6s
automerge / automerge (pull_request) Failing after 5s
dependabot-automerge / automerge (pull_request) Has been skipped
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-10-17 00:03:46 +03:00
123023be06 add missing tag case messaging.operation
Some checks failed
codeql / analyze (go) (push) Failing after 3m13s
build / test (push) Failing after 1m28s
build / lint (push) Failing after 2m42s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-09-01 16:09:18 +03:00
c2f6dfd267 Merge pull request 'uniq labels' (#45) from imp into master
Some checks failed
build / test (push) Failing after 1m28s
build / lint (push) Failing after 2m44s
codeql / analyze (go) (push) Failing after 3m12s
Reviewed-on: #45
2023-09-01 15:52:53 +03:00
acd0034624 uniq labels
Some checks failed
automerge / automerge (pull_request) Failing after 4s
codeql / analyze (go) (pull_request) Failing after 3m4s
dependabot-automerge / automerge (pull_request) Has been skipped
prbuild / test (pull_request) Failing after 1m28s
prbuild / lint (pull_request) Failing after 2m46s
autoapprove / autoapprove (pull_request) Failing after 1m25s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-09-01 15:52:29 +03:00
b32b60d136 fixup span and tags
Some checks failed
build / test (push) Failing after 1m29s
build / lint (push) Failing after 2m42s
codeql / analyze (go) (push) Failing after 3m14s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-09-01 08:27:53 +03:00
66d1c9d811 Merge pull request 'update for latest micro' (#41) from micro into master
Some checks failed
build / test (push) Failing after 1m29s
build / lint (push) Failing after 2m36s
codeql / analyze (go) (push) Failing after 3m13s
Reviewed-on: #41
2023-08-02 09:41:49 +03:00
a1838a8070 update for latest micro
Some checks failed
autoapprove / autoapprove (pull_request) Failing after 1m24s
automerge / automerge (pull_request) Failing after 3s
codeql / analyze (go) (pull_request) Failing after 3m11s
dependabot-automerge / automerge (pull_request) Has been skipped
prbuild / test (pull_request) Failing after 1m28s
prbuild / lint (pull_request) Failing after 2m41s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-08-02 09:41:32 +03:00
7 changed files with 230 additions and 111 deletions

View File

@ -12,7 +12,7 @@ jobs:
- name: setup - name: setup
uses: actions/setup-go@v3 uses: actions/setup-go@v3
with: with:
go-version: 1.17 go-version: 1.21
- name: checkout - name: checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: cache - name: cache

View File

@ -12,7 +12,7 @@ jobs:
- name: setup - name: setup
uses: actions/setup-go@v3 uses: actions/setup-go@v3
with: with:
go-version: 1.17 go-version: 1.20
- name: checkout - name: checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: cache - name: cache

8
go.mod
View File

@ -4,11 +4,7 @@ go 1.20
require ( require (
github.com/opentracing/opentracing-go v1.2.0 github.com/opentracing/opentracing-go v1.2.0
go.unistack.org/micro/v4 v4.0.1 go.unistack.org/micro/v4 v4.0.13
) )
require ( require github.com/stretchr/testify v1.8.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/stretchr/testify v1.5.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

14
go.sum
View File

@ -1,17 +1,13 @@
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/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.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.unistack.org/micro/v4 v4.0.1 h1:xo1IxbVfgh8i0eY0VeYa3cbb13u5n/Mxnp3FOgWD4Jo= go.unistack.org/micro/v4 v4.0.13 h1:wyprk+KNAv/zR6bri6p6ucmXcJ9WyZ/vzru4zjPd8dA=
go.unistack.org/micro/v4 v4.0.1/go.mod h1:p/J5UcSJjfHsWGT31uKoghQ5rUQZzQJBAFy+Z4+ZVMs= go.unistack.org/micro/v4 v4.0.13/go.mod h1:ZDgU9931vm2l7X6RN/6UuwRIVp24GRdmQ7dKmegArk4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

View File

@ -3,31 +3,40 @@ package opentracing
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"github.com/opentracing/opentracing-go" ot "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log" otlog "github.com/opentracing/opentracing-go/log"
"go.unistack.org/micro/v4/metadata" "go.unistack.org/micro/v4/metadata"
"go.unistack.org/micro/v4/options"
"go.unistack.org/micro/v4/tracer" "go.unistack.org/micro/v4/tracer"
rutil "go.unistack.org/micro/v4/util/reflect"
) )
var _ tracer.Tracer = &otTracer{} var _ tracer.Tracer = &otTracer{}
type otTracer struct { type otTracer struct {
opts tracer.Options opts tracer.Options
tracer opentracing.Tracer tracer ot.Tracer
} }
func (ot *otTracer) Name() string { func (t *otTracer) Name() string {
return ot.opts.Name return t.opts.Name
} }
func (ot *otTracer) Init(opts ...tracer.Option) error { func (t *otTracer) Flush(ctx context.Context) error {
return nil
}
func (t *otTracer) Init(opts ...options.Option) error {
for _, o := range opts { for _, o := range opts {
o(&ot.opts) if err := o(&t.opts); err != nil {
return err
}
} }
if tr, ok := ot.opts.Context.Value(tracerKey{}).(opentracing.Tracer); ok { if tr, ok := t.opts.Context.Value(tracerKey{}).(ot.Tracer); ok {
ot.tracer = tr t.tracer = tr
} else { } else {
return errors.New("Tracer option missing") return errors.New("Tracer option missing")
} }
@ -35,32 +44,69 @@ func (ot *otTracer) Init(opts ...tracer.Option) error {
return nil return nil
} }
func (ot *otTracer) Start(ctx context.Context, name string, opts ...tracer.SpanOption) (context.Context, tracer.Span) { type spanContext interface {
TraceID() idStringer
SpanID() idStringer
}
func (t *otTracer) Start(ctx context.Context, name string, opts ...options.Option) (context.Context, tracer.Span) {
options := tracer.NewSpanOptions(opts...) options := tracer.NewSpanOptions(opts...)
var span opentracing.Span var span ot.Span
switch options.Kind { switch options.Kind {
case tracer.SpanKindInternal, tracer.SpanKindUnspecified: case tracer.SpanKindUnspecified:
ctx, span = ot.startSpanFromContext(ctx, name) ctx, span = t.startSpanFromAny(ctx, name)
case tracer.SpanKindInternal:
ctx, span = t.startSpanFromContext(ctx, name)
case tracer.SpanKindClient, tracer.SpanKindProducer: case tracer.SpanKindClient, tracer.SpanKindProducer:
ctx, span = ot.startSpanFromOutgoingContext(ctx, name) ctx, span = t.startSpanFromOutgoingContext(ctx, name)
case tracer.SpanKindServer, tracer.SpanKindConsumer: case tracer.SpanKindServer, tracer.SpanKindConsumer:
ctx, span = ot.startSpanFromIncomingContext(ctx, ot.tracer, name) ctx, span = t.startSpanFromIncomingContext(ctx, name)
} }
return ctx, &otSpan{span: span, opts: options}
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 { type otSpan struct {
span opentracing.Span span ot.Span
spanID string
traceID string
opts tracer.SpanOptions opts tracer.SpanOptions
status tracer.SpanStatus status tracer.SpanStatus
statusMsg string statusMsg string
} }
func (os *otSpan) TraceID() string {
return os.traceID
}
func (os *otSpan) SpanID() string {
return os.spanID
}
func (os *otSpan) SetStatus(st tracer.SpanStatus, msg string) { func (os *otSpan) SetStatus(st tracer.SpanStatus, msg string) {
switch st {
case tracer.SpanStatusError:
os.span.SetTag("error", true)
}
os.status = st os.status = st
os.statusMsg = msg os.statusMsg = msg
} }
@ -73,19 +119,54 @@ func (os *otSpan) Tracer() tracer.Tracer {
return &otTracer{tracer: os.span.Tracer()} return &otTracer{tracer: os.span.Tracer()}
} }
func (os *otSpan) Finish(opts ...tracer.SpanOption) { func (os *otSpan) AddLogs(kv ...interface{}) {
if len(os.opts.Labels) > 0 { os.span.LogKV(kv...)
os.span.LogKV(os.opts.Labels...) }
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 {
k, ok := os.opts.Labels[idx].(string)
if !ok {
continue
}
v, ok := os.opts.Labels[idx+1].(string)
if !ok {
v = fmt.Sprintf("%v", os.opts.Labels[idx+1])
}
switch k {
case "err":
os.status = tracer.SpanStatusError
os.statusMsg = v
case "error":
continue
case "X-Request-Id", "x-request-id":
os.span.SetTag("x-request-id", v)
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(k, v)
default:
os.span.LogKV(k, v)
}
}
if os.status == tracer.SpanStatusError {
os.span.SetTag("error", true)
os.span.LogKV("error", os.statusMsg)
}
os.span.SetTag("span.kind", os.opts.Kind)
os.span.Finish() os.span.Finish()
} }
func (os *otSpan) AddEvent(name string, opts ...tracer.EventOption) { func (os *otSpan) AddEvent(name string, opts ...options.Option) {
os.span.LogFields(log.Event(name)) os.span.LogFields(otlog.Event(name))
} }
func (os *otSpan) Context() context.Context { func (os *otSpan) Context() context.Context {
return opentracing.ContextWithSpan(context.Background(), os.span) return ot.ContextWithSpan(context.Background(), os.span)
} }
func (os *otSpan) SetName(name string) { func (os *otSpan) SetName(name string) {
@ -104,80 +185,142 @@ func (os *otSpan) AddLabels(labels ...interface{}) {
os.opts.Labels = append(os.opts.Labels, labels...) os.opts.Labels = append(os.opts.Labels, labels...)
} }
func NewTracer(opts ...tracer.Option) *otTracer { func NewTracer(opts ...options.Option) *otTracer {
options := tracer.NewOptions(opts...) options := tracer.NewOptions(opts...)
return &otTracer{opts: options} return &otTracer{opts: options}
} }
func spanFromContext(ctx context.Context) opentracing.Span { func (t *otTracer) startSpanFromAny(ctx context.Context, name string, opts ...ot.StartSpanOption) (context.Context, ot.Span) {
return opentracing.SpanFromContext(ctx) if tracerSpan, ok := tracer.SpanFromContext(ctx); ok && tracerSpan != nil {
return t.startSpanFromContext(ctx, name, opts...)
}
if otSpan := ot.SpanFromContext(ctx); otSpan != nil {
return t.startSpanFromContext(ctx, name, opts...)
}
if md, ok := metadata.FromIncomingContext(ctx); ok && md != nil {
return t.startSpanFromIncomingContext(ctx, name, opts...)
}
if md, ok := metadata.FromOutgoingContext(ctx); ok && md != nil {
return t.startSpanFromOutgoingContext(ctx, name, opts...)
}
return t.startSpanFromContext(ctx, name, opts...)
} }
func (ot *otTracer) startSpanFromContext(ctx context.Context, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span) { func (t *otTracer) startSpanFromContext(ctx context.Context, name string, opts ...ot.StartSpanOption) (context.Context, ot.Span) {
if parentSpan := opentracing.SpanFromContext(ctx); parentSpan != nil { var parentSpan ot.Span
opts = append(opts, opentracing.ChildOf(parentSpan.Context())) if tracerSpan, ok := tracer.SpanFromContext(ctx); ok && tracerSpan != nil {
if sp, ok := tracerSpan.(*otSpan); ok {
parentSpan = sp.span
}
}
if parentSpan == nil {
if otSpan := ot.SpanFromContext(ctx); otSpan != nil {
parentSpan = otSpan
}
}
if parentSpan != nil {
opts = append(opts, ot.ChildOf(parentSpan.Context()))
} }
md := metadata.New(1) md := metadata.New(1)
sp := ot.tracer.StartSpan(name, opts...) sp := t.tracer.StartSpan(name, opts...)
if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(md)); err != nil { if err := sp.Tracer().Inject(sp.Context(), ot.TextMap, ot.TextMapCarrier(md)); err != nil {
return nil, nil return nil, nil
} }
ctx = opentracing.ContextWithSpan(ctx, sp) ctx = ot.ContextWithSpan(ctx, sp)
return ctx, sp return ctx, sp
} }
func (ot *otTracer) startSpanFromOutgoingContext(ctx context.Context, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span) { func (t *otTracer) startSpanFromOutgoingContext(ctx context.Context, name string, opts ...ot.StartSpanOption) (context.Context, ot.Span) {
var parentCtx opentracing.SpanContext var parentSpan ot.Span
if tracerSpan, ok := tracer.SpanFromContext(ctx); ok && tracerSpan != nil {
if sp, ok := tracerSpan.(*otSpan); ok {
parentSpan = sp.span
}
}
if parentSpan == nil {
if otSpan := ot.SpanFromContext(ctx); otSpan != nil {
parentSpan = otSpan
}
}
md, ok := metadata.FromOutgoingContext(ctx) md, ok := metadata.FromOutgoingContext(ctx)
if ok && md != nil {
if spanCtx, err := ot.tracer.Extract(opentracing.TextMap, opentracing.TextMapCarrier(md)); err == nil && ok {
parentCtx = spanCtx
}
}
if parentCtx != nil { if parentSpan != nil {
opts = append(opts, opentracing.ChildOf(parentCtx)) opts = append(opts, ot.ChildOf(parentSpan.Context()))
} else {
var parentCtx ot.SpanContext
if ok && md != nil {
if spanCtx, err := t.tracer.Extract(ot.TextMap, ot.TextMapCarrier(md)); err == nil && ok {
parentCtx = spanCtx
}
}
if parentCtx != nil {
opts = append(opts, ot.ChildOf(parentCtx))
}
} }
nmd := metadata.Copy(md) nmd := metadata.Copy(md)
sp := ot.tracer.StartSpan(name, opts...) sp := t.tracer.StartSpan(name, opts...)
if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(nmd)); err != nil { if err := sp.Tracer().Inject(sp.Context(), ot.TextMap, ot.TextMapCarrier(nmd)); err != nil {
return nil, nil return nil, nil
} }
ctx = metadata.NewOutgoingContext(opentracing.ContextWithSpan(ctx, sp), nmd) ctx = metadata.NewOutgoingContext(ot.ContextWithSpan(ctx, sp), nmd)
return ctx, sp return ctx, sp
} }
func (ot *otTracer) startSpanFromIncomingContext(ctx context.Context, tracer opentracing.Tracer, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span) { func (t *otTracer) startSpanFromIncomingContext(ctx context.Context, name string, opts ...ot.StartSpanOption) (context.Context, ot.Span) {
var parentCtx opentracing.SpanContext var parentSpan ot.Span
if tracerSpan, ok := tracer.SpanFromContext(ctx); ok && tracerSpan != nil {
if sp, ok := tracerSpan.(*otSpan); ok {
parentSpan = sp.span
}
}
if parentSpan == nil {
if otSpan := ot.SpanFromContext(ctx); otSpan != nil {
parentSpan = otSpan
}
}
md, ok := metadata.FromIncomingContext(ctx) md, ok := metadata.FromIncomingContext(ctx)
if ok && md != nil {
if spanCtx, err := tracer.Extract(opentracing.TextMap, opentracing.TextMapCarrier(md)); err == nil {
parentCtx = spanCtx
}
}
if parentCtx != nil { if parentSpan != nil {
opts = append(opts, opentracing.ChildOf(parentCtx)) opts = append(opts, ot.ChildOf(parentSpan.Context()))
} else {
var parentCtx ot.SpanContext
if ok && md != nil {
if spanCtx, err := t.tracer.Extract(ot.TextMap, ot.TextMapCarrier(md)); err == nil {
parentCtx = spanCtx
}
}
if parentCtx != nil {
opts = append(opts, ot.ChildOf(parentCtx))
}
} }
nmd := metadata.Copy(md) nmd := metadata.Copy(md)
sp := tracer.StartSpan(name, opts...) sp := t.tracer.StartSpan(name, opts...)
if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(nmd)); err != nil { if err := sp.Tracer().Inject(sp.Context(), ot.TextMap, ot.TextMapCarrier(nmd)); err != nil {
return nil, nil return nil, nil
} }
ctx = metadata.NewIncomingContext(opentracing.ContextWithSpan(ctx, sp), nmd) ctx = metadata.NewIncomingContext(ot.ContextWithSpan(ctx, sp), nmd)
return ctx, sp return ctx, sp
} }

View File

@ -2,47 +2,31 @@ package opentracing
import ( import (
"context" "context"
"sync"
"testing" "testing"
opentracing "github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go/mocktracer"
"go.unistack.org/micro/v4/metadata" "go.unistack.org/micro/v4/metadata"
"go.unistack.org/micro/v4/tracer"
) )
func TestStartSpanFromIncomingContext(t *testing.T) { func TestTraceID(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()))
ot := &otTracer{tracer: tracer} if err := tr.Init(); err != nil {
t.Fatal(err)
g.Add(8000)
cherr := make(chan error)
for i := 0; i < 8000; i++ {
go func() {
defer g.Done()
_, sp := ot.startSpanFromIncomingContext(ctx, tracer, "test")
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 err != nil { if v := sp.SpanID(); v != "44" {
t.Fatal(err) t.Fatalf("invalid span span id %#+v", v)
} else if !ok {
return
}
}
} }
} }

View File

@ -2,11 +2,11 @@ package opentracing
import ( import (
"github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go"
"go.unistack.org/micro/v4/tracer" "go.unistack.org/micro/v4/options"
) )
type tracerKey struct{} type tracerKey struct{}
func Tracer(ot opentracing.Tracer) tracer.Option { func Tracer(ot opentracing.Tracer) options.Option {
return tracer.SetOption(tracerKey{}, ot) return options.ContextOption(tracerKey{}, ot)
} }