Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
3f75c0e276 | |||
855709a753 | |||
b9fdec821e | |||
afd47c8edb |
2
.github/workflows/build.yml
vendored
2
.github/workflows/build.yml
vendored
@ -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
|
||||||
|
2
.github/workflows/pr.yml
vendored
2
.github/workflows/pr.yml
vendored
@ -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
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"go.unistack.org/micro/v4/metadata"
|
"go.unistack.org/micro/v4/metadata"
|
||||||
"go.unistack.org/micro/v4/options"
|
"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{}
|
||||||
@ -29,7 +30,9 @@ func (t *otTracer) Flush(ctx context.Context) error {
|
|||||||
|
|
||||||
func (t *otTracer) Init(opts ...options.Option) error {
|
func (t *otTracer) Init(opts ...options.Option) error {
|
||||||
for _, o := range opts {
|
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 {
|
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
|
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 (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 ot.Span
|
var span ot.Span
|
||||||
@ -54,34 +62,48 @@ func (t *otTracer) Start(ctx context.Context, name string, opts ...options.Optio
|
|||||||
case tracer.SpanKindServer, tracer.SpanKindConsumer:
|
case tracer.SpanKindServer, tracer.SpanKindConsumer:
|
||||||
ctx, span = t.startSpanFromIncomingContext(ctx, name)
|
ctx, span = t.startSpanFromIncomingContext(ctx, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
sp := &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
|
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 ot.Span
|
span ot.Span
|
||||||
|
spanID string
|
||||||
|
traceID string
|
||||||
opts tracer.SpanOptions
|
opts tracer.SpanOptions
|
||||||
status tracer.SpanStatus
|
status tracer.SpanStatus
|
||||||
statusMsg string
|
statusMsg string
|
||||||
}
|
}
|
||||||
|
|
||||||
type spanContext interface {
|
|
||||||
TraceID() fmt.Stringer
|
|
||||||
SpanID() fmt.Stringer
|
|
||||||
}
|
|
||||||
|
|
||||||
func (os *otSpan) TraceID() string {
|
func (os *otSpan) TraceID() string {
|
||||||
if spanctx, ok := os.span.Context().(spanContext); ok {
|
return os.traceID
|
||||||
return spanctx.TraceID().String()
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (os *otSpan) SpanID() string {
|
func (os *otSpan) SpanID() string {
|
||||||
if spanctx, ok := os.span.Context().(spanContext); ok {
|
return os.spanID
|
||||||
return spanctx.SpanID().String()
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (os *otSpan) SetStatus(st tracer.SpanStatus, msg string) {
|
func (os *otSpan) SetStatus(st tracer.SpanStatus, msg string) {
|
||||||
@ -107,20 +129,28 @@ func (os *otSpan) Finish(opts ...options.Option) {
|
|||||||
}
|
}
|
||||||
os.opts.Labels = tracer.UniqLabels(os.opts.Labels)
|
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] {
|
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":
|
case "err":
|
||||||
os.status = tracer.SpanStatusError
|
os.status = tracer.SpanStatusError
|
||||||
os.statusMsg = fmt.Sprintf("%v", os.opts.Labels[idx+1])
|
os.statusMsg = v
|
||||||
case "error":
|
case "error":
|
||||||
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", v)
|
||||||
case "rpc.call", "rpc.call_type", "rpc.flavor", "rpc.service", "rpc.method",
|
case "rpc.call", "rpc.call_type", "rpc.flavor", "rpc.service", "rpc.method",
|
||||||
"sdk.database", "db.statement", "db.args", "db.query", "db.method",
|
"sdk.database", "db.statement", "db.args", "db.query", "db.method",
|
||||||
"messaging.destination.name", "messaging.source.name", "messaging.operation":
|
"messaging.destination.name", "messaging.source.name", "messaging.operation":
|
||||||
os.span.SetTag(fmt.Sprintf("%v", os.opts.Labels[idx]), os.opts.Labels[idx+1])
|
os.span.SetTag(k, v)
|
||||||
default:
|
default:
|
||||||
os.span.LogKV(os.opts.Labels[idx], os.opts.Labels[idx+1])
|
os.span.LogKV(k, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if os.status == tracer.SpanStatusError {
|
if os.status == tracer.SpanStatusError {
|
||||||
@ -160,10 +190,6 @@ func NewTracer(opts ...options.Option) *otTracer {
|
|||||||
return &otTracer{opts: options}
|
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) {
|
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 {
|
if tracerSpan, ok := tracer.SpanFromContext(ctx); ok && tracerSpan != nil {
|
||||||
return t.startSpanFromContext(ctx, name, opts...)
|
return t.startSpanFromContext(ctx, name, opts...)
|
||||||
|
32
opentracing_test.go
Normal file
32
opentracing_test.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package opentracing
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/opentracing/opentracing-go/mocktracer"
|
||||||
|
"go.unistack.org/micro/v4/metadata"
|
||||||
|
"go.unistack.org/micro/v4/tracer"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTraceID(t *testing.T) {
|
||||||
|
md := metadata.New(1)
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
ctx = metadata.NewIncomingContext(ctx, md)
|
||||||
|
|
||||||
|
tr := NewTracer(Tracer(mocktracer.New()))
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user