-
Notifications
You must be signed in to change notification settings - Fork 190
/
transaction_test.go
348 lines (296 loc) · 9.98 KB
/
transaction_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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package apm_test
import (
"fmt"
"math/rand"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.elastic.co/apm"
"go.elastic.co/apm/apmtest"
"go.elastic.co/apm/model"
"go.elastic.co/apm/transport"
"go.elastic.co/apm/transport/transporttest"
)
func TestStartTransactionTraceContextOptions(t *testing.T) {
testStartTransactionTraceContextOptions(t, false)
testStartTransactionTraceContextOptions(t, true)
}
func testStartTransactionTraceContextOptions(t *testing.T, recorded bool) {
tracer, _ := transporttest.NewRecorderTracer()
defer tracer.Close()
tracer.SetSampler(samplerFunc(func(apm.TraceContext) bool {
panic("nope")
}))
opts := apm.TransactionOptions{
TraceContext: apm.TraceContext{
Trace: apm.TraceID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
Span: apm.SpanID{0, 1, 2, 3, 4, 5, 6, 7},
},
}
opts.TraceContext.Options = opts.TraceContext.Options.WithRecorded(recorded)
tx := tracer.StartTransactionOptions("name", "type", opts)
result := tx.TraceContext()
assert.Equal(t, recorded, result.Options.Recorded())
tx.Discard()
}
func TestStartTransactionInvalidTraceContext(t *testing.T) {
startTransactionInvalidTraceContext(t, apm.TraceContext{
// Trace is all zeroes, which is invalid.
Span: apm.SpanID{0, 1, 2, 3, 4, 5, 6, 7},
})
}
func startTransactionInvalidTraceContext(t *testing.T, traceContext apm.TraceContext) {
tracer, _ := transporttest.NewRecorderTracer()
defer tracer.Close()
var samplerCalled bool
tracer.SetSampler(samplerFunc(func(apm.TraceContext) bool {
samplerCalled = true
return true
}))
opts := apm.TransactionOptions{TraceContext: traceContext}
tx := tracer.StartTransactionOptions("name", "type", opts)
assert.True(t, samplerCalled)
tx.Discard()
}
func TestStartTransactionTraceParentSpanIDSpecified(t *testing.T) {
startTransactionIDSpecified(t, apm.TraceContext{
Trace: apm.TraceID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
Span: apm.SpanID{0, 1, 2, 3, 4, 5, 6, 7},
})
}
func TestStartTransactionTraceIDSpecified(t *testing.T) {
startTransactionIDSpecified(t, apm.TraceContext{
Trace: apm.TraceID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
})
}
func TestStartTransactionIDSpecified(t *testing.T) {
startTransactionIDSpecified(t, apm.TraceContext{})
}
func startTransactionIDSpecified(t *testing.T, traceContext apm.TraceContext) {
tracer, _ := transporttest.NewRecorderTracer()
defer tracer.Close()
opts := apm.TransactionOptions{
TraceContext: traceContext,
TransactionID: apm.SpanID{0, 1, 2, 3, 4, 5, 6, 7},
}
tx := tracer.StartTransactionOptions("name", "type", opts)
assert.Equal(t, opts.TransactionID, tx.TraceContext().Span)
tx.Discard()
}
func TestTransactionEnsureParent(t *testing.T) {
tracer, transport := transporttest.NewRecorderTracer()
defer tracer.Close()
tx := tracer.StartTransaction("name", "type")
traceContext := tx.TraceContext()
parentSpan := tx.EnsureParent()
assert.NotZero(t, parentSpan)
assert.NotEqual(t, traceContext.Span, parentSpan)
// EnsureParent is idempotent.
parentSpan2 := tx.EnsureParent()
assert.Equal(t, parentSpan, parentSpan2)
tx.End()
// For an ended transaction, EnsureParent will return a zero value
// even if the transaction had a parent at the time it was ended.
parentSpan3 := tx.EnsureParent()
assert.Zero(t, parentSpan3)
tracer.Flush(nil)
payloads := transport.Payloads()
require.Len(t, payloads.Transactions, 1)
assert.Equal(t, model.SpanID(parentSpan), payloads.Transactions[0].ParentID)
}
func TestTransactionContextNotSampled(t *testing.T) {
tracer := apmtest.NewRecordingTracer()
defer tracer.Close()
tracer.SetSampler(samplerFunc(func(apm.TraceContext) bool { return false }))
tx := tracer.StartTransaction("name", "type")
tx.Context.SetLabel("foo", "bar")
tx.End()
tracer.Flush(nil)
payloads := tracer.Payloads()
require.Len(t, payloads.Transactions, 1)
assert.Nil(t, payloads.Transactions[0].Context)
}
func TestTransactionNotRecording(t *testing.T) {
tracer := apmtest.NewRecordingTracer()
defer tracer.Close()
tracer.SetRecording(false)
tracer.SetSampler(samplerFunc(func(apm.TraceContext) bool {
panic("should not be called")
}))
tx := tracer.StartTransaction("name", "type")
require.NotNil(t, tx)
require.NotNil(t, tx.TransactionData)
tx.End()
require.Nil(t, tx.TransactionData)
tracer.Flush(nil)
payloads := tracer.Payloads()
require.Empty(t, payloads.Transactions)
}
func TestTransactionSampleRate(t *testing.T) {
type test struct {
actualSampleRate float64
recordedSampleRate float64
expectedTraceState string
}
tests := []test{
{0, 0, "es=s:0"},
{1, 1, "es=s:1"},
{0.00001, 0.0001, "es=s:0.0001"},
{0.55554, 0.5555, "es=s:0.5555"},
{0.55555, 0.5556, "es=s:0.5556"},
{0.55556, 0.5556, "es=s:0.5556"},
}
for _, test := range tests {
test := test // copy for closure
t.Run(fmt.Sprintf("%v", test.actualSampleRate), func(t *testing.T) {
tracer := apmtest.NewRecordingTracer()
defer tracer.Close()
tracer.SetSampler(apm.NewRatioSampler(test.actualSampleRate))
tx := tracer.StartTransactionOptions("name", "type", apm.TransactionOptions{
// Use a known transaction ID for deterministic sampling.
TransactionID: apm.SpanID{0, 1, 2, 3, 4, 5, 6, 7},
})
tx.End()
tracer.Flush(nil)
payloads := tracer.Payloads()
assert.Equal(t, test.recordedSampleRate, *payloads.Transactions[0].SampleRate)
assert.Equal(t, test.expectedTraceState, tx.TraceContext().State.String())
})
}
}
func TestTransactionUnsampledSampleRate(t *testing.T) {
tracer := apmtest.NewRecordingTracer()
defer tracer.Close()
tracer.SetSampler(apm.NewRatioSampler(0.5))
// Create transactions until we get an unsampled one.
//
// Even though the configured sampling rate is 0.5,
// we record sample_rate=0 to ensure the server does
// not count the transaction toward metrics.
var tx *apm.Transaction
for {
tx = tracer.StartTransactionOptions("name", "type", apm.TransactionOptions{})
if !tx.Sampled() {
tx.End()
break
}
tx.Discard()
}
tracer.Flush(nil)
payloads := tracer.Payloads()
assert.Equal(t, float64(0), *payloads.Transactions[0].SampleRate)
assert.Equal(t, "es=s:0", tx.TraceContext().State.String())
}
func TestTransactionSampleRatePropagation(t *testing.T) {
tracer := apmtest.NewRecordingTracer()
defer tracer.Close()
for _, tracestate := range []apm.TraceState{
apm.NewTraceState(apm.TraceStateEntry{Key: "es", Value: "s:0.5"}),
apm.NewTraceState(apm.TraceStateEntry{Key: "es", Value: "x:y;s:0.5;zz:y"}),
apm.NewTraceState(
apm.TraceStateEntry{Key: "other", Value: "s:1.0"},
apm.TraceStateEntry{Key: "es", Value: "s:0.5"},
),
} {
tx := tracer.StartTransactionOptions("name", "type", apm.TransactionOptions{
TraceContext: apm.TraceContext{
Trace: apm.TraceID{1},
Span: apm.SpanID{1},
State: tracestate,
},
})
tx.End()
}
tracer.Flush(nil)
payloads := tracer.Payloads()
assert.Len(t, payloads.Transactions, 3)
for _, tx := range payloads.Transactions {
assert.Equal(t, 0.5, *tx.SampleRate)
}
}
func TestTransactionSampleRateOmission(t *testing.T) {
tracer := apmtest.NewRecordingTracer()
defer tracer.Close()
// For downstream transactions, sample_rate should be
// omitted if a valid value is not found in tracestate.
for _, tracestate := range []apm.TraceState{
apm.TraceState{}, // empty
apm.NewTraceState(apm.TraceStateEntry{Key: "other", Value: "s:1.0"}), // not "es", ignored
apm.NewTraceState(apm.TraceStateEntry{Key: "es", Value: "s:123.0"}), // out of range
apm.NewTraceState(apm.TraceStateEntry{Key: "es", Value: ""}), // 's' missing
apm.NewTraceState(apm.TraceStateEntry{Key: "es", Value: "wat"}), // malformed
} {
for _, sampled := range []bool{false, true} {
tx := tracer.StartTransactionOptions("name", "type", apm.TransactionOptions{
TraceContext: apm.TraceContext{
Trace: apm.TraceID{1},
Span: apm.SpanID{1},
Options: apm.TraceOptions(0).WithRecorded(sampled),
State: tracestate,
},
})
tx.End()
}
}
tracer.Flush(nil)
payloads := tracer.Payloads()
assert.Len(t, payloads.Transactions, 10)
for _, tx := range payloads.Transactions {
assert.Nil(t, tx.SampleRate)
}
}
func TestTransactionDiscard(t *testing.T) {
tracer, transport := transporttest.NewRecorderTracer()
defer tracer.Close()
tx := tracer.StartTransaction("name", "type")
tx.Discard()
assert.Nil(t, tx.TransactionData)
tx.End() // ending after discarding should be a no-op
tracer.Flush(nil)
payloads := transport.Payloads()
require.Empty(t, payloads)
}
func BenchmarkTransaction(b *testing.B) {
tracer, err := apm.NewTracer("service", "")
require.NoError(b, err)
tracer.Transport = transport.Discard
defer tracer.Close()
names := []string{}
for i := 0; i < 1000; i++ {
names = append(names, fmt.Sprintf("/some/route/%d", i))
}
var mu sync.Mutex
globalRand := rand.New(rand.NewSource(time.Now().UnixNano()))
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
mu.Lock()
rand := rand.New(rand.NewSource(globalRand.Int63()))
mu.Unlock()
for pb.Next() {
tx := tracer.StartTransaction(names[rand.Intn(len(names))], "type")
tx.End()
}
})
}
type samplerFunc func(apm.TraceContext) bool
func (f samplerFunc) Sample(t apm.TraceContext) bool {
return f(t)
}