/
tracer.go
85 lines (68 loc) · 1.85 KB
/
tracer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package opentracing
import (
"context"
"github.com/iredelmeier/opentelemetry-playground/trace"
"github.com/opentracing/opentracing-go"
)
const (
traceParentKey = "traceparent"
)
type Tracer struct {
exporter trace.SpanExporter
}
func NewTracer(opts ...TracerOption) opentracing.Tracer {
c := newTracerConfig(opts...)
return Tracer{
exporter: c.exporter,
}
}
func (t Tracer) StartSpan(operationName string, opts ...opentracing.StartSpanOption) opentracing.Span {
var config opentracing.StartSpanOptions
for _, opt := range opts {
opt.Apply(&config)
}
sso := []trace.StartSpanOption{
trace.WithStartTime(config.StartTime),
}
for _, ref := range config.References {
if sc, ok := ref.ReferencedContext.(*SpanContext); ok {
parentOpts := []trace.StartSpanOption{
trace.WithTraceID(sc.traceContext.TraceID),
trace.WithParentID(sc.traceContext.SpanID),
}
sso = append(sso, parentOpts...)
break
}
}
ctx := trace.ContextWithSpanExporter(context.Background(), t.exporter)
ctx = trace.StartSpan(ctx, operationName, sso...)
return Span{
tracer: t,
ctx: ctx,
}
}
func (t Tracer) Inject(spanContext opentracing.SpanContext, format interface{}, carrier interface{}) error {
switch sc := spanContext.(type) {
case SpanContext:
switch format {
case opentracing.Binary:
return sc.injectBinary(carrier)
case opentracing.TextMap, opentracing.HTTPHeaders:
return sc.injectTextMap(carrier)
default:
return opentracing.ErrUnsupportedFormat
}
default:
return opentracing.ErrSpanContextCorrupted
}
}
func (t Tracer) Extract(format interface{}, carrier interface{}) (opentracing.SpanContext, error) {
switch format {
case opentracing.Binary:
return extractBinary(carrier)
case opentracing.TextMap, opentracing.HTTPHeaders:
return extractTextMap(carrier)
default:
return nil, opentracing.ErrUnsupportedFormat
}
}