2022-03-05 19:10:19 +03:00
|
|
|
package opentracing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-01-17 08:43:04 +03:00
|
|
|
"errors"
|
2022-03-05 19:10:19 +03:00
|
|
|
|
2023-01-16 23:41:56 +03:00
|
|
|
"github.com/opentracing/opentracing-go"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
tracer opentracing.Tracer
|
2022-03-05 19:10:19 +03:00
|
|
|
}
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
func (ot *otTracer) Name() string {
|
2022-03-05 19:10:19 +03:00
|
|
|
return ot.opts.Name
|
|
|
|
}
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
func (ot *otTracer) Init(opts ...tracer.Option) error {
|
2023-01-17 08:43:04 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&ot.opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tr, ok := ot.opts.Context.Value(tracerKey{}).(opentracing.Tracer); ok {
|
|
|
|
ot.tracer = tr
|
|
|
|
} else {
|
|
|
|
return errors.New("Tracer option missing")
|
|
|
|
}
|
|
|
|
|
2022-03-05 19:10:19 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
func (ot *otTracer) Start(ctx context.Context, name string, opts ...tracer.SpanOption) (context.Context, tracer.Span) {
|
|
|
|
options := tracer.NewSpanOptions(opts...)
|
|
|
|
var span opentracing.Span
|
|
|
|
switch options.Kind {
|
|
|
|
case tracer.SpanKindInternal, tracer.SpanKindUnspecified:
|
|
|
|
ctx, span = ot.startSpanFromContext(ctx, name)
|
|
|
|
case tracer.SpanKindClient, tracer.SpanKindProducer:
|
|
|
|
ctx, span = ot.startSpanFromOutgoingContext(ctx, name)
|
|
|
|
case tracer.SpanKindServer, tracer.SpanKindConsumer:
|
|
|
|
ctx, span = ot.startSpanFromIncomingContext(ctx, ot.tracer, name)
|
|
|
|
}
|
|
|
|
return ctx, &otSpan{span: span, opts: options}
|
|
|
|
}
|
|
|
|
|
|
|
|
type otSpan struct {
|
|
|
|
span opentracing.Span
|
|
|
|
opts tracer.SpanOptions
|
|
|
|
status tracer.SpanStatus
|
|
|
|
statusMsg string
|
2023-01-16 23:41:56 +03:00
|
|
|
}
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
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
|
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) {
|
|
|
|
if len(os.opts.Labels) > 0 {
|
|
|
|
os.span.LogKV(os.opts.Labels...)
|
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-01-16 23:41:56 +03:00
|
|
|
os.span.LogFields(log.Event(name))
|
|
|
|
}
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
func (os *otSpan) Context() context.Context {
|
|
|
|
return opentracing.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
|
|
|
}
|
|
|
|
|
|
|
|
func spanFromContext(ctx context.Context) opentracing.Span {
|
|
|
|
return opentracing.SpanFromContext(ctx)
|
|
|
|
}
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
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 := ot.tracer.StartSpan(name, opts...)
|
|
|
|
if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(md)); err != nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = opentracing.ContextWithSpan(ctx, sp)
|
|
|
|
|
|
|
|
return ctx, sp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ot *otTracer) startSpanFromOutgoingContext(ctx context.Context, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span) {
|
2022-03-05 19:10:19 +03:00
|
|
|
var parentCtx opentracing.SpanContext
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
md, ok := metadata.FromOutgoingContext(ctx)
|
|
|
|
if ok && md != nil {
|
|
|
|
if spanCtx, err := ot.tracer.Extract(opentracing.TextMap, opentracing.TextMapCarrier(md)); err == nil && ok {
|
|
|
|
parentCtx = spanCtx
|
|
|
|
}
|
2022-03-05 19:10:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if parentCtx != nil {
|
|
|
|
opts = append(opts, opentracing.ChildOf(parentCtx))
|
|
|
|
}
|
|
|
|
|
|
|
|
nmd := metadata.Copy(md)
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
sp := ot.tracer.StartSpan(name, opts...)
|
2022-03-05 19:10:19 +03:00
|
|
|
if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(nmd)); err != nil {
|
2023-01-18 10:05:02 +03:00
|
|
|
return nil, nil
|
2022-03-05 19:10:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx = metadata.NewOutgoingContext(opentracing.ContextWithSpan(ctx, sp), nmd)
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
return ctx, sp
|
2022-03-05 19:10:19 +03:00
|
|
|
}
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
func (ot *otTracer) startSpanFromIncomingContext(ctx context.Context, tracer opentracing.Tracer, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span) {
|
2022-03-05 19:10:19 +03:00
|
|
|
var parentCtx opentracing.SpanContext
|
|
|
|
|
|
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
2023-01-18 10:05:02 +03:00
|
|
|
if ok && md != nil {
|
|
|
|
if spanCtx, err := tracer.Extract(opentracing.TextMap, opentracing.TextMapCarrier(md)); err == nil {
|
|
|
|
parentCtx = spanCtx
|
|
|
|
}
|
2022-03-05 19:10:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if parentCtx != nil {
|
|
|
|
opts = append(opts, opentracing.ChildOf(parentCtx))
|
|
|
|
}
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
nmd := metadata.Copy(md)
|
2022-03-05 19:10:19 +03:00
|
|
|
|
|
|
|
sp := tracer.StartSpan(name, opts...)
|
|
|
|
if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(nmd)); err != nil {
|
2023-01-18 10:05:02 +03:00
|
|
|
return nil, nil
|
2022-03-05 19:10:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx = metadata.NewIncomingContext(opentracing.ContextWithSpan(ctx, sp), nmd)
|
|
|
|
|
2023-01-18 10:05:02 +03:00
|
|
|
return ctx, sp
|
2022-03-05 19:10:19 +03:00
|
|
|
}
|