forked from jaegertracing/jaeger-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
span.go
329 lines (285 loc) · 8.29 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
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
// Copyright (c) 2016 Uber Technologies, Inc.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package jaeger
import (
"strings"
"sync"
"time"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/log"
"github.com/uber/jaeger-client-go/utils"
)
type span struct {
sync.RWMutex
tracer *tracer
context SpanContext
// The name of the "operation" this span is an instance of.
// Known as a "span name" in some implementations.
operationName string
// firstInProcess, if true, indicates that this span is the root of the (sub)tree
// of spans in the current process. In other words it's true for the root spans,
// and the ingress spans when the process joins another trace.
firstInProcess bool
// used to distinguish local vs. RPC Server vs. RPC Client spans
spanKind string
// startTime is the timestamp indicating when the span began, with microseconds precision.
startTime time.Time
// duration returns duration of the span with microseconds precision.
// Zero value means duration is unknown.
duration time.Duration
// peer points to the peer service participating in this span,
// e.g. the Client if this span is a server span,
// or Server if this span is a client span
peer struct {
Ipv4 int32
Port int16
ServiceName string
}
// tags attached to this span
tags []Tag
// The span's "micro-log"
logs []opentracing.LogRecord
}
// Tag a simple key value wrapper
type Tag struct {
key string
value interface{}
}
// Sets or changes the operation name.
func (s *span) SetOperationName(operationName string) opentracing.Span {
s.Lock()
defer s.Unlock()
if s.context.IsSampled() {
s.operationName = operationName
}
return s
}
// SetTag implements SetTag() of opentracing.Span
func (s *span) SetTag(key string, value interface{}) opentracing.Span {
if key == string(ext.SamplingPriority) && setSamplingPriority(s, key, value) {
return s
}
s.Lock()
defer s.Unlock()
if s.context.IsSampled() {
s.setTagNoLocking(key, value)
}
return s
}
func (s *span) setTagNoLocking(key string, value interface{}) {
handled := false
if handler, ok := specialTagHandlers[key]; ok {
handled = handler(s, key, value)
}
if !handled {
s.tags = append(s.tags, Tag{key: key, value: value})
}
}
func (s *span) setTracerTags(tags []Tag) {
s.Lock()
for _, tag := range tags {
s.tags = append(s.tags, tag)
}
s.Unlock()
}
func (s *span) LogFields(fields ...log.Field) {
s.Lock()
defer s.Unlock()
if !s.context.IsSampled() {
return
}
lr := opentracing.LogRecord{
Fields: fields,
Timestamp: time.Now(),
}
s.appendLog(lr)
}
func (s *span) LogKV(alternatingKeyValues ...interface{}) {
s.RLock()
sampled := s.context.IsSampled()
s.RUnlock()
if !sampled {
return
}
fields, err := log.InterleavedKVToFields(alternatingKeyValues...)
if err != nil {
s.LogFields(log.Error(err), log.String("function", "LogKV"))
return
}
s.LogFields(fields...)
}
func (s *span) LogEvent(event string) {
s.Log(opentracing.LogData{Event: event})
}
func (s *span) LogEventWithPayload(event string, payload interface{}) {
s.Log(opentracing.LogData{Event: event, Payload: payload})
}
func (s *span) Log(ld opentracing.LogData) {
s.Lock()
defer s.Unlock()
if s.context.IsSampled() {
if ld.Timestamp.IsZero() {
ld.Timestamp = s.tracer.timeNow()
}
s.appendLog(ld.ToLogRecord())
}
}
// this function should only be called while holding a Write lock
func (s *span) appendLog(lr opentracing.LogRecord) {
// TODO add logic to limit number of logs per span (issue #46)
s.logs = append(s.logs, lr)
}
// SetBaggageItem implements SetBaggageItem() of opentracing.SpanContext
func (s *span) SetBaggageItem(key, value string) opentracing.Span {
key = normalizeBaggageKey(key)
s.Lock()
defer s.Unlock()
s.context = s.context.WithBaggageItem(key, value)
return s
}
// BaggageItem implements BaggageItem() of opentracing.SpanContext
func (s *span) BaggageItem(key string) string {
key = normalizeBaggageKey(key)
s.RLock()
defer s.RUnlock()
return s.context.baggage[key]
}
func (s *span) Finish() {
s.FinishWithOptions(opentracing.FinishOptions{})
}
func (s *span) FinishWithOptions(options opentracing.FinishOptions) {
s.Lock()
if s.context.IsSampled() {
finishTime := options.FinishTime
if finishTime.IsZero() {
finishTime = s.tracer.timeNow()
}
s.duration = finishTime.Sub(s.startTime)
// Note: bulk logs are not subject to maxLogsPerSpan limit
if options.LogRecords != nil {
s.logs = append(s.logs, options.LogRecords...)
}
for _, ld := range options.BulkLogData {
s.logs = append(s.logs, ld.ToLogRecord())
}
}
s.Unlock()
// call reportSpan even for non-sampled traces, to return span to the pool
s.tracer.reportSpan(s)
}
func (s *span) Context() opentracing.SpanContext {
return s.context
}
func (s *span) Tracer() opentracing.Tracer {
return s.tracer
}
func (s *span) String() string {
return s.context.String()
}
func (s *span) peerDefined() bool {
return s.peer.ServiceName != "" || s.peer.Ipv4 != 0 || s.peer.Port != 0
}
func (s *span) isRPC() bool {
s.RLock()
defer s.RUnlock()
return s.spanKind == string(ext.SpanKindRPCClientEnum) || s.spanKind == string(ext.SpanKindRPCServerEnum)
}
func (s *span) isRPCClient() bool {
s.RLock()
defer s.RUnlock()
return s.spanKind == string(ext.SpanKindRPCClientEnum)
}
var specialTagHandlers = map[string]func(*span, string, interface{}) bool{
string(ext.SpanKind): setSpanKind,
string(ext.PeerHostIPv4): setPeerIPv4,
string(ext.PeerPort): setPeerPort,
string(ext.PeerService): setPeerService,
}
func setSpanKind(s *span, key string, value interface{}) bool {
if val, ok := value.(string); ok {
s.spanKind = val
return true
}
if val, ok := value.(ext.SpanKindEnum); ok {
s.spanKind = string(val)
return true
}
return false
}
func setPeerIPv4(s *span, key string, value interface{}) bool {
if val, ok := value.(string); ok {
if ip, err := utils.ParseIPToUint32(val); err == nil {
s.peer.Ipv4 = int32(ip)
return true
}
}
if val, ok := value.(uint32); ok {
s.peer.Ipv4 = int32(val)
return true
}
if val, ok := value.(int32); ok {
s.peer.Ipv4 = val
return true
}
return false
}
func setPeerPort(s *span, key string, value interface{}) bool {
if val, ok := value.(string); ok {
if port, err := utils.ParsePort(val); err == nil {
s.peer.Port = int16(port)
return true
}
}
if val, ok := value.(uint16); ok {
s.peer.Port = int16(val)
return true
}
if val, ok := value.(int); ok {
s.peer.Port = int16(val)
return true
}
return false
}
func setPeerService(s *span, key string, value interface{}) bool {
if val, ok := value.(string); ok {
s.peer.ServiceName = val
return true
}
return false
}
func setSamplingPriority(s *span, key string, value interface{}) bool {
s.Lock()
defer s.Unlock()
if val, ok := value.(uint16); ok {
if val > 0 {
s.context.flags = s.context.flags | flagDebug | flagSampled
} else {
s.context.flags = s.context.flags & (^flagSampled)
}
return true
}
return false
}
// Converts end-user baggage key into internal representation.
// Used for both read and write access to baggage items.
func normalizeBaggageKey(key string) string {
// TODO(yurishkuro) normalizeBaggageKey: cache the results in some bounded LRU cache
return strings.Replace(strings.ToLower(key), "_", "-", -1)
}