forked from instana/go-sensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
span.go
277 lines (217 loc) · 5.35 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
// (c) Copyright IBM Corp. 2021
// (c) Copyright Instana Inc. 2016
package instana
import (
"bytes"
"strings"
"sync"
"time"
"github.com/instana/go-sensor/logger"
ot "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
otlog "github.com/opentracing/opentracing-go/log"
)
const minSpanLogLevel = logger.WarnLevel
var _ ot.Span = (*spanS)(nil)
type spanS struct {
Service string
Operation string
Start time.Time
Duration time.Duration
Correlation EUMCorrelationData
Tags ot.Tags
Logs []ot.LogRecord
ErrorCount int
tracer *tracerS
mu sync.Mutex
context SpanContext
}
func (r *spanS) BaggageItem(key string) string {
r.mu.Lock()
defer r.mu.Unlock()
return r.context.Baggage[key]
}
func (r *spanS) SetBaggageItem(key, val string) ot.Span {
r.mu.Lock()
defer r.mu.Unlock()
r.context = r.context.WithBaggageItem(key, val)
return r
}
func (r *spanS) Context() ot.SpanContext {
return r.context
}
func (r *spanS) Finish() {
r.FinishWithOptions(ot.FinishOptions{})
}
func (r *spanS) FinishWithOptions(opts ot.FinishOptions) {
finishTime := opts.FinishTime
if finishTime.IsZero() {
finishTime = time.Now()
}
duration := finishTime.Sub(r.Start)
r.mu.Lock()
defer r.mu.Unlock()
for _, lr := range opts.LogRecords {
r.appendLog(lr)
}
for _, ld := range opts.BulkLogData {
r.appendLog(ld.ToLogRecord())
}
r.Duration = duration
if r.sendSpanToAgent() {
if sensor.Agent().Ready() {
r.tracer.recorder.RecordSpan(r)
} else {
delayed.append(r)
}
r.sendOpenTracingLogRecords()
}
}
func (r *spanS) sendSpanToAgent() bool {
//if suppress tag is present, span shouldn't be forwarded
if r.context.Suppressed {
return false
}
if !isRootExitSpan(r.Tags[string(ext.SpanKind)], r.context.ParentID == 0) {
// if the span is an entry span, intermediate span, exit span with a parent
// it should be forwarded to the agent
return true
}
// if the span is an exit span without a parent span, then it should be forwarded
// only if ALLOW_ROOT_EXIT_SPAN is configured by the user
return allowRootExitSpan()
}
func (r *spanS) appendLog(lr ot.LogRecord) {
maxLogs := r.tracer.Options().MaxLogsPerSpan
if maxLogs == 0 || len(r.Logs) < maxLogs {
r.Logs = append(r.Logs, lr)
}
}
func (r *spanS) Log(ld ot.LogData) {
if r.tracer.Options().DropAllLogs {
return
}
r.mu.Lock()
defer r.mu.Unlock()
if ld.Timestamp.IsZero() {
ld.Timestamp = time.Now()
}
r.appendLog(ld.ToLogRecord())
}
func (r *spanS) LogEvent(event string) {
r.Log(ot.LogData{
Event: event})
}
func (r *spanS) LogEventWithPayload(event string, payload interface{}) {
r.Log(ot.LogData{
Event: event,
Payload: payload})
}
func (r *spanS) LogFields(fields ...otlog.Field) {
for _, v := range fields {
// If this tag indicates an error, increase the error count
if openTracingLogFieldLevel(v) == logger.ErrorLevel {
r.ErrorCount++
}
}
lr := ot.LogRecord{
Fields: fields,
}
if r.tracer.Options().DropAllLogs {
return
}
r.mu.Lock()
defer r.mu.Unlock()
if lr.Timestamp.IsZero() {
lr.Timestamp = time.Now()
}
r.appendLog(lr)
}
func (r *spanS) LogKV(keyValues ...interface{}) {
fields, err := otlog.InterleavedKVToFields(keyValues...)
if err != nil {
r.LogFields(otlog.Error(err), otlog.String("function", "LogKV"))
return
}
r.LogFields(fields...)
}
func (r *spanS) SetOperationName(operationName string) ot.Span {
r.mu.Lock()
defer r.mu.Unlock()
r.Operation = operationName
return r
}
func (r *spanS) SetTag(key string, value interface{}) ot.Span {
r.mu.Lock()
defer r.mu.Unlock()
if r.Tags == nil {
r.Tags = ot.Tags{}
}
// If this tag indicates an error, increase the error count
if key == "error" {
r.ErrorCount++
}
if key == suppressTracingTag {
r.context.Suppressed = true
return r
}
r.Tags[key] = value
return r
}
func (r *spanS) Tracer() ot.Tracer {
return r.tracer
}
// sendOpenTracingLogRecords converts OpenTracing log records that contain errors
// to Instana log spans and sends them to the agent
func (r *spanS) sendOpenTracingLogRecords() {
for _, lr := range r.Logs {
r.sendOpenTracingLogRecord(lr)
}
}
func (r *spanS) sendOpenTracingLogRecord(lr ot.LogRecord) {
lvl := openTracingHighestLogRecordLevel(lr)
if lvl.Less(minSpanLogLevel) {
return
}
buf := bytes.NewBuffer(nil)
enc := newOpenTracingLogEncoder(buf)
for _, lf := range lr.Fields {
lf.Marshal(enc)
buf.WriteByte(' ')
}
r.tracer.StartSpan(
"log.go",
ot.ChildOf(r.context),
ot.StartTime(lr.Timestamp),
ot.Tags{
"log.level": lvl.String(),
"log.message": strings.TrimSpace(buf.String()),
},
).FinishWithOptions(
ot.FinishOptions{
FinishTime: lr.Timestamp,
},
)
}
// openTracingHighestLogRecordLevel determines the level of this record by inspecting its fields.
// If there are multiple fields suggesting the log level, i.e. both "error" and "warn" are present,
// the highest one takes precedence.
func openTracingHighestLogRecordLevel(lr ot.LogRecord) logger.Level {
highestLvl := logger.DebugLevel
for _, lf := range lr.Fields {
if lvl := openTracingLogFieldLevel(lf); highestLvl.Less(lvl) {
highestLvl = lvl
}
}
return highestLvl
}
func openTracingLogFieldLevel(lf otlog.Field) logger.Level {
switch lf.Key() {
case "error", "error.object":
return logger.ErrorLevel
case "warn":
return logger.WarnLevel
default:
return logger.DebugLevel
}
}