-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathrecorded_span.go
138 lines (117 loc) · 3.74 KB
/
recorded_span.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
// Copyright 2019 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 tracingpb
import (
"fmt"
"strings"
"time"
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
"github.com/cockroachdb/redact"
types "github.com/gogo/protobuf/types"
)
// TraceID is a probabilistically-unique id, shared by all spans in a trace.
type TraceID uint64
// SpanID is a probabilistically-unique span id.
type SpanID uint64
// Recording represents a group of RecordedSpans rooted at a fixed root span, as
// returned by GetRecording. Spans are sorted by StartTime.
type Recording []RecordedSpan
// 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)
}
// LogMessageField is the field name used for the log message in a LogRecord.
const LogMessageField = "event"
func (s *RecordedSpan) String() string {
sb := strings.Builder{}
sb.WriteString(fmt.Sprintf("=== %s (id: %d parent: %d)\n", s.Operation, s.SpanID, s.ParentSpanID))
for _, ev := range s.Logs {
sb.WriteString(fmt.Sprintf("%s %s\n", ev.Time.UTC().Format(time.RFC3339Nano), ev.Msg()))
}
return sb.String()
}
// Structured visits the data passed to RecordStructured for the Span from which
// the RecordedSpan was created.
func (s *RecordedSpan) Structured(visit func(*types.Any, time.Time)) {
for _, sr := range s.StructuredRecords {
visit(sr.Payload, sr.Time)
}
}
// FindTagGroup returns the tag group matching the supplied name, or nil if
// the name is not found.
func (s *RecordedSpan) FindTagGroup(name string) *TagGroup {
for i := range s.TagGroups {
tagGroup := &s.TagGroups[i]
if tagGroup.Name == name {
return tagGroup
}
}
return nil
}
// Msg extracts the message of the LogRecord, which is either in an "event" or
// "error" field.
func (l LogRecord) Msg() redact.RedactableString {
if l.Message != "" {
return l.Message
}
return ""
}
// MemorySize implements the sizable interface.
func (l *LogRecord) MemorySize() int {
return 3*8 + // 3 words for time.Time
2*8 + // 2 words for StringHeader
len(l.Message)
}
// MemorySize implements the sizable interface.
func (r *StructuredRecord) MemorySize() int {
return 3*8 + // 3 words for time.Time
1*8 + // 1 words for *Any
r.Payload.Size() // TODO(andrei): this is the encoded size, not the mem size
}
// FindTag returns the value matching the supplied key, or nil if the key is
// not found.
func (tg *TagGroup) FindTag(key string) (string, bool) {
for _, tag := range tg.Tags {
if tag.Key == key {
return tag.Value, true
}
}
return "", false
}
// Combine returns the sum of m and other.
func (m OperationMetadata) Combine(other OperationMetadata) OperationMetadata {
m.Count += other.Count
m.ContainsUnfinished = m.ContainsUnfinished || other.ContainsUnfinished
m.Duration += other.Duration
return m
}
var _ redact.SafeFormatter = OperationMetadata{}
func (m OperationMetadata) String() string {
return redact.StringWithoutMarkers(m)
}
// SafeFormat implements redact.SafeFormatter.
func (m OperationMetadata) SafeFormat(s redact.SafePrinter, _ rune) {
s.SafeRune('{')
metadata := fmt.Sprintf("count: %d, duration %s", m.Count,
humanizeutil.Duration(m.Duration))
if m.ContainsUnfinished {
metadata += ", unfinished"
}
s.SafeString(redact.SafeString(metadata))
s.SafeRune('}')
}