2022-03-05 19:10:19 +03:00
|
|
|
package opentracing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-01-17 08:43:04 +03:00
|
|
|
"errors"
|
2023-09-01 08:08:34 +03:00
|
|
|
"fmt"
|
2022-03-05 19:10:19 +03:00
|
|
|
|
2023-09-01 08:08:34 +03:00
|
|
|
ot "github.com/opentracing/opentracing-go"
|
|
|
|
otlog "github.com/opentracing/opentracing-go/log"
|
2022-03-05 19:10:19 +03:00
|
|
|
"go.unistack.org/micro/v3/metadata"
|
|
|
|
"go.unistack.org/micro/v3/tracer"
|
2024-03-06 12:36:29 +03:00
|
|
|
rutil "go.unistack.org/micro/v3/util/reflect"
|
2022-03-05 19:10:19 +03:00
|
|
|
)
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
var _ tracer.Tracer = &otTracer{}
|
2022-03-05 19:10:19 +03:00
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
type otTracer struct {
|
2023-01-16 23:41:56 +03:00
|
|
|
opts tracer.Options
|
2023-09-01 08:08:34 +03:00
|
|
|
tracer ot.Tracer
|
2022-03-05 19:10:19 +03:00
|
|
|
}
|
|
|
|
|
2023-09-01 08:08:34 +03:00
|
|
|
func (t *otTracer) Name() string {
|
|
|
|
return t.opts.Name
|
2022-03-05 19:10:19 +03:00
|
|
|
}
|
|
|
|
|
2023-09-01 08:08:34 +03:00
|
|
|
func (t *otTracer) Flush(ctx context.Context) error {
|
2023-08-02 09:50:36 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-01 08:08:34 +03:00
|
|
|
func (t *otTracer) Init(opts ...tracer.Option) error {
|
2023-01-17 08:43:04 +03:00
|
|
|
for _, o := range opts {
|
2023-09-01 08:08:34 +03:00
|
|
|
o(&t.opts)
|
2023-01-17 08:43:04 +03:00
|
|
|
}
|
|
|
|
|
2023-09-01 08:08:34 +03:00
|
|
|
if tr, ok := t.opts.Context.Value(tracerKey{}).(ot.Tracer); ok {
|
|
|
|
t.tracer = tr
|
2023-01-17 08:43:04 +03:00
|
|
|
} else {
|
|
|
|
return errors.New("Tracer option missing")
|
|
|
|
}
|
|
|
|
|
2022-03-05 19:10:19 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-06 12:36:29 +03:00
|
|
|
type spanContext interface {
|
|
|
|
TraceID() idStringer
|
|
|
|
SpanID() idStringer
|
|
|
|
}
|
|
|
|
|
2023-09-01 08:08:34 +03:00
|
|
|
func (t *otTracer) Start(ctx context.Context, name string, opts ...tracer.SpanOption) (context.Context, tracer.Span) {
|
2023-01-18 10:05:02 +03:00
|
|
|
options := tracer.NewSpanOptions(opts...)
|
2023-09-01 08:08:34 +03:00
|
|
|
var span ot.Span
|
2023-01-18 10:05:02 +03:00
|
|
|
switch options.Kind {
|
2023-09-01 08:08:34 +03:00
|
|
|
case tracer.SpanKindUnspecified:
|
|
|
|
ctx, span = t.startSpanFromAny(ctx, name)
|
|
|
|
case tracer.SpanKindInternal:
|
|
|
|
ctx, span = t.startSpanFromContext(ctx, name)
|
2023-01-18 10:05:02 +03:00
|
|
|
case tracer.SpanKindClient, tracer.SpanKindProducer:
|
2023-09-01 08:08:34 +03:00
|
|
|
ctx, span = t.startSpanFromOutgoingContext(ctx, name)
|
2023-01-18 10:05:02 +03:00
|
|
|
case tracer.SpanKindServer, tracer.SpanKindConsumer:
|
2023-09-01 08:08:34 +03:00
|
|
|
ctx, span = t.startSpanFromIncomingContext(ctx, name)
|
2023-01-18 10:05:02 +03:00
|
|
|
}
|
2024-03-06 12:36:29 +03:00
|
|
|
|
2023-09-01 08:08:34 +03:00
|
|
|
sp := &otSpan{span: span, opts: options}
|
2024-03-06 12:36:29 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-01 08:08:34 +03:00
|
|
|
return tracer.NewSpanContext(ctx, sp), sp
|
2023-01-18 10:05:02 +03:00
|
|
|
}
|
|
|
|
|
2024-03-06 12:36:29 +03:00
|
|
|
type idStringer struct {
|
|
|
|
s string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s idStringer) String() string {
|
|
|
|
return s.s
|
|
|
|
}
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
type otSpan struct {
|
2023-09-01 08:08:34 +03:00
|
|
|
span ot.Span
|
2024-03-06 12:36:29 +03:00
|
|
|
spanID string
|
|
|
|
traceID string
|
2023-01-18 10:05:02 +03:00
|
|
|
opts tracer.SpanOptions
|
|
|
|
status tracer.SpanStatus
|
|
|
|
statusMsg string
|
2023-01-16 23:41:56 +03:00
|
|
|
}
|
|
|
|
|
2024-03-06 12:36:29 +03:00
|
|
|
func (os *otSpan) TraceID() string {
|
|
|
|
return os.traceID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (os *otSpan) SpanID() string {
|
|
|
|
return os.spanID
|
|
|
|
}
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
func (os *otSpan) SetStatus(st tracer.SpanStatus, msg string) {
|
|
|
|
os.status = st
|
|
|
|
os.statusMsg = msg
|
2023-01-16 23:41:56 +03:00
|
|
|
}
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
func (os *otSpan) Status() (tracer.SpanStatus, string) {
|
|
|
|
return os.status, os.statusMsg
|
2023-01-16 23:41:56 +03:00
|
|
|
}
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
func (os *otSpan) Tracer() tracer.Tracer {
|
|
|
|
return &otTracer{tracer: os.span.Tracer()}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (os *otSpan) Finish(opts ...tracer.SpanOption) {
|
2023-09-01 08:08:34 +03:00
|
|
|
if len(os.opts.Labels)%2 != 0 {
|
|
|
|
os.opts.Labels = os.opts.Labels[:len(os.opts.Labels)-1]
|
|
|
|
}
|
2023-09-01 15:39:30 +03:00
|
|
|
os.opts.Labels = tracer.UniqLabels(os.opts.Labels)
|
2023-09-01 08:08:34 +03:00
|
|
|
for idx := 0; idx < len(os.opts.Labels); idx += 2 {
|
|
|
|
switch os.opts.Labels[idx] {
|
|
|
|
case "err":
|
|
|
|
os.status = tracer.SpanStatusError
|
|
|
|
os.statusMsg = fmt.Sprintf("%v", os.opts.Labels[idx+1])
|
|
|
|
case "error":
|
|
|
|
continue
|
|
|
|
case "X-Request-Id", "x-request-id":
|
|
|
|
os.span.SetTag("x-request-id", os.opts.Labels[idx+1])
|
2023-09-01 15:39:30 +03:00
|
|
|
case "rpc.call", "rpc.call_type", "rpc.flavor", "rpc.service", "rpc.method",
|
|
|
|
"sdk.database", "db.statement", "db.args", "db.query", "db.method",
|
2023-09-01 16:10:08 +03:00
|
|
|
"messaging.destination.name", "messaging.source.name", "messaging.operation":
|
2023-09-01 15:39:30 +03:00
|
|
|
os.span.SetTag(fmt.Sprintf("%v", os.opts.Labels[idx]), os.opts.Labels[idx+1])
|
2023-09-01 08:08:34 +03:00
|
|
|
default:
|
|
|
|
os.span.LogKV(os.opts.Labels[idx], os.opts.Labels[idx+1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if os.status == tracer.SpanStatusError {
|
|
|
|
os.span.SetTag("error", true)
|
|
|
|
os.span.LogKV("error", os.statusMsg)
|
2023-01-16 23:41:56 +03:00
|
|
|
}
|
2023-09-01 15:39:30 +03:00
|
|
|
os.span.SetTag("span.kind", os.opts.Kind)
|
2023-01-16 23:41:56 +03:00
|
|
|
os.span.Finish()
|
|
|
|
}
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
func (os *otSpan) AddEvent(name string, opts ...tracer.EventOption) {
|
2023-09-01 08:08:34 +03:00
|
|
|
os.span.LogFields(otlog.Event(name))
|
2023-01-16 23:41:56 +03:00
|
|
|
}
|
|
|
|
|
2023-10-12 01:27:42 +03:00
|
|
|
func (os *otSpan) AddLogs(kv ...interface{}) {
|
|
|
|
os.span.LogKV(kv...)
|
|
|
|
}
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
func (os *otSpan) Context() context.Context {
|
2023-09-01 08:08:34 +03:00
|
|
|
return ot.ContextWithSpan(context.Background(), os.span)
|
2023-01-16 23:41:56 +03:00
|
|
|
}
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
func (os *otSpan) SetName(name string) {
|
2023-01-16 23:41:56 +03:00
|
|
|
os.span = os.span.SetOperationName(name)
|
|
|
|
}
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
func (os *otSpan) SetLabels(labels ...interface{}) {
|
|
|
|
os.opts.Labels = labels
|
2023-01-16 23:41:56 +03:00
|
|
|
}
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
func (os *otSpan) Kind() tracer.SpanKind {
|
|
|
|
return os.opts.Kind
|
2022-03-05 19:10:19 +03:00
|
|
|
}
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
func (os *otSpan) AddLabels(labels ...interface{}) {
|
|
|
|
os.opts.Labels = append(os.opts.Labels, labels...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTracer(opts ...tracer.Option) *otTracer {
|
2022-03-05 19:10:19 +03:00
|
|
|
options := tracer.NewOptions(opts...)
|
2023-01-18 10:05:02 +03:00
|
|
|
return &otTracer{opts: options}
|
2022-03-05 19:10:19 +03:00
|
|
|
}
|
|
|
|
|
2023-09-01 08:08:34 +03:00
|
|
|
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...)
|
|
|
|
}
|
|
|
|
|
|
|
|
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...)
|
2022-03-05 19:10:19 +03:00
|
|
|
}
|
|
|
|
|
2023-09-01 08:08:34 +03:00
|
|
|
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()))
|
2023-01-18 10:05:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
md := metadata.New(1)
|
|
|
|
|
2023-09-01 08:08:34 +03:00
|
|
|
sp := t.tracer.StartSpan(name, opts...)
|
|
|
|
if err := sp.Tracer().Inject(sp.Context(), ot.TextMap, ot.TextMapCarrier(md)); err != nil {
|
2023-01-18 10:05:02 +03:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-09-01 08:08:34 +03:00
|
|
|
ctx = ot.ContextWithSpan(ctx, sp)
|
2023-01-18 10:05:02 +03:00
|
|
|
|
|
|
|
return ctx, sp
|
|
|
|
}
|
|
|
|
|
2023-09-01 08:08:34 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2022-03-05 19:10:19 +03:00
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
md, ok := metadata.FromOutgoingContext(ctx)
|
2023-09-01 08:08:34 +03:00
|
|
|
|
|
|
|
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 {
|
|
|
|
parentCtx = spanCtx
|
|
|
|
}
|
2023-01-18 10:05:02 +03:00
|
|
|
}
|
2022-03-05 19:10:19 +03:00
|
|
|
|
2023-09-01 08:08:34 +03:00
|
|
|
if parentCtx != nil {
|
|
|
|
opts = append(opts, ot.ChildOf(parentCtx))
|
|
|
|
}
|
2022-03-05 19:10:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nmd := metadata.Copy(md)
|
|
|
|
|
2023-09-01 08:08:34 +03:00
|
|
|
sp := t.tracer.StartSpan(name, opts...)
|
|
|
|
if err := sp.Tracer().Inject(sp.Context(), ot.TextMap, ot.TextMapCarrier(nmd)); err != nil {
|
2023-01-18 10:05:02 +03:00
|
|
|
return nil, nil
|
2022-03-05 19:10:19 +03:00
|
|
|
}
|
|
|
|
|
2023-09-01 08:08:34 +03:00
|
|
|
ctx = metadata.NewOutgoingContext(ot.ContextWithSpan(ctx, sp), nmd)
|
2022-03-05 19:10:19 +03:00
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
return ctx, sp
|
2022-03-05 19:10:19 +03:00
|
|
|
}
|
|
|
|
|
2023-09-01 08:08:34 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2022-03-05 19:10:19 +03:00
|
|
|
|
|
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
2023-09-01 08:08:34 +03:00
|
|
|
|
|
|
|
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 {
|
|
|
|
parentCtx = spanCtx
|
|
|
|
}
|
2023-01-18 10:05:02 +03:00
|
|
|
}
|
2022-03-05 19:10:19 +03:00
|
|
|
|
2023-09-01 08:08:34 +03:00
|
|
|
if parentCtx != nil {
|
|
|
|
opts = append(opts, ot.ChildOf(parentCtx))
|
|
|
|
}
|
2022-03-05 19:10:19 +03:00
|
|
|
}
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
nmd := metadata.Copy(md)
|
2022-03-05 19:10:19 +03:00
|
|
|
|
2023-09-01 08:08:34 +03:00
|
|
|
sp := t.tracer.StartSpan(name, opts...)
|
|
|
|
if err := sp.Tracer().Inject(sp.Context(), ot.TextMap, ot.TextMapCarrier(nmd)); err != nil {
|
2023-01-18 10:05:02 +03:00
|
|
|
return nil, nil
|
2022-03-05 19:10:19 +03:00
|
|
|
}
|
|
|
|
|
2023-09-01 08:08:34 +03:00
|
|
|
ctx = metadata.NewIncomingContext(ot.ContextWithSpan(ctx, sp), nmd)
|
2022-03-05 19:10:19 +03:00
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
return ctx, sp
|
2022-03-05 19:10:19 +03:00
|
|
|
}
|