forked from vmware-archive/wavefront-opentracing-sdk-go
/
tracer.go
executable file
·221 lines (190 loc) · 5.78 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Package tracer provides an OpenTracing compliant Tracer.
package tracer
import (
"io"
"time"
"github.com/opentracing/opentracing-go"
)
// SpanReporter reports completed Spans
type SpanReporter interface {
io.Closer
ReportSpan(span RawSpan)
}
// Sampler controls whether a span should be sampled/reported
type Sampler interface {
ShouldSample(span RawSpan) bool
IsEarly() bool
}
// WavefrontTracer implements the OpenTracing `Tracer` interface.
type WavefrontTracer struct {
textPropagator *textMapPropagator
binaryPropagator *binaryPropagator
accessorPropagator *accessorPropagator
jaegerWavefrontPropagator *JaegerWavefrontPropagator
earlySamplers []Sampler
lateSamplers []Sampler
reporter SpanReporter
jeagerPropagatorTraceIdHeader string
jeagerPropagatorBaggagePrefix string
}
// Option allows customizing the WavefrontTracer.
type Option func(*WavefrontTracer)
// WithSampler defines a Sampler
func WithSampler(sampler Sampler) Option {
return func(args *WavefrontTracer) {
if sampler.IsEarly() {
args.earlySamplers = append(args.earlySamplers, sampler)
} else {
args.lateSamplers = append(args.lateSamplers, sampler)
}
}
}
func WithJaegerPropagator(traceId, baggagePrefix string) Option {
return func(args *WavefrontTracer) {
var options []JaegerOption
if traceId != "" {
options = append(options, WithTraceIdHeader(traceId))
}
if baggagePrefix != "" {
options = append(options, WithBaggagePrefix(baggagePrefix))
}
args.jaegerWavefrontPropagator = NewJaegerWavefrontPropagator(args, options)
}
}
// New creates and returns a WavefrontTracer which defers completed Spans to the given `reporter`.
func New(reporter SpanReporter, options ...Option) opentracing.Tracer {
tracer := &WavefrontTracer{
reporter: reporter,
}
tracer.textPropagator = &textMapPropagator{tracer}
tracer.binaryPropagator = &binaryPropagator{tracer}
tracer.accessorPropagator = &accessorPropagator{tracer}
for _, option := range options {
option(tracer)
}
return tracer
}
func (t *WavefrontTracer) StartSpan(operationName string, opts ...opentracing.StartSpanOption) opentracing.Span {
options := opentracing.StartSpanOptions{}
for _, o := range opts {
o.Apply(&options)
}
// Start time.
startTime := options.StartTime
if startTime.IsZero() {
startTime = time.Now()
}
// Tags.
tags := options.Tags
// Build the new span. This is the only allocation: We'll return this as an opentracing.Span.
sp := t.getSpan()
// Look for a parent in the list of References.
var firstChildOfRef SpanContext
var firstFollowsFromRef SpanContext
var refCtx SpanContext
l := len(options.References)
sp.raw.Context.Baggage = make(map[string]string, l)
for _, ref := range options.References {
for k, v := range ref.ReferencedContext.(SpanContext).Baggage {
sp.raw.Context.Baggage[k] = v
}
switch ref.Type {
case opentracing.ChildOfRef:
if len(firstChildOfRef.TraceID) == 0 {
firstChildOfRef = ref.ReferencedContext.(SpanContext)
}
case opentracing.FollowsFromRef:
if len(firstChildOfRef.TraceID) == 0 {
firstFollowsFromRef = ref.ReferencedContext.(SpanContext)
}
}
}
if len(firstChildOfRef.TraceID) != 0 {
refCtx = firstChildOfRef
} else {
refCtx = firstFollowsFromRef
}
if len(refCtx.TraceID) != 0 {
sp.raw.Context.TraceID = refCtx.TraceID
sp.raw.Context.SpanID = randomID()
sp.raw.Context.Sampled = refCtx.Sampled
sp.raw.ParentSpanID = refCtx.SpanID
} else {
// indicates a root span and that no decision has been inherited from a parent span.
// allocate new trace and span ids and perform sampling.
sp.raw.Context.TraceID, sp.raw.Context.SpanID = randomID2()
decision := t.earlySample(sp.raw)
sp.raw.Context.Sampled = &decision
}
sp.tracer = t
sp.raw.Operation = operationName
sp.raw.Start = startTime
sp.raw.Duration = -1
sp.raw.References = options.References
sp.raw.Component = defaultComponent
for k, v := range tags {
sp.SetTag(k, v)
}
return sp
}
func (t *WavefrontTracer) earlySample(raw RawSpan) bool {
if len(t.earlySamplers) == 0 && len(t.lateSamplers) == 0 {
return true
}
for _, sampler := range t.earlySamplers {
if sampler.ShouldSample(raw) {
return true
}
}
return false
}
func (t *WavefrontTracer) lateSample(raw RawSpan) bool {
for _, sampler := range t.lateSamplers {
if sampler.ShouldSample(raw) {
return true
}
}
return false
}
func (t *WavefrontTracer) getSpan() *spanImpl {
return &spanImpl{}
}
type delegatorType struct{}
// Delegator is the format to use for DelegatingCarrier.
var Delegator delegatorType
func (t *WavefrontTracer) Inject(sc opentracing.SpanContext, format interface{}, carrier interface{}) error {
if _, ok := format.(JaegerWavefrontPropagator); ok {
if t.jaegerWavefrontPropagator == nil {
return opentracing.ErrUnsupportedFormat
}
return t.jaegerWavefrontPropagator.Inject(sc, carrier)
}
switch format {
case opentracing.TextMap, opentracing.HTTPHeaders:
return t.textPropagator.Inject(sc, carrier)
case opentracing.Binary:
return t.binaryPropagator.Inject(sc, carrier)
}
if _, ok := format.(delegatorType); ok {
return t.accessorPropagator.Inject(sc, carrier)
}
return opentracing.ErrUnsupportedFormat
}
func (t *WavefrontTracer) Extract(format interface{}, carrier interface{}) (opentracing.SpanContext, error) {
if _, ok := format.(JaegerWavefrontPropagator); ok {
if t.jaegerWavefrontPropagator == nil {
return nil, opentracing.ErrUnsupportedFormat
}
return t.jaegerWavefrontPropagator.Extract(carrier)
}
switch format {
case opentracing.TextMap, opentracing.HTTPHeaders:
return t.textPropagator.Extract(carrier)
case opentracing.Binary:
return t.binaryPropagator.Extract(carrier)
}
if _, ok := format.(delegatorType); ok {
return t.accessorPropagator.Extract(carrier)
}
return nil, opentracing.ErrUnsupportedFormat
}