forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shadow.go
177 lines (154 loc) · 5.25 KB
/
shadow.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
// Copyright 2017 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)
//
// A "shadow" tracer can be any opentracing.Tracer implementation that is used
// in addition to the normal functionality of our tracer. It works by attaching
// a shadow span to every span, and attaching a shadow context to every span
// context. When injecting a span context, we encapsulate the shadow context
// inside ours.
package tracing
import (
"fmt"
"os"
"github.com/cockroachdb/cockroach/pkg/settings"
lightstep "github.com/lightstep/lightstep-tracer-go"
opentracing "github.com/opentracing/opentracing-go"
zipkin "github.com/openzipkin/zipkin-go-opentracing"
)
type shadowTracerManager interface {
Name() string
Close(tr opentracing.Tracer)
}
type lightStepManager struct{}
func (lightStepManager) Name() string {
return "lightstep"
}
func (lightStepManager) Close(tr opentracing.Tracer) {
// TODO(radu): these calls are not reliable. FlushLightstepTracer exits
// immediately if a flush is in progress (see
// github.com/lightstep/lightstep-tracer-go/issues/89), and CloseTracer always
// exits immediately (see
// https://github.com/lightstep/lightstep-tracer-go/pull/85#discussion_r123800322).
_ = lightstep.FlushLightStepTracer(tr)
_ = lightstep.CloseTracer(tr)
}
type zipkinManager struct {
collector zipkin.Collector
}
func (*zipkinManager) Name() string {
return "zipkin"
}
func (m *zipkinManager) Close(tr opentracing.Tracer) {
_ = m.collector.Close()
}
type shadowTracer struct {
opentracing.Tracer
manager shadowTracerManager
}
func (st *shadowTracer) Typ() string {
return st.manager.Name()
}
func (st *shadowTracer) Close() {
st.manager.Close(st)
}
// linkShadowSpan creates and links a Shadow span to the passed-in span (i.e.
// fills in s.shadowTr and s.shadowSpan). This should only be called when
// shadow tracing is enabled.
//
// The Shadow span will have a parent if parentShadowCtx is not nil.
// parentType is ignored if parentShadowCtx is nil.
//
// The tags from s are copied to the Shadow span.
func linkShadowSpan(
s *span,
shadowTr *shadowTracer,
parentShadowCtx opentracing.SpanContext,
parentType opentracing.SpanReferenceType,
) {
// Create the shadow lightstep span.
var opts []opentracing.StartSpanOption
// Replicate the options, using the lightstep context in the reference.
opts = append(opts, opentracing.StartTime(s.startTime))
if s.mu.tags != nil {
opts = append(opts, s.mu.tags)
}
if parentShadowCtx != nil {
opts = append(opts, opentracing.SpanReference{
Type: parentType,
ReferencedContext: parentShadowCtx,
})
}
s.shadowTr = shadowTr
s.shadowSpan = shadowTr.StartSpan(s.operation, opts...)
}
var lightStepToken = settings.RegisterStringSetting(
"trace.lightstep.token",
"if set, traces go to Lightstep using this token",
"",
)
func createLightStepTracer(token string) (shadowTracerManager, opentracing.Tracer) {
return lightStepManager{}, lightstep.NewTracer(lightstep.Options{
AccessToken: token,
MaxLogsPerSpan: maxLogsPerSpan,
MaxBufferedSpans: 10000,
UseGRPC: true,
})
}
var zipkinCollector = settings.RegisterStringSetting(
"trace.zipkin.collector",
"if set, traces go to the given Zipkin instance (example: '127.0.0.1:9411'); ignored if trace.lightstep.token is set.",
"",
)
func createZipkinTracer(collectorAddr string) (shadowTracerManager, opentracing.Tracer) {
// Create our HTTP collector.
collector, err := zipkin.NewHTTPCollector(
fmt.Sprintf("http://%s/api/v1/spans", collectorAddr),
zipkin.HTTPLogger(zipkin.LoggerFunc(func(keyvals ...interface{}) error {
// These logs are from the collector (e.g. errors sending data, dropped
// traces). We can't use `log` from this package so print them to stderr.
toPrint := append([]interface{}{"Zipkin collector"}, keyvals...)
fmt.Fprintln(os.Stderr, toPrint)
return nil
})),
)
if err != nil {
panic(err)
}
// Create our recorder.
recorder := zipkin.NewRecorder(collector, false /* !debug */, "0.0.0.0:0", "cockroach")
// Create our tracer.
zipkinTr, err := zipkin.NewTracer(recorder)
if err != nil {
panic(err)
}
return &zipkinManager{collector: collector}, zipkinTr
}
// We don't call OnChange inline above because it causes an "initialization
// loop" compile error.
var _ = lightStepToken.OnChange(updateShadowTracers)
var _ = zipkinCollector.OnChange(updateShadowTracers)
func updateShadowTracer(t *Tracer) {
if lsToken := lightStepToken.Get(); lsToken != "" {
t.setShadowTracer(createLightStepTracer(lsToken))
} else if zipkinAddr := zipkinCollector.Get(); zipkinAddr != "" {
t.setShadowTracer(createZipkinTracer(zipkinAddr))
} else {
t.setShadowTracer(nil, nil)
}
}
func updateShadowTracers() {
tracerRegistry.ForEach(updateShadowTracer)
}