2022-03-05 19:10:19 +03:00
|
|
|
package opentracing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-01-17 08:43:04 +03:00
|
|
|
"errors"
|
2024-07-17 17:37:06 +03:00
|
|
|
"strings"
|
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"
|
2024-03-14 23:35:38 +03:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2022-03-05 19:10:19 +03:00
|
|
|
"go.unistack.org/micro/v3/metadata"
|
|
|
|
"go.unistack.org/micro/v3/tracer"
|
2024-09-16 14:55:01 +03:00
|
|
|
"go.unistack.org/micro/v3/util/sort"
|
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-07-17 17:37:06 +03:00
|
|
|
type otSpanContext 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...)
|
2024-07-06 00:19:38 +03:00
|
|
|
|
2024-05-09 19:30:32 +03:00
|
|
|
if len(options.Labels)%2 != 0 {
|
|
|
|
options.Labels = options.Labels[:len(options.Labels)-1]
|
|
|
|
}
|
2024-07-06 00:19:38 +03:00
|
|
|
|
|
|
|
for _, fn := range t.opts.ContextAttrFuncs {
|
|
|
|
options.Labels = append(options.Labels, fn(ctx)...)
|
|
|
|
}
|
|
|
|
|
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()
|
2024-07-17 17:37:06 +03:00
|
|
|
|
|
|
|
var traceID, spanID, parentID string
|
|
|
|
|
|
|
|
if v, ok := spctx.(otSpanContext); ok {
|
|
|
|
traceID = v.TraceID().String()
|
|
|
|
spanID = v.SpanID().String()
|
2024-03-06 12:36:29 +03:00
|
|
|
} else {
|
2024-07-17 17:37:06 +03:00
|
|
|
carrier := make(map[string]string, 1)
|
|
|
|
_ = span.Tracer().Inject(spctx, ot.TextMap, ot.TextMapCarrier(carrier))
|
|
|
|
for k, v := range carrier {
|
|
|
|
switch k {
|
2024-07-17 17:51:51 +03:00
|
|
|
case "mockpfx-ids-sampled":
|
|
|
|
continue
|
|
|
|
case "mockpfx-ids-spanid":
|
|
|
|
spanID = v
|
|
|
|
case "mockpfx-ids-traceid":
|
|
|
|
traceID = v
|
2024-07-17 17:37:06 +03:00
|
|
|
default: // reasonable default
|
|
|
|
p := strings.Split(v, ":")
|
|
|
|
traceID = p[0]
|
|
|
|
spanID = p[1]
|
|
|
|
parentID = p[2]
|
|
|
|
case "uber-trace-id": // jaeger trace span
|
|
|
|
p := strings.Split(v, ":")
|
|
|
|
traceID = p[0]
|
|
|
|
spanID = p[1]
|
|
|
|
parentID = p[2]
|
|
|
|
}
|
2024-03-06 12:36:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-17 17:37:06 +03:00
|
|
|
sp.traceID = traceID
|
|
|
|
sp.spanID = spanID
|
|
|
|
sp.parentID = parentID
|
|
|
|
|
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
|
2024-07-17 17:37:06 +03:00
|
|
|
parentID string
|
2023-01-18 10:05:02 +03:00
|
|
|
opts tracer.SpanOptions
|
|
|
|
status tracer.SpanStatus
|
|
|
|
statusMsg string
|
2024-03-17 23:07:06 +03:00
|
|
|
labels []interface{}
|
2024-05-09 19:30:32 +03:00
|
|
|
finished bool
|
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
|
|
|
|
}
|
|
|
|
|
2024-07-17 17:38:14 +03:00
|
|
|
func (os *otSpan) ParentID() string {
|
|
|
|
return os.parentID
|
|
|
|
}
|
|
|
|
|
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) {
|
2024-05-09 19:30:32 +03:00
|
|
|
if os.finished {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-15 11:34:31 +03:00
|
|
|
options := os.opts
|
2024-05-09 19:30:32 +03:00
|
|
|
|
|
|
|
options.Status = os.status
|
|
|
|
options.StatusMsg = os.statusMsg
|
|
|
|
options.Labels = append(options.Labels, os.labels...)
|
|
|
|
|
2024-03-15 11:34:31 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
2023-09-01 08:08:34 +03:00
|
|
|
}
|
2024-03-15 11:34:31 +03:00
|
|
|
|
2024-05-04 19:24:10 +03:00
|
|
|
if !options.Record {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-09 19:30:32 +03:00
|
|
|
if len(options.Labels)%2 != 0 {
|
|
|
|
options.Labels = options.Labels[:len(options.Labels)-1]
|
|
|
|
}
|
|
|
|
|
2024-09-16 14:55:01 +03:00
|
|
|
os.opts.Labels = sort.Uniq(os.opts.Labels)
|
|
|
|
|
2024-05-09 19:30:32 +03:00
|
|
|
l := len(options.Labels)
|
2024-03-17 00:51:29 +03:00
|
|
|
for idx := 0; idx < l; idx++ {
|
2024-05-09 19:30:32 +03:00
|
|
|
switch lt := options.Labels[idx].(type) {
|
2024-03-15 11:34:31 +03:00
|
|
|
case attribute.KeyValue:
|
|
|
|
os.span.SetTag(string(lt.Key), lt.Value.AsInterface())
|
|
|
|
case string:
|
2024-03-17 00:51:29 +03:00
|
|
|
if l > idx+1 {
|
2024-05-09 19:30:32 +03:00
|
|
|
os.span.SetTag(lt, options.Labels[idx+1])
|
2024-03-15 11:34:31 +03:00
|
|
|
idx++
|
|
|
|
}
|
2024-03-12 12:38:40 +03:00
|
|
|
}
|
2023-09-01 08:08:34 +03:00
|
|
|
}
|
2024-03-15 11:34:31 +03:00
|
|
|
|
2024-05-09 19:30:32 +03:00
|
|
|
if options.Status == tracer.SpanStatusError {
|
2023-09-01 08:08:34 +03:00
|
|
|
os.span.SetTag("error", true)
|
2024-05-09 19:30:32 +03:00
|
|
|
os.span.LogKV("error", options.StatusMsg)
|
2023-01-16 23:41:56 +03:00
|
|
|
}
|
2024-05-09 19:30:32 +03:00
|
|
|
|
2024-03-15 11:34:31 +03:00
|
|
|
os.span.SetTag("span.kind", options.Kind)
|
2023-01-16 23:41:56 +03:00
|
|
|
os.span.Finish()
|
2024-05-09 19:30:32 +03:00
|
|
|
|
|
|
|
os.finished = true
|
2023-01-16 23:41:56 +03:00
|
|
|
}
|
|
|
|
|
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{}) {
|
2024-03-17 23:07:06 +03:00
|
|
|
os.labels = append(os.labels, labels...)
|
2023-01-18 10:05:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2024-07-22 23:39:22 +03:00
|
|
|
smd := metadata.New(1)
|
2022-03-05 19:10:19 +03:00
|
|
|
|
2023-09-01 08:08:34 +03:00
|
|
|
sp := t.tracer.StartSpan(name, opts...)
|
2024-07-22 23:39:22 +03:00
|
|
|
if err := sp.Tracer().Inject(sp.Context(), ot.TextMap, ot.TextMapCarrier(smd)); err != nil {
|
2023-01-18 10:05:02 +03:00
|
|
|
return nil, nil
|
2022-03-05 19:10:19 +03:00
|
|
|
}
|
2024-07-22 23:39:22 +03:00
|
|
|
for k, v := range smd {
|
|
|
|
nmd.Set(k, v)
|
|
|
|
}
|
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)
|
2024-07-22 23:39:22 +03:00
|
|
|
smd := metadata.New(1)
|
2022-03-05 19:10:19 +03:00
|
|
|
|
2023-09-01 08:08:34 +03:00
|
|
|
sp := t.tracer.StartSpan(name, opts...)
|
2024-07-22 23:39:22 +03:00
|
|
|
if err := sp.Tracer().Inject(sp.Context(), ot.TextMap, ot.TextMapCarrier(smd)); err != nil {
|
2023-01-18 10:05:02 +03:00
|
|
|
return nil, nil
|
2022-03-05 19:10:19 +03:00
|
|
|
}
|
2024-07-22 23:39:22 +03:00
|
|
|
for k, v := range smd {
|
|
|
|
nmd.Set(k, v)
|
|
|
|
}
|
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
|
|
|
}
|