Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
3f75c0e276 | |||
855709a753 | |||
b9fdec821e | |||
afd47c8edb | |||
eeef4515ab | |||
a53b3b08bf | |||
123023be06 | |||
c2f6dfd267 | |||
acd0034624 |
2
.github/workflows/build.yml
vendored
2
.github/workflows/build.yml
vendored
@ -12,7 +12,7 @@ jobs:
|
||||
- name: setup
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.17
|
||||
go-version: 1.21
|
||||
- name: checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: cache
|
||||
|
2
.github/workflows/pr.yml
vendored
2
.github/workflows/pr.yml
vendored
@ -12,7 +12,7 @@ jobs:
|
||||
- name: setup
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.17
|
||||
go-version: 1.20
|
||||
- name: checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: cache
|
||||
|
2
go.mod
2
go.mod
@ -4,7 +4,7 @@ go 1.20
|
||||
|
||||
require (
|
||||
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
|
||||
|
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.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.7 h1:2lwtZlHcSwgkahhFbkI4x1lOS79lw8uLHtcEhlFF+AM=
|
||||
go.unistack.org/micro/v4 v4.0.7/go.mod h1:bVEYTlPi0EsdgZZt311bIroDg9ict7ky3C87dSCCAGk=
|
||||
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=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
"go.unistack.org/micro/v4/metadata"
|
||||
"go.unistack.org/micro/v4/options"
|
||||
"go.unistack.org/micro/v4/tracer"
|
||||
rutil "go.unistack.org/micro/v4/util/reflect"
|
||||
)
|
||||
|
||||
var _ tracer.Tracer = &otTracer{}
|
||||
@ -29,7 +30,9 @@ func (t *otTracer) Flush(ctx context.Context) error {
|
||||
|
||||
func (t *otTracer) Init(opts ...options.Option) error {
|
||||
for _, o := range opts {
|
||||
o(&t.opts)
|
||||
if err := o(&t.opts); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if tr, ok := t.opts.Context.Value(tracerKey{}).(ot.Tracer); ok {
|
||||
@ -41,6 +44,11 @@ func (t *otTracer) Init(opts ...options.Option) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
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...)
|
||||
var span ot.Span
|
||||
@ -54,17 +62,50 @@ func (t *otTracer) Start(ctx context.Context, name string, opts ...options.Optio
|
||||
case tracer.SpanKindServer, tracer.SpanKindConsumer:
|
||||
ctx, span = t.startSpanFromIncomingContext(ctx, name)
|
||||
}
|
||||
|
||||
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 {
|
||||
span ot.Span
|
||||
spanID string
|
||||
traceID string
|
||||
opts tracer.SpanOptions
|
||||
status tracer.SpanStatus
|
||||
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) {
|
||||
os.status = st
|
||||
os.statusMsg = msg
|
||||
@ -78,29 +119,45 @@ 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) {
|
||||
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] {
|
||||
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 = fmt.Sprintf("%v", os.opts.Labels[idx+1])
|
||||
os.statusMsg = v
|
||||
case "error":
|
||||
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", "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]))
|
||||
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(os.opts.Labels[idx], os.opts.Labels[idx+1])
|
||||
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()
|
||||
}
|
||||
|
||||
@ -133,10 +190,6 @@ func NewTracer(opts ...options.Option) *otTracer {
|
||||
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) {
|
||||
if tracerSpan, ok := tracer.SpanFromContext(ctx); ok && tracerSpan != nil {
|
||||
return t.startSpanFromContext(ctx, name, opts...)
|
||||
|
@ -2,49 +2,31 @@ package opentracing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
opentracing "github.com/opentracing/opentracing-go"
|
||||
"github.com/opentracing/opentracing-go/mocktracer"
|
||||
"go.unistack.org/micro/v4/metadata"
|
||||
"go.unistack.org/micro/v4/tracer"
|
||||
)
|
||||
|
||||
func TestStartSpanFromIncomingContext(t *testing.T) {
|
||||
md := metadata.New(2)
|
||||
md.Set("key", "val")
|
||||
|
||||
var g sync.WaitGroup
|
||||
|
||||
func TestTraceID(t *testing.T) {
|
||||
md := metadata.New(1)
|
||||
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()
|
||||
}()
|
||||
tr := NewTracer(Tracer(mocktracer.New()))
|
||||
if err := tr.Init(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
default:
|
||||
g.Wait()
|
||||
close(cherr)
|
||||
case err, ok := <-cherr:
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
} else if !ok {
|
||||
return
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user