forked from cockroachdb/cockroach
/
crdbspan.go
559 lines (483 loc) · 15 KB
/
crdbspan.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
// Copyright 2021 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 (
"fmt"
"sort"
"sync"
"sync/atomic"
"time"
"github.com/cockroachdb/cockroach/pkg/util/ring"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/tracing/tracingpb"
"github.com/cockroachdb/logtags"
"github.com/gogo/protobuf/types"
"github.com/opentracing/opentracing-go"
)
// crdbSpan is a span for internal crdb usage. This is used to power SQL session
// tracing.
type crdbSpan struct {
rootSpan *crdbSpan // root span of the containing trace; could be itself
// traceEmpty indicates whether or not the trace rooted at this span
// (provided it is a root span) contains any recordings or baggage. All
// spans hold a reference to the rootSpan; this field is accessed
// through that reference.
traceEmpty atomic.Value // contains a bool
traceID uint64 // probabilistically unique
spanID uint64 // probabilistically unique
parentSpanID uint64
goroutineID uint64
operation string
startTime time.Time
// logTags are set to the log tags that were available when this Span was
// created, so that there's no need to eagerly copy all of those log tags
// into this Span's tags. If the Span's tags are actually requested, these
// logTags will be copied out at that point.
//
// Note that these tags have not gone through the log tag -> Span tag
// remapping procedure; tagName() needs to be called before exposing each
// tag's key to a user.
logTags *logtags.Buffer
mu crdbSpanMu
testing *testingKnob
}
type testingKnob struct {
clock timeutil.TimeSource
}
type crdbSpanMu struct {
syncutil.Mutex
// duration is initialized to -1 and set on Finish().
duration time.Duration
recording struct {
// recordingType is the recording type of the ongoing recording, if any.
// Its 'load' method may be called without holding the surrounding mutex,
// but its 'swap' method requires the mutex.
recordingType atomicRecordingType
logs sizeLimitedBuffer // of *tracingpb.LogRecords
structured sizeLimitedBuffer // of Structured events
// dropped is true if the span has capped out it's memory limits for
// logs and structured events, and has had to drop some. It's used to
// annotate recordings with the _dropped tag, when applicable.
dropped bool
// children contains the list of child spans started after this Span
// started recording.
children childSpanRefs
// remoteSpan contains the list of remote child span recordings that
// were manually imported.
remoteSpans []tracingpb.RecordedSpan
}
// tags are only captured when recording. These are tags that have been
// added to this Span, and will be appended to the tags in logTags when
// someone needs to actually observe the total set of tags that is a part of
// this Span.
// TODO(radu): perhaps we want a recording to capture all the tags (even
// those that were set before recording started)?
tags opentracing.Tags
// The Span's associated baggage.
baggage map[string]string
}
type childSpanRefs struct {
preAllocated [4]*crdbSpan
overflow []*crdbSpan
}
func (c *childSpanRefs) len() int {
l := 0
for i := 0; i < len(c.preAllocated); i++ {
if c.preAllocated[i] == nil {
return l
}
l++
}
return len(c.overflow) + l
}
func (c *childSpanRefs) add(ref *crdbSpan) {
for i := 0; i < len(c.preAllocated); i++ {
if c.preAllocated[i] == nil {
c.preAllocated[i] = ref
return
}
}
// Only record the child if the parent still has room.
if len(c.overflow) < maxChildrenPerSpan-len(c.preAllocated) {
c.overflow = append(c.overflow, ref)
}
}
func (c *childSpanRefs) list() []*crdbSpan {
var refs []*crdbSpan
for i := 0; i < len(c.preAllocated); i++ {
if c.preAllocated[i] == nil {
return refs
}
refs = append(refs, c.preAllocated[i])
}
if c.overflow != nil {
refs = append(refs, c.overflow...)
}
return refs
}
func (c *childSpanRefs) get(idx int) *crdbSpan {
if idx < len(c.preAllocated) {
ref := c.preAllocated[idx]
if ref == nil {
panic(fmt.Sprintf("idx %d out of bounds", idx))
}
return ref
}
return c.overflow[idx-len(c.preAllocated)]
}
func (c *childSpanRefs) reset() {
for i := 0; i < len(c.preAllocated); i++ {
c.preAllocated[i] = nil
}
c.overflow = nil
}
func newSizeLimitedBuffer(limit int64) sizeLimitedBuffer {
return sizeLimitedBuffer{
limit: limit,
}
}
type sizeLimitedBuffer struct {
ring.Buffer
size int64 // in bytes
limit int64 // in bytes
}
func (b *sizeLimitedBuffer) Reset() {
b.Buffer.Reset()
b.size = 0
}
func (s *crdbSpan) recordingType() RecordingType {
if s == nil {
return RecordingOff
}
return s.mu.recording.recordingType.load()
}
// enableRecording start recording on the Span. From now on, log events and
// child spans will be stored.
//
// If parent != nil, the Span will be registered as a child of the respective
// parent. If nil, the parent's recording will not include this child.
func (s *crdbSpan) enableRecording(parent *crdbSpan, recType RecordingType) {
if parent != nil {
parent.addChild(s)
}
if recType == RecordingOff || s.recordingType() == recType {
return
}
s.mu.Lock()
defer s.mu.Unlock()
s.mu.recording.recordingType.swap(recType)
if recType == RecordingVerbose {
s.setBaggageItemLocked(verboseTracingBaggageKey, "1")
}
}
// resetRecording clears any previously recorded info.
//
// NB: This is needed by SQL SessionTracing, who likes to start and stop
// recording repeatedly on the same Span, and collect the (separate) recordings
// every time.
func (s *crdbSpan) resetRecording() {
s.mu.Lock()
defer s.mu.Unlock()
s.mu.recording.logs.Reset()
s.mu.recording.structured.Reset()
s.mu.recording.dropped = false
s.mu.recording.children.reset()
s.mu.recording.remoteSpans = nil
}
func (s *crdbSpan) disableRecording() {
if s.recordingType() == RecordingOff {
return
}
s.mu.Lock()
defer s.mu.Unlock()
oldRecType := s.mu.recording.recordingType.swap(RecordingOff)
// We test the duration as a way to check if the Span has been finished. If it
// has, we don't want to do the call below as it might crash (at least if
// there's a netTr).
if (s.mu.duration == -1) && (oldRecType == RecordingVerbose) {
// Clear the verboseTracingBaggageKey baggage item, assuming that it was set by
// enableRecording().
s.setBaggageItemLocked(verboseTracingBaggageKey, "")
}
}
func (s *crdbSpan) getRecording(everyoneIsV211 bool, wantTags bool) Recording {
if s == nil {
return nil // noop span
}
if !everyoneIsV211 {
// The cluster may contain nodes that are running v20.2. Unfortunately that
// version can easily crash when a peer returns a recording that that node
// did not expect would get created. To circumvent this, retain the v20.2
// behavior of eliding recordings when verbosity is off until we're sure
// that v20.2 is not around any longer.
//
// TODO(tbg): remove this in the v21.2 cycle.
if s.recordingType() == RecordingOff {
return nil
}
}
// Return early (without allocating) if the trace is empty, i.e. there are
// no recordings or baggage. If the trace is verbose, we'll still recurse in
// order to pick up all the operations that were part of the trace, despite
// nothing having any actual data in them.
if s.recordingType() != RecordingVerbose && s.inAnEmptyTrace() {
return nil
}
s.mu.Lock()
// The capacity here is approximate since we don't know how many
// grandchildren there are.
result := make(Recording, 0, 1+s.mu.recording.children.len()+len(s.mu.recording.remoteSpans))
// Shallow-copy the children so we can process them without the lock.
children := s.mu.recording.children.list()
result = append(result, s.getRecordingLocked(wantTags))
result = append(result, s.mu.recording.remoteSpans...)
s.mu.Unlock()
for _, child := range children {
result = append(result, child.getRecording(everyoneIsV211, wantTags)...)
}
// Sort the spans by StartTime, except the first Span (the root of this
// recording) which stays in place.
toSort := sortPool.Get().(*Recording) // avoids allocations in sort.Sort
*toSort = result[1:]
sort.Sort(toSort)
*toSort = nil
sortPool.Put(toSort)
return result
}
func (s *crdbSpan) importRemoteSpans(remoteSpans []tracingpb.RecordedSpan) {
if len(remoteSpans) == 0 {
return
}
s.markTraceAsNonEmpty()
// Change the root of the remote recording to be a child of this Span. This is
// usually already the case, except with DistSQL traces where remote
// processors run in spans that FollowFrom an RPC Span that we don't collect.
remoteSpans[0].ParentSpanID = s.spanID
s.mu.Lock()
defer s.mu.Unlock()
s.mu.recording.remoteSpans = append(s.mu.recording.remoteSpans, remoteSpans...)
}
func (s *crdbSpan) setTagLocked(key string, value interface{}) {
if s.recordingType() != RecordingVerbose {
// Don't bother storing tags if we're unlikely to retrieve them.
return
}
if s.mu.tags == nil {
s.mu.tags = make(opentracing.Tags)
}
s.mu.tags[key] = value
}
func (s *crdbSpan) record(msg string) {
if s.recordingType() != RecordingVerbose {
return
}
var now time.Time
if s.testing != nil {
now = s.testing.clock.Now()
} else {
now = time.Now()
}
logRecord := &tracingpb.LogRecord{
Time: now,
Fields: []tracingpb.LogRecord_Field{
{Key: tracingpb.LogMessageField, Value: msg},
},
}
s.recordInternal(logRecord, &s.mu.recording.logs)
}
func (s *crdbSpan) recordStructured(item Structured) {
s.recordInternal(item, &s.mu.recording.structured)
}
// sizable is a subset for protoutil.Message, for payloads (log records and
// structured events) that can be recorded.
type sizable interface {
Size() int
}
// inAnEmptyTrace indicates whether or not the containing trace is "empty" (i.e.
// has any recordings or baggage).
func (s *crdbSpan) inAnEmptyTrace() bool {
return s.rootSpan.traceEmpty.Load().(bool)
}
func (s *crdbSpan) markTraceAsNonEmpty() {
s.rootSpan.traceEmpty.Store(false)
}
func (s *crdbSpan) recordInternal(payload sizable, buffer *sizeLimitedBuffer) {
s.markTraceAsNonEmpty()
s.mu.Lock()
defer s.mu.Unlock()
size := int64(payload.Size())
if size > buffer.limit {
// The incoming payload alone blows past the memory limit. Let's just
// drop it.
s.mu.recording.dropped = true
return
}
buffer.size += size
if buffer.size > buffer.limit {
s.mu.recording.dropped = true
}
for buffer.size > buffer.limit {
first := buffer.GetFirst().(sizable)
buffer.RemoveFirst()
buffer.size -= int64(first.Size())
}
buffer.AddLast(payload)
}
func (s *crdbSpan) setBaggageItemAndTag(restrictedKey, value string) {
s.markTraceAsNonEmpty()
s.mu.Lock()
defer s.mu.Unlock()
s.setBaggageItemLocked(restrictedKey, value)
// Don't set the tag if this is the special cased baggage item indicating
// span verbosity, as it is named nondescriptly and the recording knows
// how to display its verbosity independently.
if restrictedKey != verboseTracingBaggageKey {
s.setTagLocked(restrictedKey, value)
}
}
func (s *crdbSpan) setBaggageItemLocked(restrictedKey, value string) {
if oldVal, ok := s.mu.baggage[restrictedKey]; ok && oldVal == value {
// No-op.
return
}
if s.mu.baggage == nil {
s.mu.baggage = make(map[string]string)
}
s.mu.baggage[restrictedKey] = value
}
// getRecordingLocked returns the Span's recording. This does not include
// children.
//
// When wantTags is false, no tags will be added. This is a performance
// optimization as stringifying the tag values can be expensive.
func (s *crdbSpan) getRecordingLocked(wantTags bool) tracingpb.RecordedSpan {
rs := tracingpb.RecordedSpan{
TraceID: s.traceID,
SpanID: s.spanID,
ParentSpanID: s.parentSpanID,
GoroutineID: s.goroutineID,
Operation: s.operation,
StartTime: s.startTime,
Duration: s.mu.duration,
}
if rs.Duration == -1 {
// -1 indicates an unfinished Span. For a recording it's better to put some
// duration in it, otherwise tools get confused. For example, we export
// recordings to Jaeger, and spans with a zero duration don't look nice.
rs.Duration = timeutil.Now().Sub(rs.StartTime)
rs.Finished = false
} else {
rs.Finished = true
}
addTag := func(k, v string) {
if rs.Tags == nil {
rs.Tags = make(map[string]string)
}
rs.Tags[k] = v
}
if wantTags {
if s.mu.duration == -1 {
addTag("_unfinished", "1")
}
if s.mu.recording.recordingType.load() == RecordingVerbose {
addTag("_verbose", "1")
}
if s.mu.recording.dropped {
addTag("_dropped", "1")
}
}
if numEvents := s.mu.recording.structured.Len(); numEvents != 0 {
rs.InternalStructured = make([]*types.Any, 0, numEvents)
for i := 0; i < numEvents; i++ {
event := s.mu.recording.structured.Get(i).(Structured)
item, err := types.MarshalAny(event)
if err != nil {
// An error here is an error from Marshal; these
// are unlikely to happen.
continue
}
rs.InternalStructured = append(rs.InternalStructured, item)
}
}
if len(s.mu.baggage) > 0 {
rs.Baggage = make(map[string]string)
for k, v := range s.mu.baggage {
rs.Baggage[k] = v
}
}
if wantTags {
if s.logTags != nil {
setLogTags(s.logTags.Get(), func(remappedKey string, tag *logtags.Tag) {
addTag(remappedKey, tag.ValueStr())
})
}
if len(s.mu.tags) > 0 {
for k, v := range s.mu.tags {
// We encode the tag values as strings.
addTag(k, fmt.Sprint(v))
}
}
}
if numLogs := s.mu.recording.logs.Len(); numLogs != 0 {
rs.Logs = make([]tracingpb.LogRecord, numLogs)
for i := 0; i < numLogs; i++ {
lr := s.mu.recording.logs.Get(i).(*tracingpb.LogRecord)
rs.Logs[i] = *lr
}
}
return rs
}
func (s *crdbSpan) addChild(child *crdbSpan) {
s.mu.Lock()
defer s.mu.Unlock()
s.mu.recording.children.add(child)
}
// setVerboseRecursively sets the verbosity of the crdbSpan appropriately and
// recurses on its list of children.
func (s *crdbSpan) setVerboseRecursively(to bool) {
if to {
s.enableRecording(nil /* parent */, RecordingVerbose)
} else {
s.disableRecording()
}
s.mu.Lock()
children := s.mu.recording.children.list()
s.mu.Unlock()
for _, child := range children {
child.setVerboseRecursively(to)
}
}
var sortPool = sync.Pool{
New: func() interface{} {
return &Recording{}
},
}
// Less implements sort.Interface.
func (r Recording) Less(i, j int) bool {
return r[i].StartTime.Before(r[j].StartTime)
}
// Swap implements sort.Interface.
func (r Recording) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
// Len implements sort.Interface.
func (r Recording) Len() int {
return len(r)
}
type atomicRecordingType RecordingType
// load returns the recording type.
func (art *atomicRecordingType) load() RecordingType {
return RecordingType(atomic.LoadInt32((*int32)(art)))
}
// swap stores the new recording type and returns the old one.
func (art *atomicRecordingType) swap(recType RecordingType) RecordingType {
return RecordingType(atomic.SwapInt32((*int32)(art), int32(recType)))
}