-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtee_tracer.go
260 lines (227 loc) · 7.58 KB
/
tee_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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Radu Berinde (radu@cockroachlabs.com)
package tracing
import (
"fmt"
basictracer "github.com/opentracing/basictracer-go"
opentracing "github.com/opentracing/opentracing-go"
otlog "github.com/opentracing/opentracing-go/log"
"github.com/pkg/errors"
)
// TeeTracer is an opentracing.Tracer that sends events to multiple Tracers.
type TeeTracer struct {
tracers []opentracing.Tracer
}
var _ opentracing.Tracer = &TeeTracer{}
// NewTeeTracer creates a Tracer that sends events to multiple Tracers.
//
// Note that only the span from the first tracer is used for serialization
// purposes (Inject/Extract).
func NewTeeTracer(tracers ...opentracing.Tracer) *TeeTracer {
return &TeeTracer{tracers: tracers}
}
// option is a wrapper for opentracing.StartSpanOptions that
// implements opentracing.StartSpanOption. This is the only way to pass options
// to StartSpan.
type option opentracing.StartSpanOptions
var _ opentracing.StartSpanOption = &option{}
// Apply is part of the StartSpanOption interface.
func (o *option) Apply(sso *opentracing.StartSpanOptions) {
*sso = opentracing.StartSpanOptions(*o)
}
// StartSpan is part of the opentracing.Tracer interface.
func (t *TeeTracer) StartSpan(
operationName string, opts ...opentracing.StartSpanOption,
) opentracing.Span {
// Form the StartSpanOptions from the opts.
sso := opentracing.StartSpanOptions{}
for _, o := range opts {
o.Apply(&sso)
}
spans := make([]spanWithOpt, len(t.tracers))
for i := 0; i < len(t.tracers); i++ {
o := option(sso)
// Replace any references to the TeeSpanContext with the corresponding
// SpanContext for tracer i.
o.References = make([]opentracing.SpanReference, len(sso.References))
for j := range sso.References {
tsc := sso.References[j].ReferencedContext.(TeeSpanContext)
o.References[j].ReferencedContext = tsc.contexts[i]
}
spans[i] = spanWithOpt{
Span: t.tracers[i].StartSpan(operationName, &o),
owned: true,
}
}
return &TeeSpan{tracer: t, spans: spans}
}
type spanWithOpt struct {
opentracing.Span
// If owned is set, the span needs to be Finish()ed by the TeeSpan that
// contains it.
owned bool
}
// CreateSpanFrom creates a TeeSpan that will contains the passed-in spans.
// Depending on span.owned, the TeeSpan takes ownership of some of the child
// spans.
func (t *TeeTracer) CreateSpanFrom(spans ...spanWithOpt) *TeeSpan {
if len(spans) != len(t.tracers) {
panic(fmt.Sprintf("non-matching spans for TeeTracer. spans: %d, tracers: %d",
len(spans), len(t.tracers)))
}
for i, tracer := range t.tracers {
if tracer != spans[i].Tracer() {
panic(fmt.Sprintf("non-matching span at position: %d", i))
}
}
return &TeeSpan{tracer: t, spans: spans}
}
// Inject is part of the opentracing.Tracer interface.
func (t *TeeTracer) Inject(
sc opentracing.SpanContext, format interface{}, carrier interface{},
) error {
tsc, ok := sc.(TeeSpanContext)
if !ok {
return errors.Errorf("SpanContext type %T incompatible with TeeTracer", sc)
}
// TODO(radu): we only serialize the span for the first tracer. Ideally we
// would produce our own format that includes serializations for all the
// underlying spans.
return t.tracers[0].Inject(tsc.contexts[0], format, carrier)
}
// Extract is part of the opentracing.Tracer interface.
func (t *TeeTracer) Extract(
format interface{}, carrier interface{},
) (opentracing.SpanContext, error) {
decoded, err := t.tracers[0].Extract(format, carrier)
if err != nil {
return nil, err
}
contexts := make([]opentracing.SpanContext, len(t.tracers))
contexts[0] = decoded
// We use the fact that we only use tracers based on basictracer spans and
// duplicate the same span.
ctxBasic := decoded.(basictracer.SpanContext)
for i := 1; i < len(contexts); i++ {
ctxCopy := ctxBasic
ctxCopy.Baggage = make(map[string]string)
for k, v := range ctxBasic.Baggage {
ctxCopy.Baggage[k] = v
}
contexts[i] = ctxCopy
}
return TeeSpanContext{contexts: contexts}, nil
}
// TeeSpanContext is an opentracing.SpanContext that keeps track of SpanContexts
// from multiple tracers.
type TeeSpanContext struct {
contexts []opentracing.SpanContext
}
var _ opentracing.SpanContext = TeeSpanContext{}
// ForeachBaggageItem is part of the opentracing.SpanContext interface.
func (tsc TeeSpanContext) ForeachBaggageItem(handler func(k, v string) bool) {
// All underlying SpanContexts have the same baggage.
tsc.contexts[0].ForeachBaggageItem(handler)
}
// TeeSpan is the opentracing.Span implementation used by the TeeTracer.
type TeeSpan struct {
tracer *TeeTracer
spans []spanWithOpt
}
var _ opentracing.Span = &TeeSpan{}
// Finish is part of the opentracing.Span interface.
func (ts *TeeSpan) Finish() {
for _, sp := range ts.spans {
if sp.owned {
sp.Finish()
}
}
}
// FinishWithOptions is part of the opentracing.Span interface.
func (ts *TeeSpan) FinishWithOptions(opts opentracing.FinishOptions) {
for _, sp := range ts.spans {
sp.FinishWithOptions(opts)
}
}
// Context is part of the opentracing.Span interface.
func (ts *TeeSpan) Context() opentracing.SpanContext {
// Build a composite context.
ctx := TeeSpanContext{}
ctx.contexts = make([]opentracing.SpanContext, len(ts.spans))
for i := range ts.spans {
ctx.contexts[i] = ts.spans[i].Context()
}
return ctx
}
// SetOperationName is part of the opentracing.Span interface.
func (ts *TeeSpan) SetOperationName(operationName string) opentracing.Span {
for _, sp := range ts.spans {
sp.SetOperationName(operationName)
}
return ts
}
// SetTag is part of the opentracing.Span interface.
func (ts *TeeSpan) SetTag(key string, value interface{}) opentracing.Span {
for _, sp := range ts.spans {
sp.SetTag(key, value)
}
return ts
}
// LogFields is part of the opentracing.Span interface.
func (ts *TeeSpan) LogFields(fields ...otlog.Field) {
for _, sp := range ts.spans {
sp.LogFields(fields...)
}
}
// LogKV is part of the opentracing.Span interface.
func (ts *TeeSpan) LogKV(alternatingKeyValues ...interface{}) {
for _, sp := range ts.spans {
sp.LogKV(alternatingKeyValues...)
}
}
// LogEvent is part of the opentracing.Span interface.
//
// Deprecated: use LogKV/LogFields.
func (ts *TeeSpan) LogEvent(event string) {
panic("deprecated")
}
// LogEventWithPayload is part of the opentracing.Span interface.
//
// Deprecated: use LogKV/LogFields.
func (ts *TeeSpan) LogEventWithPayload(event string, payload interface{}) {
panic("deprecated")
}
// Log is part of the opentracing.Span interface.
//
// Deprecated: use LogKV/LogFields.
func (ts *TeeSpan) Log(data opentracing.LogData) {
panic("deprecated")
}
// SetBaggageItem is part of the opentracing.Span interface.
func (ts *TeeSpan) SetBaggageItem(restrictedKey, value string) opentracing.Span {
for _, sp := range ts.spans {
sp.SetBaggageItem(restrictedKey, value)
}
return ts
}
// BaggageItem is part of the opentracing.Span interface.
func (ts *TeeSpan) BaggageItem(restrictedKey string) string {
return ts.spans[0].BaggageItem(restrictedKey)
}
// Tracer is part of the opentracing.Span interface.
func (ts *TeeSpan) Tracer() opentracing.Tracer {
return ts.tracer
}