Compare commits

..

27 Commits
master ... v3

Author SHA1 Message Date
31350e8c21 Merge pull request '#347 add Sort Labels' (#47) from kgorbunov/micro-tracer-opentracing:#347-v3 into v3
Some checks failed
build / test (push) Failing after 1s
build / lint (push) Failing after 1s
codeql / analyze (go) (push) Failing after 1s
Reviewed-on: #47
2024-09-16 15:01:07 +03:00
Gorbunov Kirill Andreevich
d3d5925747 #347 add Sort Labels
Some checks failed
autoapprove / autoapprove (pull_request) Failing after 1s
automerge / automerge (pull_request) Has been skipped
dependabot-automerge / automerge (pull_request) Has been skipped
codeql / analyze (go) (pull_request) Has been cancelled
prbuild / test (pull_request) Has been cancelled
prbuild / lint (pull_request) Has been cancelled
2024-09-16 14:55:01 +03:00
89f75e0431 improve tracing
Some checks failed
build / test (push) Failing after 26s
build / lint (push) Successful in 25s
codeql / analyze (go) (push) Failing after 49s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-07-22 23:39:22 +03:00
9f6620676a get trace info from context
Some checks failed
build / test (push) Failing after 23s
build / lint (push) Successful in 22s
codeql / analyze (go) (push) Failing after 1m0s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-07-17 17:51:51 +03:00
e1f85545d9 get trace info from context
Some checks failed
build / test (push) Waiting to run
build / lint (push) Successful in 41s
codeql / analyze (go) (push) Failing after 2m5s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-07-17 17:38:14 +03:00
e887edb475 get trace info from context
Some checks failed
build / test (push) Has been cancelled
build / lint (push) Has been cancelled
codeql / analyze (go) (push) Has been cancelled
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-07-17 17:37:06 +03:00
2149a713d5 support never micro context attrs funcs
Some checks failed
build / lint (push) Successful in 24s
build / test (push) Failing after 1m30s
codeql / analyze (go) (push) Failing after 1m55s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-07-06 00:19:38 +03:00
9b553cfa62 add ability to pass span options on finish
Some checks failed
build / test (push) Failing after 1m38s
codeql / analyze (go) (push) Failing after 1m54s
build / lint (push) Successful in 9m17s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-05-09 19:30:32 +03:00
65ac3aff56 fixup add labels
Some checks failed
build / test (push) Failing after 1m43s
codeql / analyze (go) (push) Failing after 1m43s
build / lint (push) Successful in 9m13s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-05-06 01:09:41 +03:00
e9933df477 add abilit to skip span recording
Some checks failed
build / test (push) Failing after 1m32s
codeql / analyze (go) (push) Failing after 1m42s
build / lint (push) Successful in 9m16s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-05-04 19:24:10 +03:00
905910e8ce avoid duplicate tags
Some checks failed
build / test (push) Failing after 1m17s
codeql / analyze (go) (push) Failing after 2m45s
build / lint (push) Successful in 9m30s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-17 23:07:06 +03:00
7948652293 fixup labels
Some checks failed
build / test (push) Failing after 1m13s
codeql / analyze (go) (push) Failing after 1m46s
build / lint (push) Successful in 9m14s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-17 00:51:29 +03:00
54d9857bb9 fixup labels
Some checks failed
build / test (push) Failing after 1m11s
codeql / analyze (go) (push) Failing after 1m48s
build / lint (push) Successful in 9m20s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-17 00:35:37 +03:00
1e9abb9905 fixup tracing
Some checks failed
build / test (push) Failing after 1m11s
codeql / analyze (go) (push) Failing after 1m47s
build / lint (push) Successful in 9m19s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-16 23:59:36 +03:00
6b9edf3593 fixup finish
Some checks failed
build / test (push) Failing after 1m21s
codeql / analyze (go) (push) Failing after 1m51s
build / lint (push) Successful in 9m15s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-15 11:34:31 +03:00
742b86f128 support semconv
Some checks failed
build / test (push) Failing after 1m9s
codeql / analyze (go) (push) Failing after 1m48s
build / lint (push) Successful in 9m16s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-14 23:35:38 +03:00
474fc506dd fixup logs
Some checks failed
build / test (push) Failing after 1m7s
codeql / analyze (go) (push) Failing after 3m10s
build / lint (push) Successful in 9m15s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-12 23:33:59 +03:00
c94af432a0 fixup span tags and logs
Some checks failed
build / test (push) Failing after 1m49s
build / lint (push) Successful in 9m49s
codeql / analyze (go) (push) Failing after 12m46s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-12 12:38:40 +03:00
3f9704aef8 add new micro tracer methods
Some checks failed
build / test (push) Failing after 1m13s
codeql / analyze (go) (push) Failing after 2m31s
build / lint (push) Successful in 9m26s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-06 12:36:29 +03:00
5224363bfc update micro
Some checks failed
build / test (push) Failing after 58s
build / lint (push) Successful in 15s
codeql / analyze (go) (push) Failing after 1m52s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-10-12 01:27:42 +03:00
52ec3cf264 add missing tag case messaging.operation
Some checks failed
build / test (push) Failing after 1m28s
build / lint (push) Failing after 2m35s
codeql / analyze (go) (push) Failing after 3m7s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-09-01 16:10:08 +03:00
d2b9201423 Merge pull request 'tracer improve' (#44) from improve into v3
Some checks failed
build / test (push) Failing after 1m28s
build / lint (push) Failing after 2m40s
codeql / analyze (go) (push) Failing after 3m10s
Reviewed-on: #44
2023-09-01 15:41:22 +03:00
6543196faf tracer improve
Some checks failed
prbuild / test (pull_request) Failing after 1m29s
prbuild / lint (pull_request) Failing after 2m42s
autoapprove / autoapprove (pull_request) Failing after 1m26s
automerge / automerge (pull_request) Failing after 4s
codeql / analyze (go) (pull_request) Failing after 3m20s
dependabot-automerge / automerge (pull_request) Has been skipped
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-09-01 15:39:30 +03:00
f9dad57eb2 Merge pull request 'fixup span and tags' (#43) from fixup into v3
Some checks failed
codeql / analyze (go) (push) Failing after 3m3s
build / test (push) Failing after 1m28s
build / lint (push) Failing after 2m33s
Reviewed-on: #43
2023-09-01 08:09:14 +03:00
59283b64c9 fixup span and tags
Some checks failed
codeql / analyze (go) (pull_request) Failing after 3m7s
dependabot-automerge / automerge (pull_request) Has been skipped
prbuild / test (pull_request) Failing after 1m29s
prbuild / lint (pull_request) Failing after 2m33s
autoapprove / autoapprove (pull_request) Failing after 1m24s
automerge / automerge (pull_request) Failing after 3s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-09-01 08:08:34 +03:00
4805482921 Merge pull request 'update for latest micro' (#42) from micro into v3
Some checks failed
build / test (push) Failing after 1m28s
build / lint (push) Failing after 2m27s
codeql / analyze (go) (push) Failing after 3m9s
Reviewed-on: #42
2023-08-02 09:55:13 +03:00
c2225cb92a update for latest micro
Some checks failed
autoapprove / autoapprove (pull_request) Failing after 1m25s
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 1m29s
prbuild / lint (pull_request) Failing after 2m39s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-08-02 09:50:36 +03:00
7 changed files with 235 additions and 82 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.21 go-version: 1.17
- 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.20 go-version: 1.17
- name: checkout - name: checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: cache - name: cache

11
go.mod
View File

@ -1,10 +1,11 @@
module go.unistack.org/micro-tracer-opentracing/v4 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/v4 v4.0.13 go.opentelemetry.io/otel v1.28.0
go.unistack.org/micro/v3 v3.10.81
) )
require github.com/stretchr/testify v1.8.3 // indirect

14
go.sum
View File

@ -1,13 +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/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/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.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.unistack.org/micro/v4 v4.0.13 h1:wyprk+KNAv/zR6bri6p6ucmXcJ9WyZ/vzru4zjPd8dA= go.opentelemetry.io/otel v1.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo=
go.unistack.org/micro/v4 v4.0.13/go.mod h1:ZDgU9931vm2l7X6RN/6UuwRIVp24GRdmQ7dKmegArk4= go.opentelemetry.io/otel v1.28.0/go.mod h1:q68ijF8Fc8CnMHKyzqL6akLO46ePnjkgfIMIjUIX9z4=
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= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -3,14 +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.unistack.org/micro/v4/metadata" "go.opentelemetry.io/otel/attribute"
"go.unistack.org/micro/v4/options" "go.unistack.org/micro/v3/metadata"
"go.unistack.org/micro/v4/tracer" "go.unistack.org/micro/v3/tracer"
rutil "go.unistack.org/micro/v4/util/reflect" "go.unistack.org/micro/v3/util/sort"
) )
var _ tracer.Tracer = &otTracer{} var _ tracer.Tracer = &otTracer{}
@ -28,11 +28,9 @@ func (t *otTracer) Flush(ctx context.Context) error {
return nil return nil
} }
func (t *otTracer) Init(opts ...options.Option) error { func (t *otTracer) Init(opts ...tracer.Option) error {
for _, o := range opts { for _, o := range opts {
if err := o(&t.opts); err != nil { o(&t.opts)
return err
}
} }
if tr, ok := t.opts.Context.Value(tracerKey{}).(ot.Tracer); ok { if tr, ok := t.opts.Context.Value(tracerKey{}).(ot.Tracer); ok {
@ -44,13 +42,27 @@ func (t *otTracer) Init(opts ...options.Option) error {
return nil return nil
} }
type spanContext interface { type otSpanContext interface {
TraceID() idStringer TraceID() idStringer
SpanID() idStringer SpanID() idStringer
} }
func (t *otTracer) Start(ctx context.Context, name string, opts ...options.Option) (context.Context, tracer.Span) { type jSpanContext interface {
TraceID() idStringer
SpanID() idStringer
}
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:
@ -66,18 +78,41 @@ func (t *otTracer) Start(ctx context.Context, name string, opts ...options.Optio
sp := &otSpan{span: span, opts: options} sp := &otSpan{span: span, opts: options}
spctx := span.Context() spctx := span.Context()
if v, ok := spctx.(spanContext); ok {
sp.traceID = v.TraceID().String() var traceID, spanID, parentID string
sp.spanID = v.SpanID().String()
if v, ok := spctx.(otSpanContext); ok {
traceID = v.TraceID().String()
spanID = v.SpanID().String()
} else { } else {
if val, err := rutil.StructFieldByName(spctx, "TraceID"); err == nil { carrier := make(map[string]string, 1)
sp.traceID = fmt.Sprintf("%v", val) _ = span.Tracer().Inject(spctx, ot.TextMap, ot.TextMapCarrier(carrier))
} for k, v := range carrier {
if val, err := rutil.StructFieldByName(spctx, "SpanID"); err == nil { switch k {
sp.spanID = fmt.Sprintf("%v", val) 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
} }
@ -93,9 +128,12 @@ type otSpan struct {
span ot.Span span ot.Span
spanID string spanID string
traceID 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 { func (os *otSpan) TraceID() string {
@ -106,6 +144,10 @@ func (os *otSpan) SpanID() string {
return os.spanID 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) {
os.status = st os.status = st
os.statusMsg = msg os.statusMsg = msg
@ -119,52 +161,63 @@ 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) {
if os.finished {
return
}
options := os.opts
options.Status = os.status
options.StatusMsg = os.statusMsg
options.Labels = append(options.Labels, os.labels...)
for _, o := range opts {
o(&options)
}
if !options.Record {
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 options.Status == tracer.SpanStatusError {
os.span.SetTag("error", true)
os.span.LogKV("error", options.StatusMsg)
}
os.span.SetTag("span.kind", options.Kind)
os.span.Finish()
os.finished = true
}
func (os *otSpan) AddEvent(name string, opts ...tracer.EventOption) {
os.span.LogFields(otlog.Event(name))
}
func (os *otSpan) AddLogs(kv ...interface{}) { func (os *otSpan) AddLogs(kv ...interface{}) {
os.span.LogKV(kv...) os.span.LogKV(kv...)
} }
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()
}
func (os *otSpan) AddEvent(name string, opts ...options.Option) {
os.span.LogFields(otlog.Event(name))
}
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)
} }
@ -182,10 +235,10 @@ 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 ...options.Option) *otTracer { func NewTracer(opts ...tracer.Option) *otTracer {
options := tracer.NewOptions(opts...) options := tracer.NewOptions(opts...)
return &otTracer{opts: options} return &otTracer{opts: options}
} }
@ -271,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)
@ -314,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)

View File

@ -2,14 +2,17 @@ package opentracing
import ( import (
"context" "context"
"fmt"
"testing" "testing"
"github.com/opentracing/opentracing-go/mocktracer" "github.com/opentracing/opentracing-go/mocktracer"
"go.unistack.org/micro/v4/metadata" // jconfig "github.com/uber/jaeger-client-go/config"
"go.unistack.org/micro/v4/tracer" "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) md := metadata.New(1)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
@ -29,4 +32,86 @@ func TestTraceID(t *testing.T) {
if v := sp.SpanID(); v != "44" { if v := sp.SpanID(); v != "44" {
t.Fatalf("invalid span span id %#+v", v) 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())
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)
}
} }

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/options" "go.unistack.org/micro/v3/tracer"
) )
type tracerKey struct{} type tracerKey struct{}
func Tracer(ot opentracing.Tracer) options.Option { func Tracer(ot opentracing.Tracer) tracer.Option {
return options.ContextOption(tracerKey{}, ot) return tracer.SetOption(tracerKey{}, ot)
} }