/
tracer.go
280 lines (230 loc) · 7.01 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package instracer
import (
"context"
"encoding/binary"
"hash/crc64"
"io"
"time"
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go"
"github.com/uber/jaeger-client-go/config"
"go.opencensus.io/trace"
errors "github.com/insolar/assured-ledger/ledger-core/vanilla/throw"
"github.com/insolar/assured-ledger/ledger-core/instrumentation/inslogger"
)
const (
uint32Size = 16
uint64Size = uint32Size * 2
)
type LoggingSpan struct {
opentracing.Span
ctx context.Context
name string
spanID jaeger.SpanID
}
func (ls *LoggingSpan) Finish() {
if ls.spanID != 0 {
inslogger.FromContext(ls.ctx).Infof("span finished [%s] {SpanID: %s}",
ls.name, ls.spanID.String())
} else {
inslogger.FromContext(ls.ctx).Infof("span finished %s", ls.name)
}
ls.Span.Finish()
}
func InitWrapper(ctx context.Context, span opentracing.Span, name string) *LoggingSpan {
spanCtx, isJaegerCtx := span.Context().(jaeger.SpanContext)
if isJaegerCtx {
inslogger.FromContext(ctx).Infof("span started [%s] {SpanID: %s, TraceID: %s, ParentID: %s}",
name, spanCtx.SpanID().String(), spanCtx.TraceID().String(), spanCtx.ParentID())
return &LoggingSpan{
Span: span,
name: name,
ctx: ctx,
spanID: spanCtx.SpanID(),
}
}
inslogger.FromContext(ctx).Infof("span started %s", name)
return &LoggingSpan{
Span: span,
name: name,
ctx: ctx,
}
}
// StartSpan starts span with stored baggage and with parent span if find in context.
func StartSpan(ctx context.Context, name string, o ...opentracing.StartSpanOption) (context.Context, opentracing.Span) {
parentCtx, ctx := ParentSpanCtx(ctx)
if parentCtx.IsValid() {
o = append(o, opentracing.ChildOf(parentCtx))
}
span, ctx := opentracing.StartSpanFromContext(ctx, name, o...)
span.SetTag("insTraceID", inslogger.TraceID(ctx))
return ctx, InitWrapper(ctx, span, name)
}
func StartSpanWithSpanID(ctx context.Context, name string, spanID uint64, o ...opentracing.StartSpanOption) (context.Context, opentracing.Span) {
var (
traceID jaeger.TraceID
parentID jaeger.SpanID
)
span := opentracing.SpanFromContext(ctx)
if span != nil {
if sc, ok := span.Context().(jaeger.SpanContext); ok && sc.IsValid() {
traceID = sc.TraceID()
parentID = sc.SpanID()
}
// If it is shorter then uint32, then probably it is custom trace and it cannot be used for jaeger trace
} else if traceStr := inslogger.TraceID(ctx); len(traceStr) >= uint32Size {
var err error
if len(traceStr) > uint64Size {
traceStr = traceStr[:uint64Size]
}
traceID, err = jaeger.TraceIDFromString(traceStr)
if err != nil {
inslogger.FromContext(ctx).Error(errors.W(err, "failed to parse tracespan traceID"))
}
}
newCtx := jaeger.NewSpanContext(traceID, jaeger.SpanID(spanID), parentID, true, nil)
o = append(o, jaeger.SelfRef(newCtx))
span = opentracing.StartSpan(name, o...)
span.SetTag("insTraceID", inslogger.TraceID(ctx))
return opentracing.ContextWithSpan(ctx, span), InitWrapper(ctx, span, name)
}
type parentSpanKey struct{}
func WithParentSpan(ctx context.Context, pspan TraceSpan) context.Context {
return context.WithValue(ctx, parentSpanKey{}, pspan)
}
var (
emptyContext = jaeger.SpanContext{}
)
func ParentSpan(ctx context.Context) (traceSpan TraceSpan, ok bool) {
val := ctx.Value(parentSpanKey{})
if val == nil {
return traceSpan, false
}
traceSpan, ok = val.(TraceSpan)
return traceSpan, ok
}
var (
crc64Table = crc64.MakeTable(crc64.ISO)
)
func MakeUintSpan(input []byte) uint64 {
return crc64.Checksum(input, crc64Table)
}
func MakeBinarySpan(input []byte) []byte {
spanUint := crc64.Checksum(input, crc64Table)
binarySpanID := make([]byte, 8)
binary.LittleEndian.PutUint64(binarySpanID, spanUint)
return binarySpanID
}
func ParentSpanCtx(ctx context.Context) (jaeger.SpanContext, context.Context) {
traceSpan, ok := ParentSpan(ctx)
if !ok {
return emptyContext, ctx
}
ctx = context.WithValue(ctx, parentSpanKey{}, nil)
var (
traceID jaeger.TraceID
spanID jaeger.SpanID
err error
)
stringTrace := inslogger.TraceID(ctx)
if len(traceSpan.TraceID) > 0 {
stringTrace = string(traceSpan.TraceID)
}
// If it is shorter then uint32, then probably it is custom trace and it cannot be used for jaeger trace
if len(stringTrace) >= uint32Size {
if len(stringTrace) > uint64Size {
stringTrace = stringTrace[:uint64Size]
}
traceID, err = jaeger.TraceIDFromString(stringTrace)
if err != nil {
inslogger.FromContext(ctx).Error(errors.W(err, "failed to parse tracespan traceID"))
return emptyContext, ctx
}
}
if len(traceSpan.SpanID) > 0 {
spanID = jaeger.SpanID(binary.LittleEndian.Uint64(traceSpan.SpanID))
}
return jaeger.NewSpanContext(traceID, spanID, 0, true, nil), ctx
}
// ErrJaegerConfigEmpty is returned if jaeger configuration has empty endpoint values.
var ErrJaegerConfigEmpty = errors.New("can't create jaeger exporter, config not provided")
// NewJaegerTracer creates jaeger exporter and registers it in opencensus trace lib.
func NewJaegerTracer(
_ context.Context,
serviceName string,
nodeRef string,
agentEndpoint string,
collectorEndpoint string,
probabilityRate float64,
) (opentracing.Tracer, io.Closer, error) {
if agentEndpoint == "" && collectorEndpoint == "" {
return nil, nil, ErrJaegerConfigEmpty
}
sampler := &config.SamplerConfig{
Type: "const",
Param: 0,
}
if probabilityRate > 0 {
sampler.Type = "probabilistic"
sampler.Param = 1 / probabilityRate
} else {
trace.ApplyConfig(trace.Config{
DefaultSampler: trace.NeverSample(),
})
}
remoteReporterCfg := &config.ReporterConfig{
BufferFlushInterval: 1 * time.Second,
LocalAgentHostPort: agentEndpoint,
CollectorEndpoint: collectorEndpoint,
}
remoteReporter, err := remoteReporterCfg.NewReporter(serviceName, nil, nil)
if err != nil {
return nil, nil, errors.W(err, "failed to init new reporter")
}
cfg := config.Configuration{
ServiceName: serviceName,
Tags: []opentracing.Tag{{Key: "hostname", Value: hostname()}, {Key: "nodeRef", Value: nodeRef}},
Sampler: sampler,
}
tracer, closer, err := cfg.NewTracer(config.Reporter(remoteReporter))
if err != nil {
return nil, nil, err
}
return tracer, closer, nil
}
// ShouldRegisterJaeger calls NewJaegerTracer and returns flush function.
func ShouldRegisterJaeger(
ctx context.Context,
serviceName string,
nodeRef string,
agentEndpoint string,
collectorEndpoint string,
probabilityRate float64,
) func() {
tracer, closer, regerr := NewJaegerTracer(
ctx,
serviceName,
nodeRef,
agentEndpoint,
collectorEndpoint,
probabilityRate,
)
inslog := inslogger.FromContext(ctx)
if regerr == nil {
opentracing.SetGlobalTracer(tracer)
return func() {
inslog.Debugf("Flush jaeger for %v\n", serviceName)
closer.Close()
}
}
if regerr == ErrJaegerConfigEmpty {
inslog.Info("registerJaeger skipped: config is not provided")
} else {
inslog.Warn("registerJaeger error:", regerr)
}
return func() {}
}
// AddError add error info to span and mark span as errored
func AddError(span opentracing.Span, err error) {
span.SetTag("error", err.Error())
}