Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
eeef4515ab | |||
a53b3b08bf | |||
123023be06 | |||
c2f6dfd267 | |||
acd0034624 |
2
go.mod
2
go.mod
@@ -4,7 +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.7
|
go.unistack.org/micro/v4 v4.0.13
|
||||||
)
|
)
|
||||||
|
|
||||||
require github.com/stretchr/testify v1.8.3 // indirect
|
require github.com/stretchr/testify v1.8.3 // indirect
|
||||||
|
4
go.sum
4
go.sum
@@ -8,6 +8,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
|
|||||||
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.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
|
||||||
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
go.unistack.org/micro/v4 v4.0.7 h1:2lwtZlHcSwgkahhFbkI4x1lOS79lw8uLHtcEhlFF+AM=
|
go.unistack.org/micro/v4 v4.0.13 h1:wyprk+KNAv/zR6bri6p6ucmXcJ9WyZ/vzru4zjPd8dA=
|
||||||
go.unistack.org/micro/v4 v4.0.7/go.mod h1:bVEYTlPi0EsdgZZt311bIroDg9ict7ky3C87dSCCAGk=
|
go.unistack.org/micro/v4 v4.0.13/go.mod h1:ZDgU9931vm2l7X6RN/6UuwRIVp24GRdmQ7dKmegArk4=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
@@ -65,6 +65,25 @@ type otSpan struct {
|
|||||||
statusMsg string
|
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) {
|
func (os *otSpan) SetStatus(st tracer.SpanStatus, msg string) {
|
||||||
os.status = st
|
os.status = st
|
||||||
os.statusMsg = msg
|
os.statusMsg = msg
|
||||||
@@ -78,10 +97,15 @@ func (os *otSpan) Tracer() tracer.Tracer {
|
|||||||
return &otTracer{tracer: os.span.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 ...options.Option) {
|
||||||
if len(os.opts.Labels)%2 != 0 {
|
if len(os.opts.Labels)%2 != 0 {
|
||||||
os.opts.Labels = os.opts.Labels[:len(os.opts.Labels)-1]
|
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 {
|
for idx := 0; idx < len(os.opts.Labels); idx += 2 {
|
||||||
switch os.opts.Labels[idx] {
|
switch os.opts.Labels[idx] {
|
||||||
case "err":
|
case "err":
|
||||||
@@ -91,8 +115,10 @@ func (os *otSpan) Finish(opts ...options.Option) {
|
|||||||
continue
|
continue
|
||||||
case "X-Request-Id", "x-request-id":
|
case "X-Request-Id", "x-request-id":
|
||||||
os.span.SetTag("x-request-id", os.opts.Labels[idx+1])
|
os.span.SetTag("x-request-id", 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":
|
case "rpc.call", "rpc.call_type", "rpc.flavor", "rpc.service", "rpc.method",
|
||||||
os.span.SetTag(fmt.Sprintf("%v", os.opts.Labels[idx]), fmt.Sprintf("%v", os.opts.Labels[idx+1]))
|
"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:
|
default:
|
||||||
os.span.LogKV(os.opts.Labels[idx], os.opts.Labels[idx+1])
|
os.span.LogKV(os.opts.Labels[idx], os.opts.Labels[idx+1])
|
||||||
}
|
}
|
||||||
@@ -101,6 +127,7 @@ func (os *otSpan) Finish(opts ...options.Option) {
|
|||||||
os.span.SetTag("error", true)
|
os.span.SetTag("error", true)
|
||||||
os.span.LogKV("error", os.statusMsg)
|
os.span.LogKV("error", os.statusMsg)
|
||||||
}
|
}
|
||||||
|
os.span.SetTag("span.kind", os.opts.Kind)
|
||||||
os.span.Finish()
|
os.span.Finish()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,50 +0,0 @@
|
|||||||
package opentracing
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"sync"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
opentracing "github.com/opentracing/opentracing-go"
|
|
||||||
"go.unistack.org/micro/v4/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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user