This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathspan_test.go
224 lines (200 loc) · 6.92 KB
/
span_test.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
package basictracer
import (
"reflect"
"strconv"
"testing"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/log"
"github.com/stretchr/testify/assert"
)
func TestSpan_Baggage(t *testing.T) {
recorder := NewInMemoryRecorder()
tracer := NewWithOptions(Options{
Recorder: recorder,
ShouldSample: func(traceID uint64) bool { return true }, // always sample
})
span := tracer.StartSpan("x")
span.SetBaggageItem("x", "y")
assert.Equal(t, "y", span.BaggageItem("x"))
span.Finish()
spans := recorder.GetSpans()
assert.Equal(t, 1, len(spans))
assert.Equal(t, map[string]string{"x": "y"}, spans[0].Context.Baggage)
recorder.Reset()
span = tracer.StartSpan("x")
span.SetBaggageItem("x", "y")
baggage := make(map[string]string)
span.Context().ForeachBaggageItem(func(k, v string) bool {
baggage[k] = v
return true
})
assert.Equal(t, map[string]string{"x": "y"}, baggage)
span.SetBaggageItem("a", "b")
baggage = make(map[string]string)
span.Context().ForeachBaggageItem(func(k, v string) bool {
baggage[k] = v
return false // exit early
})
assert.Equal(t, 1, len(baggage))
span.Finish()
spans = recorder.GetSpans()
assert.Equal(t, 1, len(spans))
assert.Equal(t, 2, len(spans[0].Context.Baggage))
}
func TestSpan_Sampling(t *testing.T) {
recorder := NewInMemoryRecorder()
tracer := NewWithOptions(Options{
Recorder: recorder,
ShouldSample: func(traceID uint64) bool { return true },
})
span := tracer.StartSpan("x")
span.Finish()
assert.Equal(t, 1, len(recorder.GetSampledSpans()), "by default span should be sampled")
recorder.Reset()
span = tracer.StartSpan("x")
ext.SamplingPriority.Set(span, 0)
span.Finish()
assert.Equal(t, 0, len(recorder.GetSampledSpans()), "SamplingPriority=0 should turn off sampling")
tracer = NewWithOptions(Options{
Recorder: recorder,
ShouldSample: func(traceID uint64) bool { return false },
})
recorder.Reset()
span = tracer.StartSpan("x")
span.Finish()
assert.Equal(t, 0, len(recorder.GetSampledSpans()), "by default span should not be sampled")
recorder.Reset()
span = tracer.StartSpan("x")
ext.SamplingPriority.Set(span, 1)
span.Finish()
assert.Equal(t, 1, len(recorder.GetSampledSpans()), "SamplingPriority=1 should turn on sampling")
}
func TestSpan_SingleLoggedTaggedSpan(t *testing.T) {
recorder := NewInMemoryRecorder()
tracer := NewWithOptions(Options{
Recorder: recorder,
ShouldSample: func(traceID uint64) bool { return true }, // always sample
})
span := tracer.StartSpan("x")
span.LogEventWithPayload("event", "payload")
span.LogFields(log.String("key_str", "value"), log.Uint32("32bit", 4294967295))
span.SetTag("tag", "value")
span.Finish()
spans := recorder.GetSpans()
assert.Equal(t, 1, len(spans))
assert.Equal(t, "x", spans[0].Operation)
assert.Equal(t, 2, len(spans[0].Logs))
assert.Equal(t, opentracing.Tags{"tag": "value"}, spans[0].Tags)
fv := NewLogFieldValidator(t, spans[0].Logs[0].Fields)
fv.
ExpectNextFieldEquals("event", reflect.String, "event").
ExpectNextFieldEquals("payload", reflect.Interface, "payload")
fv = NewLogFieldValidator(t, spans[0].Logs[1].Fields)
fv.
ExpectNextFieldEquals("key_str", reflect.String, "value").
ExpectNextFieldEquals("32bit", reflect.Uint32, "4294967295")
}
func TestSpan_TrimUnsampledSpans(t *testing.T) {
recorder := NewInMemoryRecorder()
// Tracer that trims only unsampled but always samples
tracer := NewWithOptions(Options{
Recorder: recorder,
ShouldSample: func(traceID uint64) bool { return true }, // always sample
TrimUnsampledSpans: true,
})
span := tracer.StartSpan("x")
span.LogFields(log.String("key_str", "value"), log.Uint32("32bit", 4294967295))
span.SetTag("tag", "value")
span.Finish()
spans := recorder.GetSpans()
assert.Equal(t, 1, len(spans))
assert.Equal(t, 1, len(spans[0].Logs))
assert.Equal(t, opentracing.Tags{"tag": "value"}, spans[0].Tags)
fv := NewLogFieldValidator(t, spans[0].Logs[0].Fields)
fv.
ExpectNextFieldEquals("key_str", reflect.String, "value").
ExpectNextFieldEquals("32bit", reflect.Uint32, "4294967295")
recorder.Reset()
// Tracer that trims only unsampled and never samples
tracer = NewWithOptions(Options{
Recorder: recorder,
ShouldSample: func(traceID uint64) bool { return false }, // never sample
TrimUnsampledSpans: true,
})
span = tracer.StartSpan("x")
span.LogFields(log.String("key_str", "value"), log.Uint32("32bit", 4294967295))
span.SetTag("tag", "value")
span.Finish()
spans = recorder.GetSpans()
assert.Equal(t, 1, len(spans))
assert.Equal(t, 0, len(spans[0].Logs))
assert.Equal(t, 0, len(spans[0].Tags))
}
func TestSpan_DropAllLogs(t *testing.T) {
recorder := NewInMemoryRecorder()
// Tracer that drops logs
tracer := NewWithOptions(Options{
Recorder: recorder,
ShouldSample: func(traceID uint64) bool { return true }, // always sample
DropAllLogs: true,
})
span := tracer.StartSpan("x")
span.LogFields(log.String("key_str", "value"), log.Uint32("32bit", 4294967295))
span.SetTag("tag", "value")
span.Finish()
spans := recorder.GetSpans()
assert.Equal(t, 1, len(spans))
assert.Equal(t, "x", spans[0].Operation)
assert.Equal(t, opentracing.Tags{"tag": "value"}, spans[0].Tags)
// Only logs are dropped
assert.Equal(t, 0, len(spans[0].Logs))
}
func TestSpan_MaxLogSperSpan(t *testing.T) {
for _, limit := range []int{1, 2, 3, 5, 10, 15, 20} {
for _, numLogs := range []int{1, 2, 3, 5, 10, 15, 20, 30, 40, 50} {
recorder := NewInMemoryRecorder()
// Tracer that only retains the last <limit> logs.
tracer := NewWithOptions(Options{
Recorder: recorder,
ShouldSample: func(traceID uint64) bool { return true }, // always sample
MaxLogsPerSpan: limit,
})
span := tracer.StartSpan("x")
for i := 0; i < numLogs; i++ {
span.LogKV("eventIdx", i)
}
span.Finish()
spans := recorder.GetSpans()
assert.Equal(t, 1, len(spans))
assert.Equal(t, "x", spans[0].Operation)
logs := spans[0].Logs
var firstLogs, lastLogs []opentracing.LogRecord
if numLogs <= limit {
assert.Equal(t, numLogs, len(logs))
firstLogs = logs
} else {
assert.Equal(t, limit, len(logs))
if len(logs) > 0 {
numOld := (len(logs) - 1) / 2
firstLogs = logs[:numOld]
lastLogs = logs[numOld+1:]
fv := NewLogFieldValidator(t, logs[numOld].Fields)
fv = fv.ExpectNextFieldEquals("event", reflect.String, "dropped Span logs")
fv = fv.ExpectNextFieldEquals(
"dropped_log_count", reflect.Int, strconv.Itoa(numLogs-limit+1),
)
fv.ExpectNextFieldEquals("component", reflect.String, "basictracer")
}
}
for i, lr := range firstLogs {
fv := NewLogFieldValidator(t, lr.Fields)
fv.ExpectNextFieldEquals("eventIdx", reflect.Int, strconv.Itoa(i))
}
for i, lr := range lastLogs {
fv := NewLogFieldValidator(t, lr.Fields)
fv.ExpectNextFieldEquals("eventIdx", reflect.Int, strconv.Itoa(numLogs-len(lastLogs)+i))
}
}
}
}