/
trace.go
176 lines (151 loc) · 3.81 KB
/
trace.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
package tracing
import (
"context"
"fmt"
"time"
"github.com/reddit/baseplate.go/randbp"
"github.com/reddit/baseplate.go/timebp"
)
// Well-known (special) span tag keys
const (
TagKeyClient = "client"
TagKeyEndpoint = "endpoint"
TagKeySuccess = "success"
TagKeyPeerService = "peer.service"
)
// FlagMask values.
//
// Reference: https://github.com/reddit/baseplate.py/blob/1ca8488bcd42c8786e6a3db35b2a99517fd07a99/baseplate/observers/tracing.py#L60-L64
const (
// When set, this trace passes all samplers.
FlagMaskDebug int64 = 1
)
const (
counterKeyPrefix = "counter."
baseplateComponent = "baseplate"
)
type trace struct {
tracer *Tracer
name string
traceID string
spanID string
parentID string
sampled bool
flags int64
timeAnnotationReceiveKey string
timeAnnotationSendKey string
start time.Time
stop time.Time
counters map[string]float64
tags map[string]string
}
func newTrace(tracer *Tracer, name string) *trace {
if tracer == nil {
tracer = &globalTracer
}
return &trace{
tracer: tracer,
name: name,
traceID: tracer.newTraceID(),
spanID: tracer.newSpanID(),
start: time.Now(),
counters: make(map[string]float64),
tags: map[string]string{
ZipkinBinaryAnnotationKeyComponent: baseplateComponent,
},
}
}
func (t *trace) addCounter(key string, delta float64) {
t.counters[key] += delta
}
func (t *trace) setTag(key string, value interface{}) {
t.tags[key] = fmt.Sprintf("%v", value)
}
func (t *trace) toZipkinSpan() ZipkinSpan {
zs := ZipkinSpan{
TraceID: t.traceID,
Name: t.name,
SpanID: t.spanID,
Start: timebp.TimestampMicrosecond(t.start),
ParentID: t.parentID,
}
end := t.stop
if end.IsZero() {
end = time.Now()
}
zs.Duration = timebp.DurationMicrosecond(end.Sub(t.start))
var endpoint ZipkinEndpointInfo
if t.tracer != nil {
endpoint = t.tracer.endpoint
}
if t.timeAnnotationReceiveKey != "" {
zs.TimeAnnotations = append(zs.TimeAnnotations, ZipkinTimeAnnotation{
Endpoint: endpoint,
Key: t.timeAnnotationReceiveKey,
Timestamp: timebp.TimestampMicrosecond(t.start),
})
}
if t.timeAnnotationSendKey != "" {
zs.TimeAnnotations = append(zs.TimeAnnotations, ZipkinTimeAnnotation{
Endpoint: endpoint,
Key: t.timeAnnotationSendKey,
Timestamp: timebp.TimestampMicrosecond(end),
})
}
zs.BinaryAnnotations = make([]ZipkinBinaryAnnotation, 0, len(t.counters)+len(t.tags))
for key, value := range t.counters {
zs.BinaryAnnotations = append(
zs.BinaryAnnotations,
ZipkinBinaryAnnotation{
Endpoint: endpoint,
Key: counterKeyPrefix + key,
Value: value,
},
)
}
for key, value := range t.tags {
zs.BinaryAnnotations = append(
zs.BinaryAnnotations,
ZipkinBinaryAnnotation{
Endpoint: endpoint,
Key: key,
Value: value,
},
)
}
return zs
}
func (t *trace) isDebugSet() bool {
return t.flags&FlagMaskDebug != 0
}
func (t *trace) setDebug(v bool) {
if v {
t.flags |= FlagMaskDebug
} else {
t.flags &= ^FlagMaskDebug
}
}
// shouldSample returns true if this span should be sampled.
//
// If the span's debug flag was set, then this function will always return true.
func (t *trace) shouldSample() bool {
return t.sampled || t.isDebugSet()
}
func (t *trace) publish(ctx context.Context) error {
if !t.shouldSample() || t.tracer == nil {
return nil
}
return t.tracer.Record(ctx, t.toZipkinSpan())
}
// In opentracing spec, zero trace/span/parent ids have special meanings.
// So we should use this function to generate non-zero random ids.
func nonZeroRandUint64() uint64 {
for {
// NOTE: theoretically this could be an infinite loop.
// In reality go's pseudo rand number generator won't keep giving us 0
// uint64 so we should be fine.
if id := randbp.R.Uint64(); id != 0 {
return id
}
}
}