forked from opentracing/basictracer-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
span.go
199 lines (165 loc) · 4.42 KB
/
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
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
package basictracer
import (
"fmt"
"sync"
"time"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
)
// Span provides access to the essential details of the span, for use
// by basictracer consumers. These methods may only be called prior
// to (*opentracing.Span).Finish().
type Span interface {
opentracing.Span
// Context contains trace identifiers
Context() Context
// Operation names the work done by this span instance
Operation() string
// Start indicates when the span began
Start() time.Time
}
// Implements the `Span` interface. Created via tracerImpl (see
// `basictracer.New()`).
type spanImpl struct {
tracer *tracerImpl
event func(SpanEvent)
sync.Mutex // protects the fields below
raw RawSpan
}
var spanPool = &sync.Pool{New: func() interface{} {
return &spanImpl{}
}}
func (s *spanImpl) reset() {
s.tracer, s.event = nil, nil
// Note: Would like to do the following, but then the consumer of RawSpan
// (the recorder) needs to make sure that they're not holding on to the
// baggage or logs when they return (i.e. they need to copy if they care):
//
// logs, baggage := s.raw.Logs[:0], s.raw.Baggage
// for k := range baggage {
// delete(baggage, k)
// }
// s.raw.Logs, s.raw.Baggage = logs, baggage
//
// That's likely too much to ask for. But there is some magic we should
// be able to do with `runtime.SetFinalizer` to reclaim that memory into
// a buffer pool when GC considers them unreachable, which should ease
// some of the load. Hard to say how quickly that would be in practice
// though.
s.raw = RawSpan{}
}
func (s *spanImpl) SetOperationName(operationName string) opentracing.Span {
s.Lock()
defer s.Unlock()
s.raw.Operation = operationName
return s
}
func (s *spanImpl) trim() bool {
return !s.raw.Sampled && s.tracer.options.TrimUnsampledSpans
}
func (s *spanImpl) SetTag(key string, value interface{}) opentracing.Span {
defer s.onTag(key, value)
s.Lock()
defer s.Unlock()
if key == string(ext.SamplingPriority) {
s.raw.Sampled = true
return s
}
if s.trim() {
return s
}
if s.raw.Tags == nil {
s.raw.Tags = opentracing.Tags{}
}
s.raw.Tags[key] = value
return s
}
func (s *spanImpl) LogEvent(event string) {
s.Log(opentracing.LogData{
Event: event,
})
}
func (s *spanImpl) LogEventWithPayload(event string, payload interface{}) {
s.Log(opentracing.LogData{
Event: event,
Payload: payload,
})
}
func (s *spanImpl) Log(ld opentracing.LogData) {
defer s.onLog(ld)
s.Lock()
defer s.Unlock()
if s.trim() {
return
}
if ld.Timestamp.IsZero() {
ld.Timestamp = time.Now()
}
s.raw.Logs = append(s.raw.Logs, ld)
}
func (s *spanImpl) Finish() {
s.FinishWithOptions(opentracing.FinishOptions{})
}
func (s *spanImpl) FinishWithOptions(opts opentracing.FinishOptions) {
finishTime := opts.FinishTime
if finishTime.IsZero() {
finishTime = time.Now()
}
duration := finishTime.Sub(s.raw.Start)
s.Lock()
defer s.Unlock()
if opts.BulkLogData != nil {
s.raw.Logs = append(s.raw.Logs, opts.BulkLogData...)
}
s.raw.Duration = duration
s.onFinish(s.raw)
s.tracer.options.Recorder.RecordSpan(s.raw)
// Last chance to get options before the span is possbily reset.
poolEnabled := s.tracer.options.EnableSpanPool
if s.tracer.options.DebugAssertUseAfterFinish {
// This makes it much more likely to catch a panic on any subsequent
// operation since s.tracer is accessed on every call to `Lock`.
s.reset()
}
if poolEnabled {
spanPool.Put(s)
}
}
func (s *spanImpl) SetBaggageItem(restrictedKey, val string) opentracing.Span {
canonicalKey, valid := opentracing.CanonicalizeBaggageKey(restrictedKey)
if !valid {
panic(fmt.Errorf("Invalid key: %q", restrictedKey))
}
s.Lock()
defer s.Unlock()
s.onBaggage(canonicalKey, val)
if s.trim() {
return s
}
if s.raw.Baggage == nil {
s.raw.Baggage = make(map[string]string)
}
s.raw.Baggage[canonicalKey] = val
return s
}
func (s *spanImpl) BaggageItem(restrictedKey string) string {
canonicalKey, valid := opentracing.CanonicalizeBaggageKey(restrictedKey)
if !valid {
panic(fmt.Errorf("Invalid key: %q", restrictedKey))
}
s.Lock()
defer s.Unlock()
return s.raw.Baggage[canonicalKey]
}
func (s *spanImpl) Tracer() opentracing.Tracer {
return s.tracer
}
func (s *spanImpl) Context() Context {
return s.raw.Context
}
func (s *spanImpl) Operation() string {
return s.raw.Operation
}
func (s *spanImpl) Start() time.Time {
return s.raw.Start
}