forked from iwanbk/go-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_run_test.go
388 lines (359 loc) · 12.4 KB
/
app_run_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
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package newrelic
import (
"encoding/json"
"fmt"
"reflect"
"testing"
"time"
"github.com/iwanbk/go-agent/internal"
)
func TestResponseCodeIsError(t *testing.T) {
cfg := NewConfig("my app", "0123456789012345678901234567890123456789")
cfg.ErrorCollector.IgnoreStatusCodes = append(cfg.ErrorCollector.IgnoreStatusCodes, 504)
run := newAppRun(cfg, internal.ConnectReplyDefaults())
for _, tc := range []struct {
Code int
IsError bool
}{
{Code: 0, IsError: false}, // gRPC
{Code: 1, IsError: true}, // gRPC
{Code: 5, IsError: false}, // gRPC
{Code: 6, IsError: true}, // gRPC
{Code: 99, IsError: true},
{Code: 100, IsError: false},
{Code: 199, IsError: false},
{Code: 200, IsError: false},
{Code: 300, IsError: false},
{Code: 399, IsError: false},
{Code: 400, IsError: true},
{Code: 404, IsError: false},
{Code: 503, IsError: true},
{Code: 504, IsError: false},
} {
if is := run.responseCodeIsError(tc.Code); is != tc.IsError {
t.Errorf("responseCodeIsError for %d, wanted=%v got=%v",
tc.Code, tc.IsError, is)
}
}
}
func TestCrossAppTracingEnabled(t *testing.T) {
// CAT should be enabled by default.
cfg := NewConfig("my app", "0123456789012345678901234567890123456789")
run := newAppRun(cfg, internal.ConnectReplyDefaults())
if enabled := run.Config.CrossApplicationTracer.Enabled; !enabled {
t.Error(enabled)
}
// DT gets priority over CAT.
cfg = NewConfig("my app", "0123456789012345678901234567890123456789")
cfg.DistributedTracer.Enabled = true
cfg.CrossApplicationTracer.Enabled = true
run = newAppRun(cfg, internal.ConnectReplyDefaults())
if enabled := run.Config.CrossApplicationTracer.Enabled; enabled {
t.Error(enabled)
}
cfg = NewConfig("my app", "0123456789012345678901234567890123456789")
cfg.DistributedTracer.Enabled = false
cfg.CrossApplicationTracer.Enabled = false
run = newAppRun(cfg, internal.ConnectReplyDefaults())
if enabled := run.Config.CrossApplicationTracer.Enabled; enabled {
t.Error(enabled)
}
cfg = NewConfig("my app", "0123456789012345678901234567890123456789")
cfg.DistributedTracer.Enabled = false
cfg.CrossApplicationTracer.Enabled = true
run = newAppRun(cfg, internal.ConnectReplyDefaults())
if enabled := run.Config.CrossApplicationTracer.Enabled; !enabled {
t.Error(enabled)
}
}
func TestTxnTraceThreshold(t *testing.T) {
// Test that the default txn trace threshold is the failing apdex.
cfg := NewConfig("my app", "0123456789012345678901234567890123456789")
run := newAppRun(cfg, internal.ConnectReplyDefaults())
threshold := run.txnTraceThreshold(1 * time.Second)
if threshold != 4*time.Second {
t.Error(threshold)
}
// Test that the trace threshold can be assigned to a fixed value.
cfg = NewConfig("my app", "0123456789012345678901234567890123456789")
cfg.TransactionTracer.Threshold.IsApdexFailing = false
cfg.TransactionTracer.Threshold.Duration = 3 * time.Second
run = newAppRun(cfg, internal.ConnectReplyDefaults())
threshold = run.txnTraceThreshold(1 * time.Second)
if threshold != 3*time.Second {
t.Error(threshold)
}
// Test that the trace threshold can be overwritten by server-side-config.
// with "apdex_f".
cfg = NewConfig("my app", "0123456789012345678901234567890123456789")
cfg.TransactionTracer.Threshold.IsApdexFailing = false
cfg.TransactionTracer.Threshold.Duration = 3 * time.Second
reply := internal.ConnectReplyDefaults()
json.Unmarshal([]byte(`{"agent_config":{"transaction_tracer.transaction_threshold":"apdex_f"}}`), &reply)
run = newAppRun(cfg, reply)
threshold = run.txnTraceThreshold(1 * time.Second)
if threshold != 4*time.Second {
t.Error(threshold)
}
// Test that the trace threshold can be overwritten by server-side-config.
// with a numberic value.
cfg = NewConfig("my app", "0123456789012345678901234567890123456789")
reply = internal.ConnectReplyDefaults()
json.Unmarshal([]byte(`{"agent_config":{"transaction_tracer.transaction_threshold":3}}`), &reply)
run = newAppRun(cfg, reply)
threshold = run.txnTraceThreshold(1 * time.Second)
if threshold != 3*time.Second {
t.Error(threshold)
}
}
var cfg = NewConfig("name", "license")
func TestEmptyReplyEventHarvestDefaults(t *testing.T) {
var run internal.HarvestConfigurer = newAppRun(cfg, &internal.ConnectReply{})
assertHarvestConfig(t, &run, expectHarvestConfig{
maxTxnEvents: internal.MaxTxnEvents,
maxCustomEvents: internal.MaxCustomEvents,
maxErrorEvents: internal.MaxErrorEvents,
maxSpanEvents: internal.MaxSpanEvents,
periods: map[internal.HarvestTypes]time.Duration{
internal.HarvestTypesAll: 60 * time.Second,
0: 60 * time.Second,
},
})
}
func TestEventHarvestFieldsAllPopulated(t *testing.T) {
reply, err := internal.ConstructConnectReply([]byte(`{"return_value":{
"event_harvest_config": {
"report_period_ms": 5000,
"harvest_limits": {
"analytic_event_data": 1,
"custom_event_data": 2,
"span_event_data": 3,
"error_event_data": 4
}
}
}}`), internal.PreconnectReply{})
if nil != err {
t.Fatal(err)
}
var run internal.HarvestConfigurer = newAppRun(cfg, reply)
assertHarvestConfig(t, &run, expectHarvestConfig{
maxTxnEvents: 1,
maxCustomEvents: 2,
maxErrorEvents: 4,
maxSpanEvents: 3,
periods: map[internal.HarvestTypes]time.Duration{
internal.HarvestMetricsTraces: 60 * time.Second,
internal.HarvestTypesEvents: 5 * time.Second,
},
})
}
func TestZeroReportPeriod(t *testing.T) {
reply, err := internal.ConstructConnectReply([]byte(`{"return_value":{
"event_harvest_config": {
"report_period_ms": 0
}
}}`), internal.PreconnectReply{})
if nil != err {
t.Fatal(err)
}
var run internal.HarvestConfigurer = newAppRun(cfg, reply)
assertHarvestConfig(t, &run, expectHarvestConfig{
maxTxnEvents: internal.MaxTxnEvents,
maxCustomEvents: internal.MaxCustomEvents,
maxErrorEvents: internal.MaxErrorEvents,
maxSpanEvents: internal.MaxSpanEvents,
periods: map[internal.HarvestTypes]time.Duration{
internal.HarvestTypesAll: 60 * time.Second,
0: 60 * time.Second,
},
})
}
func TestEventHarvestFieldsOnlySpanEvents(t *testing.T) {
reply, err := internal.ConstructConnectReply([]byte(`{"return_value":{
"event_harvest_config": {
"report_period_ms": 5000,
"harvest_limits": { "span_event_data": 3 }
}}}`), internal.PreconnectReply{})
if nil != err {
t.Fatal(err)
}
var run internal.HarvestConfigurer = newAppRun(cfg, reply)
assertHarvestConfig(t, &run, expectHarvestConfig{
maxTxnEvents: internal.MaxTxnEvents,
maxCustomEvents: internal.MaxCustomEvents,
maxErrorEvents: internal.MaxErrorEvents,
maxSpanEvents: 3,
periods: map[internal.HarvestTypes]time.Duration{
internal.HarvestTypesAll ^ internal.HarvestSpanEvents: 60 * time.Second,
internal.HarvestSpanEvents: 5 * time.Second,
},
})
}
func TestEventHarvestFieldsOnlyTxnEvents(t *testing.T) {
reply, err := internal.ConstructConnectReply([]byte(`{"return_value":{
"event_harvest_config": {
"report_period_ms": 5000,
"harvest_limits": { "analytic_event_data": 3 }
}}}`), internal.PreconnectReply{})
if nil != err {
t.Fatal(err)
}
var run internal.HarvestConfigurer = newAppRun(cfg, reply)
assertHarvestConfig(t, &run, expectHarvestConfig{
maxTxnEvents: 3,
maxCustomEvents: internal.MaxCustomEvents,
maxErrorEvents: internal.MaxErrorEvents,
maxSpanEvents: internal.MaxSpanEvents,
periods: map[internal.HarvestTypes]time.Duration{
internal.HarvestTypesAll ^ internal.HarvestTxnEvents: 60 * time.Second,
internal.HarvestTxnEvents: 5 * time.Second,
},
})
}
func TestEventHarvestFieldsOnlyErrorEvents(t *testing.T) {
reply, err := internal.ConstructConnectReply([]byte(`{"return_value":{
"event_harvest_config": {
"report_period_ms": 5000,
"harvest_limits": { "error_event_data": 3 }
}}}`), internal.PreconnectReply{})
if nil != err {
t.Fatal(err)
}
var run internal.HarvestConfigurer = newAppRun(cfg, reply)
assertHarvestConfig(t, &run, expectHarvestConfig{
maxTxnEvents: internal.MaxTxnEvents,
maxCustomEvents: internal.MaxCustomEvents,
maxErrorEvents: 3,
maxSpanEvents: internal.MaxSpanEvents,
periods: map[internal.HarvestTypes]time.Duration{
internal.HarvestTypesAll ^ internal.HarvestErrorEvents: 60 * time.Second,
internal.HarvestErrorEvents: 5 * time.Second,
},
})
}
func TestEventHarvestFieldsOnlyCustomEvents(t *testing.T) {
reply, err := internal.ConstructConnectReply([]byte(`{"return_value":{
"event_harvest_config": {
"report_period_ms": 5000,
"harvest_limits": { "custom_event_data": 3 }
}}}`), internal.PreconnectReply{})
if nil != err {
t.Fatal(err)
}
var run internal.HarvestConfigurer = newAppRun(cfg, reply)
assertHarvestConfig(t, &run, expectHarvestConfig{
maxTxnEvents: internal.MaxTxnEvents,
maxCustomEvents: 3,
maxErrorEvents: internal.MaxErrorEvents,
maxSpanEvents: internal.MaxSpanEvents,
periods: map[internal.HarvestTypes]time.Duration{
internal.HarvestTypesAll ^ internal.HarvestCustomEvents: 60 * time.Second,
internal.HarvestCustomEvents: 5 * time.Second,
},
})
}
func TestConfigurableHarvestNegativeReportPeriod(t *testing.T) {
h, err := internal.ConstructConnectReply([]byte(`{"return_value":{
"event_harvest_config": {
"report_period_ms": -1
}}}`), internal.PreconnectReply{})
if nil != err {
t.Fatal(err)
}
expect := time.Duration(internal.DefaultConfigurableEventHarvestMs) * time.Millisecond
if period := h.ConfigurablePeriod(); period != expect {
t.Fatal(expect, period)
}
}
func TestReplyTraceIDGenerator(t *testing.T) {
// Test that the default connect reply has a populated trace id
// generator that works.
reply := internal.ConnectReplyDefaults()
id1 := reply.TraceIDGenerator.GenerateTraceID()
id2 := reply.TraceIDGenerator.GenerateTraceID()
if len(id1) != 16 || len(id2) != 16 || id1 == id2 {
t.Error(id1, id2)
}
}
func TestConfigurableTxnEvents_withCollResponse(t *testing.T) {
h, err := internal.ConstructConnectReply([]byte(
`{"return_value":{
"event_harvest_config": {
"report_period_ms": 10000,
"harvest_limits": {
"analytic_event_data": 15
}
}
}}`), internal.PreconnectReply{})
if nil != err {
t.Fatal(err)
}
result := newAppRun(cfg, h).MaxTxnEvents()
if result != 15 {
t.Error(fmt.Sprintf("Unexpected max number of txn events, expected %d but got %d", 15, result))
}
}
func TestConfigurableTxnEvents_notInCollResponse(t *testing.T) {
reply, err := internal.ConstructConnectReply([]byte(
`{"return_value":{
"event_harvest_config": {
"report_period_ms": 10000
}
}}`), internal.PreconnectReply{})
if nil != err {
t.Fatal(err)
}
expected := 10
cfg.TransactionEvents.MaxSamplesStored = expected
result := newAppRun(cfg, reply).MaxTxnEvents()
if result != expected {
t.Error(fmt.Sprintf("Unexpected max number of txn events, expected %d but got %d", expected, result))
}
}
func TestConfigurableTxnEvents_configMoreThanMax(t *testing.T) {
h, err := internal.ConstructConnectReply([]byte(
`{"return_value":{
"event_harvest_config": {
"report_period_ms": 10000
}
}}`), internal.PreconnectReply{})
if nil != err {
t.Fatal(err)
}
cfg.TransactionEvents.MaxSamplesStored = internal.MaxTxnEvents + 100
result := newAppRun(cfg, h).MaxTxnEvents()
if result != internal.MaxTxnEvents {
t.Error(fmt.Sprintf("Unexpected max number of txn events, expected %d but got %d", internal.MaxTxnEvents, result))
}
}
type expectHarvestConfig struct {
maxTxnEvents int
maxCustomEvents int
maxErrorEvents int
maxSpanEvents int
periods map[internal.HarvestTypes]time.Duration
}
func assertHarvestConfig(t testing.TB, hc *internal.HarvestConfigurer, expect expectHarvestConfig) {
if h, ok := t.(interface {
Helper()
}); ok {
h.Helper()
}
if max := (*hc).MaxTxnEvents(); max != expect.maxTxnEvents {
t.Error(max, expect.maxTxnEvents)
}
if max := (*hc).MaxCustomEvents(); max != expect.maxCustomEvents {
t.Error(max, expect.maxCustomEvents)
}
if max := (*hc).MaxSpanEvents(); max != expect.maxSpanEvents {
t.Error(max, expect.maxSpanEvents)
}
if max := (*hc).MaxErrorEvents(); max != expect.maxErrorEvents {
t.Error(max, expect.maxErrorEvents)
}
if periods := (*hc).ReportPeriods(); !reflect.DeepEqual(periods, expect.periods) {
t.Error(periods, expect.periods)
}
}