-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
recording.go
421 lines (380 loc) · 12.2 KB
/
recording.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package tracing
import (
"encoding/json"
"fmt"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/cockroachdb/cockroach/pkg/util/tracing/tracingpb"
"github.com/gogo/protobuf/types"
jaegerjson "github.com/jaegertracing/jaeger/model/json"
)
// RecordingType is the type of recording that a Span might be performing.
type RecordingType int32
const (
// RecordingOff means that the Span discards all events handed to it.
// Child spans created from it similarly won't be recording by default.
RecordingOff RecordingType = iota
// RecordingVerbose means that the Span is adding events passed in via LogKV
// and LogData to its recording and that derived spans will do so as well.
RecordingVerbose
// TODO(tbg): add RecordingBackground for always-on tracing.
)
type traceLogData struct {
logRecord
depth int
// timeSincePrev represents the duration since the previous log line (previous in the
// set of log lines that this is part of). This is always computed relative to a log line
// from the same Span, except for start of Span in which case the duration is computed relative
// to the last log in the parent occurring before this start. For example:
// start Span A
// log 1 // duration relative to "start Span A"
// start Span B // duration relative to "log 1"
// log 2 // duration relative to "start Span B"
// log 3 // duration relative to "log 1"
timeSincePrev time.Duration
}
type logRecord struct {
Timestamp time.Time
Msg string
}
// String formats the given spans for human consumption, showing the
// relationship using nesting and times as both relative to the previous event
// and cumulative.
//
// Child spans are inserted into the parent at the point of the child's
// StartTime; see the diagram on generateSessionTraceVTable() for the ordering
// of messages.
//
// Each log line show the time since the beginning of the trace
// and since the previous log line. Span starts are shown with special "===
// <operation>" lines. For a Span start, the time since the relative log line
// can be negative when the Span start follows a message from the parent that
// was generated after the child Span started (or even after the child
// finished).
//
// TODO(andrei): this should be unified with
// SessionTracing.generateSessionTraceVTable().
func (r Recording) String() string {
if len(r) == 0 {
return "<empty recording>"
}
var buf strings.Builder
start := r[0].StartTime
writeLogs := func(logs []traceLogData) {
for _, entry := range logs {
fmt.Fprintf(&buf, "% 10.3fms % 10.3fms%s",
1000*entry.Timestamp.Sub(start).Seconds(),
1000*entry.timeSincePrev.Seconds(),
strings.Repeat(" ", entry.depth+1))
fmt.Fprint(&buf, "", entry.Msg)
buf.WriteByte('\n')
}
}
logs := r.visitSpan(r[0], 0 /* depth */)
writeLogs(logs)
// Check if there's any orphan spans (spans for which the parent is missing).
// This shouldn't happen, but we're protecting against incomplete traces. For
// example, ingesting of remote spans through DistSQL is complex. Orphan spans
// would not be reflected in the output string at all without this.
orphans := r.OrphanSpans()
if len(orphans) > 0 {
// This shouldn't happen.
buf.WriteString("orphan spans (trace is missing spans):\n")
for _, o := range orphans {
logs := r.visitSpan(o, 0 /* depth */)
writeLogs(logs)
}
}
return buf.String()
}
// OrphanSpans returns the spans with parents missing from the recording.
func (r Recording) OrphanSpans() []tracingpb.RecordedSpan {
spanIDs := make(map[uint64]struct{})
for _, sp := range r {
spanIDs[sp.SpanID] = struct{}{}
}
var orphans []tracingpb.RecordedSpan
for i, sp := range r {
if i == 0 {
// The first Span can be a root Span. Note that any other root Span will
// be considered an orphan.
continue
}
if _, ok := spanIDs[sp.ParentSpanID]; !ok {
orphans = append(orphans, sp)
}
}
return orphans
}
// FindLogMessage returns the first log message in the recording that matches
// the given regexp. The bool return value is true if such a message is found.
func (r Recording) FindLogMessage(pattern string) (string, bool) {
re := regexp.MustCompile(pattern)
for _, sp := range r {
for _, l := range sp.Logs {
msg := l.Msg()
if re.MatchString(msg) {
return msg, true
}
}
}
return "", false
}
// FindSpan returns the Span with the given operation. The bool retval is false
// if the Span is not found.
func (r Recording) FindSpan(operation string) (tracingpb.RecordedSpan, bool) {
for _, sp := range r {
if sp.Operation == operation {
return sp, true
}
}
return tracingpb.RecordedSpan{}, false
}
// visitSpan returns the log messages for sp, and all of sp's children.
//
// All messages from a Span are kept together. Sibling spans are ordered within
// the parent in their start order.
func (r Recording) visitSpan(sp tracingpb.RecordedSpan, depth int) []traceLogData {
ownLogs := make([]traceLogData, 0, len(sp.Logs)+1)
conv := func(msg string, timestamp time.Time, ref time.Time) traceLogData {
var timeSincePrev time.Duration
if ref != (time.Time{}) {
timeSincePrev = timestamp.Sub(ref)
}
return traceLogData{
logRecord: logRecord{
Timestamp: timestamp,
Msg: msg,
},
depth: depth,
timeSincePrev: timeSincePrev,
}
}
// Add a log line representing the start of the Span.
var sb strings.Builder
sb.WriteString("=== operation:")
sb.WriteString(sp.Operation)
if len(sp.Tags) > 0 {
sb.WriteRune(' ')
}
tags := make([]string, 0, len(sp.Tags))
for k := range sp.Tags {
tags = append(tags, k)
}
sort.Strings(tags)
first := true
for _, k := range tags {
if !first {
sb.WriteRune(' ')
}
first = false
sb.WriteString(k)
sb.WriteRune(':')
sb.WriteString(sp.Tags[k])
}
ownLogs = append(ownLogs, conv(
sb.String(),
sp.StartTime,
// ref - this entries timeSincePrev will be computed when we merge it into the parent
time.Time{}))
for _, l := range sp.Logs {
lastLog := ownLogs[len(ownLogs)-1]
ownLogs = append(ownLogs, conv("event:"+l.Msg(), l.Time, lastLog.Timestamp))
}
// If the span was verbose then the Structured events would have been
// stringified and included in the Logs above. If the span was not verbose
// we should add the Structured events now.
if !isVerbose(sp) {
sp.Structured(func(sr *types.Any, t time.Time) {
str, err := MessageToJSONString(sr, true /* emitDefaults */)
if err != nil {
return
}
lastLog := ownLogs[len(ownLogs)-1]
ownLogs = append(ownLogs, conv("structured:"+str, t, lastLog.Timestamp))
})
}
childSpans := make([][]traceLogData, 0)
for _, osp := range r {
if osp.ParentSpanID != sp.SpanID {
continue
}
childSpans = append(childSpans, r.visitSpan(osp, depth+1))
}
// Merge ownLogs with childSpans.
mergedLogs := make([]traceLogData, 0, len(ownLogs))
timeMax := time.Date(2200, 0, 0, 0, 0, 0, 0, time.UTC)
i, j := 0, 0
var lastTimestamp time.Time
for i < len(ownLogs) || j < len(childSpans) {
if len(mergedLogs) > 0 {
lastTimestamp = mergedLogs[len(mergedLogs)-1].Timestamp
}
nextLog, nextChild := timeMax, timeMax
if i < len(ownLogs) {
nextLog = ownLogs[i].Timestamp
}
if j < len(childSpans) {
nextChild = childSpans[j][0].Timestamp
}
if nextLog.After(nextChild) {
// Fill in timeSincePrev for the first one of the child's entries.
if lastTimestamp != (time.Time{}) {
childSpans[j][0].timeSincePrev = childSpans[j][0].Timestamp.Sub(lastTimestamp)
}
mergedLogs = append(mergedLogs, childSpans[j]...)
lastTimestamp = childSpans[j][0].Timestamp
j++
} else {
mergedLogs = append(mergedLogs, ownLogs[i])
lastTimestamp = ownLogs[i].Timestamp
i++
}
}
return mergedLogs
}
// ToJaegerJSON returns the trace as a JSON that can be imported into Jaeger for
// visualization.
//
// The format is described here: https://github.com/jaegertracing/jaeger-ui/issues/381#issuecomment-494150826
//
// The statement is passed in so it can be included in the trace.
func (r Recording) ToJaegerJSON(stmt, comment, nodeStr string) (string, error) {
if len(r) == 0 {
return "", nil
}
cpy := make(Recording, len(r))
copy(cpy, r)
r = cpy
tagsCopy := make(map[string]string)
for k, v := range r[0].Tags {
tagsCopy[k] = v
}
tagsCopy["statement"] = stmt
r[0].Tags = tagsCopy
toJaegerSpanID := func(spanID uint64) jaegerjson.SpanID {
return jaegerjson.SpanID(strconv.FormatUint(spanID, 10))
}
// Each Span in Jaeger belongs to a "process" that generated it. Spans
// belonging to different colors are colored differently in Jaeger. We're
// going to map our different nodes to different processes.
processes := make(map[jaegerjson.ProcessID]jaegerjson.Process)
// getProcessID figures out what "process" a Span belongs to. It looks for an
// "node: <node id>" tag. The processes map is populated with an entry for every
// node present in the trace.
getProcessID := func(sp tracingpb.RecordedSpan) jaegerjson.ProcessID {
node := "unknown node"
for k, v := range sp.Tags {
if k == "node" {
node = fmt.Sprintf("node %s", v)
break
}
}
// If we have passed in an explicit nodeStr then use that as a processID.
if nodeStr != "" {
node = nodeStr
}
pid := jaegerjson.ProcessID(node)
if _, ok := processes[pid]; !ok {
processes[pid] = jaegerjson.Process{
ServiceName: node,
Tags: nil,
}
}
return pid
}
var t jaegerjson.Trace
t.TraceID = jaegerjson.TraceID(strconv.FormatUint(r[0].TraceID, 10))
t.Processes = processes
for _, sp := range r {
var s jaegerjson.Span
s.TraceID = t.TraceID
s.Duration = uint64(sp.Duration.Microseconds())
s.StartTime = uint64(sp.StartTime.UnixNano() / 1000)
s.SpanID = toJaegerSpanID(sp.SpanID)
s.OperationName = sp.Operation
s.ProcessID = getProcessID(sp)
if sp.ParentSpanID != 0 {
s.References = []jaegerjson.Reference{{
RefType: jaegerjson.ChildOf,
TraceID: s.TraceID,
SpanID: toJaegerSpanID(sp.ParentSpanID),
}}
}
for k, v := range sp.Tags {
s.Tags = append(s.Tags, jaegerjson.KeyValue{
Key: k,
Value: v,
Type: "STRING",
})
}
for _, l := range sp.Logs {
jl := jaegerjson.Log{
Timestamp: uint64(l.Time.UnixNano() / 1000),
Fields: []jaegerjson.KeyValue{{
Value: l.Msg(),
Type: "STRING",
}},
}
s.Logs = append(s.Logs, jl)
}
// If the span was verbose then the Structured events would have been
// stringified and included in the Logs above. If the span was not verbose
// we should add the Structured events now.
if !isVerbose(sp) {
sp.Structured(func(sr *types.Any, t time.Time) {
jl := jaegerjson.Log{Timestamp: uint64(t.UnixNano() / 1000)}
jsonStr, err := MessageToJSONString(sr, true /* emitDefaults */)
if err != nil {
return
}
jl.Fields = append(jl.Fields, jaegerjson.KeyValue{
Key: "structured",
Value: jsonStr,
Type: "STRING",
})
s.Logs = append(s.Logs, jl)
})
}
t.Spans = append(t.Spans, s)
}
data := TraceCollection{
Data: []jaegerjson.Trace{t},
// Add a comment that will show-up at the top of the JSON file, is someone opens the file.
// NOTE: This comment is scarce on newlines because they appear as \n in the
// generated file doing more harm than good.
Comment: comment,
}
json, err := json.MarshalIndent(data, "" /* prefix */, "\t" /* indent */)
if err != nil {
return "", err
}
return string(json), nil
}
// TraceCollection is the format accepted by the Jaegar upload feature, as per
// https://github.com/jaegertracing/jaeger-ui/issues/381#issuecomment-494150826
type TraceCollection struct {
// Comment is a dummy field we use to put instructions on how to load the trace.
Comment string `json:"_comment"`
Data []jaegerjson.Trace `json:"data"`
}
// isVerbose returns true if the RecordedSpan was started is a verbose mode.
func isVerbose(s tracingpb.RecordedSpan) bool {
if s.Baggage == nil {
return false
}
_, isVerbose := s.Baggage[verboseTracingBaggageKey]
return isVerbose
}