forked from newrelic/go-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal_txn_test.go
556 lines (525 loc) · 16.7 KB
/
internal_txn_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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package newrelic
import (
"testing"
"time"
"github.com/newrelic/go-agent/internal"
"github.com/newrelic/go-agent/internal/cat"
"github.com/newrelic/go-agent/internal/sysinfo"
)
func TestShouldSaveTrace(t *testing.T) {
for _, tc := range []struct {
name string
expected bool
synthetics bool
tracerEnabled bool
collectTraces bool
duration time.Duration
threshold time.Duration
}{
{
name: "insufficient duration, all disabled",
expected: false,
synthetics: false,
tracerEnabled: false,
collectTraces: false,
duration: 1 * time.Second,
threshold: 2 * time.Second,
},
{
name: "insufficient duration, only synthetics enabled",
expected: false,
synthetics: true,
tracerEnabled: false,
collectTraces: false,
duration: 1 * time.Second,
threshold: 2 * time.Second,
},
{
name: "insufficient duration, only tracer enabled",
expected: false,
synthetics: false,
tracerEnabled: true,
collectTraces: false,
duration: 1 * time.Second,
threshold: 2 * time.Second,
},
{
name: "insufficient duration, only collect traces enabled",
expected: false,
synthetics: false,
tracerEnabled: false,
collectTraces: true,
duration: 1 * time.Second,
threshold: 2 * time.Second,
},
{
name: "insufficient duration, all normal flags enabled",
expected: false,
synthetics: false,
tracerEnabled: true,
collectTraces: true,
duration: 1 * time.Second,
threshold: 2 * time.Second,
},
{
name: "insufficient duration, all flags enabled",
expected: true,
synthetics: true,
tracerEnabled: true,
collectTraces: true,
duration: 1 * time.Second,
threshold: 2 * time.Second,
},
{
name: "sufficient duration, all disabled",
expected: false,
synthetics: false,
tracerEnabled: false,
collectTraces: false,
duration: 3 * time.Second,
threshold: 2 * time.Second,
},
{
name: "sufficient duration, only synthetics enabled",
expected: false,
synthetics: true,
tracerEnabled: false,
collectTraces: false,
duration: 3 * time.Second,
threshold: 2 * time.Second,
},
{
name: "sufficient duration, only tracer enabled",
expected: false,
synthetics: false,
tracerEnabled: true,
collectTraces: false,
duration: 3 * time.Second,
threshold: 2 * time.Second,
},
{
name: "sufficient duration, only collect traces enabled",
expected: false,
synthetics: false,
tracerEnabled: false,
collectTraces: true,
duration: 3 * time.Second,
threshold: 2 * time.Second,
},
{
name: "sufficient duration, all normal flags enabled",
expected: true,
synthetics: false,
tracerEnabled: true,
collectTraces: true,
duration: 3 * time.Second,
threshold: 2 * time.Second,
},
{
name: "sufficient duration, all flags enabled",
expected: true,
synthetics: true,
tracerEnabled: true,
collectTraces: true,
duration: 3 * time.Second,
threshold: 2 * time.Second,
},
} {
txn := &txn{}
cfg := NewConfig("my app", "0123456789012345678901234567890123456789")
cfg.TransactionTracer.Enabled = tc.tracerEnabled
cfg.TransactionTracer.Threshold.Duration = tc.threshold
cfg.TransactionTracer.Threshold.IsApdexFailing = false
reply := internal.ConnectReplyDefaults()
reply.CollectTraces = tc.collectTraces
txn.appRun = newAppRun(cfg, reply)
txn.Duration = tc.duration
if tc.synthetics {
txn.CrossProcess.Synthetics = &cat.SyntheticsHeader{}
txn.CrossProcess.SetSynthetics(tc.synthetics)
}
if actual := txn.shouldSaveTrace(); actual != tc.expected {
t.Errorf("%s: unexpected shouldSaveTrace value; expected %v; got %v", tc.name, tc.expected, actual)
}
}
}
func TestLazilyCalculateSampledTrue(t *testing.T) {
tx := &txn{}
tx.appRun = &appRun{}
tx.BetterCAT.Priority = 0.5
tx.sampledCalculated = false
tx.BetterCAT.Enabled = true
tx.Reply = &internal.ConnectReply{
AdaptiveSampler: internal.SampleEverything{},
}
out := tx.lazilyCalculateSampled()
if !out || !tx.BetterCAT.Sampled || !tx.sampledCalculated || tx.BetterCAT.Priority != 1.5 {
t.Error(out, tx.BetterCAT.Sampled, tx.sampledCalculated, tx.BetterCAT.Priority)
}
tx.Reply.AdaptiveSampler = internal.SampleNothing{}
out = tx.lazilyCalculateSampled()
if !out || !tx.BetterCAT.Sampled || !tx.sampledCalculated || tx.BetterCAT.Priority != 1.5 {
t.Error(out, tx.BetterCAT.Sampled, tx.sampledCalculated, tx.BetterCAT.Priority)
}
}
func TestLazilyCalculateSampledFalse(t *testing.T) {
tx := &txn{}
tx.appRun = &appRun{}
tx.BetterCAT.Priority = 0.5
tx.sampledCalculated = false
tx.BetterCAT.Enabled = true
tx.Reply = &internal.ConnectReply{
AdaptiveSampler: internal.SampleNothing{},
}
out := tx.lazilyCalculateSampled()
if out || tx.BetterCAT.Sampled || !tx.sampledCalculated || tx.BetterCAT.Priority != 0.5 {
t.Error(out, tx.BetterCAT.Sampled, tx.sampledCalculated, tx.BetterCAT.Priority)
}
tx.Reply.AdaptiveSampler = internal.SampleEverything{}
out = tx.lazilyCalculateSampled()
if out || tx.BetterCAT.Sampled || !tx.sampledCalculated || tx.BetterCAT.Priority != 0.5 {
t.Error(out, tx.BetterCAT.Sampled, tx.sampledCalculated, tx.BetterCAT.Priority)
}
}
func TestLazilyCalculateSampledCATDisabled(t *testing.T) {
tx := &txn{}
tx.appRun = &appRun{}
tx.BetterCAT.Priority = 0.5
tx.sampledCalculated = false
tx.BetterCAT.Enabled = false
tx.Reply = &internal.ConnectReply{
AdaptiveSampler: internal.SampleEverything{},
}
out := tx.lazilyCalculateSampled()
if out || tx.BetterCAT.Sampled || tx.sampledCalculated || tx.BetterCAT.Priority != 0.5 {
t.Error(out, tx.BetterCAT.Sampled, tx.sampledCalculated, tx.BetterCAT.Priority)
}
out = tx.lazilyCalculateSampled()
if out || tx.BetterCAT.Sampled || tx.sampledCalculated || tx.BetterCAT.Priority != 0.5 {
t.Error(out, tx.BetterCAT.Sampled, tx.sampledCalculated, tx.BetterCAT.Priority)
}
}
type expectTxnTimes struct {
txn *txn
testName string
start time.Time
stop time.Time
duration time.Duration
totalTime time.Duration
}
func TestTransactionDurationTotalTime(t *testing.T) {
// These tests touch internal txn structures rather than the public API:
// Testing duration and total time is tough because our API functions do
// not take fixed times.
start := time.Now()
testTxnTimes := func(expect expectTxnTimes) {
if expect.txn.Start != expect.start {
t.Error("start time", expect.testName, expect.txn.Start, expect.start)
}
if expect.txn.Stop != expect.stop {
t.Error("stop time", expect.testName, expect.txn.Stop, expect.stop)
}
if expect.txn.Duration != expect.duration {
t.Error("duration", expect.testName, expect.txn.Duration, expect.duration)
}
if expect.txn.TotalTime != expect.totalTime {
t.Error("total time", expect.testName, expect.txn.TotalTime, expect.totalTime)
}
}
// Basic transaction with no async activity.
tx := &txn{}
tx.markStart(start)
segmentStart := internal.StartSegment(&tx.TxnData, &tx.mainThread, start.Add(1*time.Second))
internal.EndBasicSegment(&tx.TxnData, &tx.mainThread, segmentStart, start.Add(2*time.Second), "name")
tx.markEnd(start.Add(3*time.Second), &tx.mainThread)
testTxnTimes(expectTxnTimes{
txn: tx,
testName: "basic transaction",
start: start,
stop: start.Add(3 * time.Second),
duration: 3 * time.Second,
totalTime: 3 * time.Second,
})
// Transaction with async activity.
tx = &txn{}
tx.markStart(start)
segmentStart = internal.StartSegment(&tx.TxnData, &tx.mainThread, start.Add(1*time.Second))
internal.EndBasicSegment(&tx.TxnData, &tx.mainThread, segmentStart, start.Add(2*time.Second), "name")
asyncThread := createThread(tx)
asyncSegmentStart := internal.StartSegment(&tx.TxnData, asyncThread, start.Add(1*time.Second))
internal.EndBasicSegment(&tx.TxnData, asyncThread, asyncSegmentStart, start.Add(2*time.Second), "name")
tx.markEnd(start.Add(3*time.Second), &tx.mainThread)
testTxnTimes(expectTxnTimes{
txn: tx,
testName: "transaction with async activity",
start: start,
stop: start.Add(3 * time.Second),
duration: 3 * time.Second,
totalTime: 4 * time.Second,
})
// Transaction ended on async thread.
tx = &txn{}
tx.markStart(start)
segmentStart = internal.StartSegment(&tx.TxnData, &tx.mainThread, start.Add(1*time.Second))
internal.EndBasicSegment(&tx.TxnData, &tx.mainThread, segmentStart, start.Add(2*time.Second), "name")
asyncThread = createThread(tx)
asyncSegmentStart = internal.StartSegment(&tx.TxnData, asyncThread, start.Add(1*time.Second))
internal.EndBasicSegment(&tx.TxnData, asyncThread, asyncSegmentStart, start.Add(2*time.Second), "name")
tx.markEnd(start.Add(3*time.Second), asyncThread)
testTxnTimes(expectTxnTimes{
txn: tx,
testName: "transaction ended on async thread",
start: start,
stop: start.Add(3 * time.Second),
duration: 3 * time.Second,
totalTime: 4 * time.Second,
})
// Duration exceeds TotalTime.
tx = &txn{}
tx.markStart(start)
segmentStart = internal.StartSegment(&tx.TxnData, &tx.mainThread, start.Add(0*time.Second))
internal.EndBasicSegment(&tx.TxnData, &tx.mainThread, segmentStart, start.Add(1*time.Second), "name")
asyncThread = createThread(tx)
asyncSegmentStart = internal.StartSegment(&tx.TxnData, asyncThread, start.Add(2*time.Second))
internal.EndBasicSegment(&tx.TxnData, asyncThread, asyncSegmentStart, start.Add(3*time.Second), "name")
tx.markEnd(start.Add(3*time.Second), asyncThread)
testTxnTimes(expectTxnTimes{
txn: tx,
testName: "TotalTime should be at least Duration",
start: start,
stop: start.Add(3 * time.Second),
duration: 3 * time.Second,
totalTime: 3 * time.Second,
})
}
func TestGetTraceMetadataDistributedTracingDisabled(t *testing.T) {
replyfn := func(reply *internal.ConnectReply) {
reply.AdaptiveSampler = internal.SampleEverything{}
}
cfgfn := func(cfg *Config) {
cfg.DistributedTracer.Enabled = false
}
app := testApp(replyfn, cfgfn, t)
txn := app.StartTransaction("hello", nil, nil)
metadata := txn.GetTraceMetadata()
if metadata.SpanID != "" {
t.Error(metadata.SpanID)
}
if metadata.TraceID != "" {
t.Error(metadata.TraceID)
}
}
func TestGetTraceMetadataSuccess(t *testing.T) {
replyfn := func(reply *internal.ConnectReply) {
reply.AdaptiveSampler = internal.SampleEverything{}
reply.TraceIDGenerator = internal.NewTraceIDGenerator(12345)
}
cfgfn := func(cfg *Config) {
cfg.DistributedTracer.Enabled = true
}
app := testApp(replyfn, cfgfn, t)
txn := app.StartTransaction("hello", nil, nil)
metadata := txn.GetTraceMetadata()
if metadata.SpanID != "bcfb32e050b264b8" {
t.Error(metadata.SpanID)
}
if metadata.TraceID != "d9466896a525ccbf" {
t.Error(metadata.TraceID)
}
StartSegment(txn, "name")
// Span id should be different now that a segment has started.
metadata = txn.GetTraceMetadata()
if metadata.SpanID != "0e97aeb2f79d5d27" {
t.Error(metadata.SpanID)
}
if metadata.TraceID != "d9466896a525ccbf" {
t.Error(metadata.TraceID)
}
}
func TestGetTraceMetadataEnded(t *testing.T) {
// Test that GetTraceMetadata returns empty strings if the transaction
// has been finished.
replyfn := func(reply *internal.ConnectReply) {
reply.AdaptiveSampler = internal.SampleEverything{}
reply.TraceIDGenerator = internal.NewTraceIDGenerator(12345)
}
cfgfn := func(cfg *Config) {
cfg.DistributedTracer.Enabled = true
}
app := testApp(replyfn, cfgfn, t)
txn := app.StartTransaction("hello", nil, nil)
txn.End()
metadata := txn.GetTraceMetadata()
if metadata.SpanID != "" {
t.Error(metadata.SpanID)
}
if metadata.TraceID != "" {
t.Error(metadata.TraceID)
}
}
func TestGetTraceMetadataNotSampled(t *testing.T) {
replyfn := func(reply *internal.ConnectReply) {
reply.AdaptiveSampler = internal.SampleNothing{}
reply.TraceIDGenerator = internal.NewTraceIDGenerator(12345)
}
cfgfn := func(cfg *Config) {
cfg.DistributedTracer.Enabled = true
}
app := testApp(replyfn, cfgfn, t)
txn := app.StartTransaction("hello", nil, nil)
metadata := txn.GetTraceMetadata()
if metadata.SpanID != "" {
t.Error(metadata.SpanID)
}
if metadata.TraceID != "d9466896a525ccbf" {
t.Error(metadata.TraceID)
}
}
func TestGetTraceMetadataSpanEventsDisabled(t *testing.T) {
replyfn := func(reply *internal.ConnectReply) {
reply.AdaptiveSampler = internal.SampleEverything{}
reply.TraceIDGenerator = internal.NewTraceIDGenerator(12345)
}
cfgfn := func(cfg *Config) {
cfg.DistributedTracer.Enabled = true
cfg.SpanEvents.Enabled = false
}
app := testApp(replyfn, cfgfn, t)
txn := app.StartTransaction("hello", nil, nil)
metadata := txn.GetTraceMetadata()
if metadata.SpanID != "" {
t.Error(metadata.SpanID)
}
if metadata.TraceID != "d9466896a525ccbf" {
t.Error(metadata.TraceID)
}
}
func TestGetTraceMetadataInboundPayload(t *testing.T) {
replyfn := func(reply *internal.ConnectReply) {
reply.AdaptiveSampler = internal.SampleEverything{}
reply.TraceIDGenerator = internal.NewTraceIDGenerator(12345)
reply.AccountID = "account-id"
reply.TrustedAccountKey = "trust-key"
reply.PrimaryAppID = "app-id"
}
cfgfn := func(cfg *Config) {
cfg.DistributedTracer.Enabled = true
}
app := testApp(replyfn, cfgfn, t)
payload := app.StartTransaction("hello", nil, nil).CreateDistributedTracePayload()
p := payload.(internal.Payload)
p.TracedID = "trace-id"
txn := app.StartTransaction("hello", nil, nil)
err := txn.AcceptDistributedTracePayload(TransportHTTP, p)
if nil != err {
t.Error(err)
}
metadata := txn.GetTraceMetadata()
if metadata.SpanID != "9d2c19bd03daf755" {
t.Error(metadata.SpanID)
}
if metadata.TraceID != "trace-id" {
t.Error(metadata.TraceID)
}
}
func TestGetLinkingMetadata(t *testing.T) {
replyfn := func(reply *internal.ConnectReply) {
reply.AdaptiveSampler = internal.SampleEverything{}
reply.EntityGUID = "entities-are-guid"
reply.TraceIDGenerator = internal.NewTraceIDGenerator(12345)
}
cfgfn := func(cfg *Config) {
cfg.AppName = "app-name"
cfg.DistributedTracer.Enabled = true
}
app := testApp(replyfn, cfgfn, t)
txn := app.StartTransaction("hello", nil, nil)
metadata := txn.GetLinkingMetadata()
host, _ := sysinfo.Hostname()
if metadata.TraceID != "d9466896a525ccbf" {
t.Error("wrong TraceID:", metadata.TraceID)
}
if metadata.SpanID != "bcfb32e050b264b8" {
t.Error("wrong SpanID:", metadata.SpanID)
}
if metadata.EntityName != "app-name" {
t.Error("wrong EntityName:", metadata.EntityName)
}
if metadata.EntityType != "SERVICE" {
t.Error("wrong EntityType:", metadata.EntityType)
}
if metadata.EntityGUID != "entities-are-guid" {
t.Error("wrong EntityGUID:", metadata.EntityGUID)
}
if metadata.Hostname != host {
t.Error("wrong Hostname:", metadata.Hostname)
}
}
func TestGetLinkingMetadataAppNames(t *testing.T) {
testcases := []struct {
appName string
expected string
}{
{appName: "one-name", expected: "one-name"},
{appName: "one-name;two-name;three-name", expected: "one-name"},
{appName: "", expected: ""},
}
for _, test := range testcases {
cfgfn := func(cfg *Config) {
cfg.AppName = test.appName
}
app := testApp(nil, cfgfn, t)
txn := app.StartTransaction("hello", nil, nil)
metadata := txn.GetLinkingMetadata()
if metadata.EntityName != test.expected {
t.Errorf("wrong EntityName, actual=%s expected=%s", metadata.EntityName, test.expected)
}
}
}
func TestIsSampledFalse(t *testing.T) {
replyfn := func(reply *internal.ConnectReply) {
reply.AdaptiveSampler = internal.SampleNothing{}
}
cfgfn := func(cfg *Config) {
cfg.DistributedTracer.Enabled = true
}
app := testApp(replyfn, cfgfn, t)
txn := app.StartTransaction("hello", nil, nil)
sampled := txn.IsSampled()
if sampled == true {
t.Error("txn should not be sampled")
}
}
func TestIsSampledTrue(t *testing.T) {
replyfn := func(reply *internal.ConnectReply) {
reply.AdaptiveSampler = internal.SampleEverything{}
}
cfgfn := func(cfg *Config) {
cfg.DistributedTracer.Enabled = true
}
app := testApp(replyfn, cfgfn, t)
txn := app.StartTransaction("hello", nil, nil)
sampled := txn.IsSampled()
if sampled == false {
t.Error("txn should be sampled")
}
}
func TestIsSampledEnded(t *testing.T) {
// Test that Transaction.IsSampled returns false if the transaction has
// already ended.
replyfn := func(reply *internal.ConnectReply) {
reply.AdaptiveSampler = internal.SampleEverything{}
}
cfgfn := func(cfg *Config) {
cfg.DistributedTracer.Enabled = true
}
app := testApp(replyfn, cfgfn, t)
txn := app.StartTransaction("hello", nil, nil)
txn.End()
sampled := txn.IsSampled()
if sampled == true {
t.Error("finished txn should not be sampled")
}
}