Compare commits
No commits in common. "master" and "v4.0.1" have entirely different histories.
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.21
|
||||
go-version: 1.17
|
||||
- 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.20
|
||||
go-version: 1.17
|
||||
- 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.13
|
||||
go.unistack.org/micro/v4 v4.0.5
|
||||
)
|
||||
|
||||
require github.com/stretchr/testify v1.8.3 // indirect
|
||||
|
6
go.sum
6
go.sum
@ -8,6 +8,8 @@ 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.13 h1:wyprk+KNAv/zR6bri6p6ucmXcJ9WyZ/vzru4zjPd8dA=
|
||||
go.unistack.org/micro/v4 v4.0.13/go.mod h1:ZDgU9931vm2l7X6RN/6UuwRIVp24GRdmQ7dKmegArk4=
|
||||
go.unistack.org/micro/v4 v4.0.1 h1:xo1IxbVfgh8i0eY0VeYa3cbb13u5n/Mxnp3FOgWD4Jo=
|
||||
go.unistack.org/micro/v4 v4.0.1/go.mod h1:p/J5UcSJjfHsWGT31uKoghQ5rUQZzQJBAFy+Z4+ZVMs=
|
||||
go.unistack.org/micro/v4 v4.0.5 h1:uacmV8yHjCOtVfECc4w1tgp6bd3bBWrZa8X4iQLk48g=
|
||||
go.unistack.org/micro/v4 v4.0.5/go.mod h1:bVEYTlPi0EsdgZZt311bIroDg9ict7ky3C87dSCCAGk=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
234
opentracing.go
234
opentracing.go
@ -3,40 +3,36 @@ package opentracing
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
ot "github.com/opentracing/opentracing-go"
|
||||
otlog "github.com/opentracing/opentracing-go/log"
|
||||
"github.com/opentracing/opentracing-go"
|
||||
"github.com/opentracing/opentracing-go/log"
|
||||
"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{}
|
||||
|
||||
type otTracer struct {
|
||||
opts tracer.Options
|
||||
tracer ot.Tracer
|
||||
tracer opentracing.Tracer
|
||||
}
|
||||
|
||||
func (t *otTracer) Name() string {
|
||||
return t.opts.Name
|
||||
func (ot *otTracer) Name() string {
|
||||
return ot.opts.Name
|
||||
}
|
||||
|
||||
func (t *otTracer) Flush(ctx context.Context) error {
|
||||
func (ot *otTracer) Flush(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *otTracer) Init(opts ...options.Option) error {
|
||||
func (ot *otTracer) Init(opts ...options.Option) error {
|
||||
for _, o := range opts {
|
||||
if err := o(&t.opts); err != nil {
|
||||
return err
|
||||
}
|
||||
o(&ot.opts)
|
||||
}
|
||||
|
||||
if tr, ok := t.opts.Context.Value(tracerKey{}).(ot.Tracer); ok {
|
||||
t.tracer = tr
|
||||
if tr, ok := ot.opts.Context.Value(tracerKey{}).(opentracing.Tracer); ok {
|
||||
ot.tracer = tr
|
||||
} else {
|
||||
return errors.New("Tracer option missing")
|
||||
}
|
||||
@ -44,69 +40,32 @@ 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) {
|
||||
func (ot *otTracer) Start(ctx context.Context, name string, opts ...options.Option) (context.Context, tracer.Span) {
|
||||
options := tracer.NewSpanOptions(opts...)
|
||||
var span ot.Span
|
||||
var span opentracing.Span
|
||||
switch options.Kind {
|
||||
case tracer.SpanKindUnspecified:
|
||||
ctx, span = t.startSpanFromAny(ctx, name)
|
||||
case tracer.SpanKindInternal:
|
||||
ctx, span = t.startSpanFromContext(ctx, name)
|
||||
case tracer.SpanKindInternal, tracer.SpanKindUnspecified:
|
||||
ctx, span = ot.startSpanFromContext(ctx, name)
|
||||
case tracer.SpanKindClient, tracer.SpanKindProducer:
|
||||
ctx, span = t.startSpanFromOutgoingContext(ctx, name)
|
||||
ctx, span = ot.startSpanFromOutgoingContext(ctx, name)
|
||||
case tracer.SpanKindServer, tracer.SpanKindConsumer:
|
||||
ctx, span = t.startSpanFromIncomingContext(ctx, name)
|
||||
ctx, span = ot.startSpanFromIncomingContext(ctx, ot.tracer, 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
|
||||
return ctx, &otSpan{span: span, opts: options}
|
||||
}
|
||||
|
||||
type otSpan struct {
|
||||
span ot.Span
|
||||
spanID string
|
||||
traceID string
|
||||
span opentracing.Span
|
||||
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) {
|
||||
switch st {
|
||||
case tracer.SpanStatusError:
|
||||
os.span.SetTag("error", true)
|
||||
}
|
||||
os.status = st
|
||||
os.statusMsg = msg
|
||||
}
|
||||
@ -119,54 +78,19 @@ 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]
|
||||
if len(os.opts.Labels) > 0 {
|
||||
os.span.LogKV(os.opts.Labels...)
|
||||
}
|
||||
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))
|
||||
os.span.LogFields(log.Event(name))
|
||||
}
|
||||
|
||||
func (os *otSpan) Context() context.Context {
|
||||
return ot.ContextWithSpan(context.Background(), os.span)
|
||||
return opentracing.ContextWithSpan(context.Background(), os.span)
|
||||
}
|
||||
|
||||
func (os *otSpan) SetName(name string) {
|
||||
@ -190,137 +114,75 @@ func NewTracer(opts ...options.Option) *otTracer {
|
||||
return &otTracer{opts: options}
|
||||
}
|
||||
|
||||
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...)
|
||||
func spanFromContext(ctx context.Context) opentracing.Span {
|
||||
return opentracing.SpanFromContext(ctx)
|
||||
}
|
||||
|
||||
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 (t *otTracer) startSpanFromContext(ctx context.Context, name string, opts ...ot.StartSpanOption) (context.Context, ot.Span) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
if parentSpan != nil {
|
||||
opts = append(opts, ot.ChildOf(parentSpan.Context()))
|
||||
func (ot *otTracer) startSpanFromContext(ctx context.Context, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span) {
|
||||
if parentSpan := opentracing.SpanFromContext(ctx); parentSpan != nil {
|
||||
opts = append(opts, opentracing.ChildOf(parentSpan.Context()))
|
||||
}
|
||||
|
||||
md := metadata.New(1)
|
||||
|
||||
sp := t.tracer.StartSpan(name, opts...)
|
||||
if err := sp.Tracer().Inject(sp.Context(), ot.TextMap, ot.TextMapCarrier(md)); err != nil {
|
||||
sp := ot.tracer.StartSpan(name, opts...)
|
||||
if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(md)); err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
ctx = ot.ContextWithSpan(ctx, sp)
|
||||
ctx = opentracing.ContextWithSpan(ctx, sp)
|
||||
|
||||
return ctx, sp
|
||||
}
|
||||
|
||||
func (t *otTracer) startSpanFromOutgoingContext(ctx context.Context, name string, opts ...ot.StartSpanOption) (context.Context, ot.Span) {
|
||||
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
|
||||
}
|
||||
}
|
||||
func (ot *otTracer) startSpanFromOutgoingContext(ctx context.Context, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span) {
|
||||
var parentCtx opentracing.SpanContext
|
||||
|
||||
md, ok := metadata.FromOutgoingContext(ctx)
|
||||
|
||||
if parentSpan != nil {
|
||||
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 {
|
||||
if spanCtx, err := ot.tracer.Extract(opentracing.TextMap, opentracing.TextMapCarrier(md)); err == nil && ok {
|
||||
parentCtx = spanCtx
|
||||
}
|
||||
}
|
||||
|
||||
if parentCtx != nil {
|
||||
opts = append(opts, ot.ChildOf(parentCtx))
|
||||
}
|
||||
opts = append(opts, opentracing.ChildOf(parentCtx))
|
||||
}
|
||||
|
||||
nmd := metadata.Copy(md)
|
||||
|
||||
sp := t.tracer.StartSpan(name, opts...)
|
||||
if err := sp.Tracer().Inject(sp.Context(), ot.TextMap, ot.TextMapCarrier(nmd)); err != nil {
|
||||
sp := ot.tracer.StartSpan(name, opts...)
|
||||
if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(nmd)); err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
ctx = metadata.NewOutgoingContext(ot.ContextWithSpan(ctx, sp), nmd)
|
||||
ctx = metadata.NewOutgoingContext(opentracing.ContextWithSpan(ctx, sp), nmd)
|
||||
|
||||
return ctx, sp
|
||||
}
|
||||
|
||||
func (t *otTracer) startSpanFromIncomingContext(ctx context.Context, name string, opts ...ot.StartSpanOption) (context.Context, ot.Span) {
|
||||
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
|
||||
}
|
||||
}
|
||||
func (ot *otTracer) startSpanFromIncomingContext(ctx context.Context, tracer opentracing.Tracer, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span) {
|
||||
var parentCtx opentracing.SpanContext
|
||||
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
|
||||
if parentSpan != nil {
|
||||
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 {
|
||||
if spanCtx, err := tracer.Extract(opentracing.TextMap, opentracing.TextMapCarrier(md)); err == nil {
|
||||
parentCtx = spanCtx
|
||||
}
|
||||
}
|
||||
|
||||
if parentCtx != nil {
|
||||
opts = append(opts, ot.ChildOf(parentCtx))
|
||||
}
|
||||
opts = append(opts, opentracing.ChildOf(parentCtx))
|
||||
}
|
||||
|
||||
nmd := metadata.Copy(md)
|
||||
|
||||
sp := t.tracer.StartSpan(name, opts...)
|
||||
if err := sp.Tracer().Inject(sp.Context(), ot.TextMap, ot.TextMapCarrier(nmd)); err != nil {
|
||||
sp := tracer.StartSpan(name, opts...)
|
||||
if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(nmd)); err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
ctx = metadata.NewIncomingContext(ot.ContextWithSpan(ctx, sp), nmd)
|
||||
ctx = metadata.NewIncomingContext(opentracing.ContextWithSpan(ctx, sp), nmd)
|
||||
|
||||
return ctx, sp
|
||||
}
|
||||
|
@ -2,31 +2,47 @@ package opentracing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/opentracing/opentracing-go/mocktracer"
|
||||
opentracing "github.com/opentracing/opentracing-go"
|
||||
"go.unistack.org/micro/v4/metadata"
|
||||
"go.unistack.org/micro/v4/tracer"
|
||||
)
|
||||
|
||||
func TestTraceID(t *testing.T) {
|
||||
md := metadata.New(1)
|
||||
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)
|
||||
|
||||
tr := NewTracer(Tracer(mocktracer.New()))
|
||||
if err := tr.Init(); err != nil {
|
||||
tracer := opentracing.GlobalTracer()
|
||||
ot := &otTracer{tracer: tracer}
|
||||
|
||||
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 {
|
||||
select {
|
||||
default:
|
||||
g.Wait()
|
||||
close(cherr)
|
||||
case err, ok := <-cherr:
|
||||
if 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)
|
||||
} else if !ok {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user