-
Notifications
You must be signed in to change notification settings - Fork 225
/
config.test.js
983 lines (900 loc) · 25.7 KB
/
config.test.js
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
/*
* Copyright Elasticsearch B.V. and other contributors where applicable.
* Licensed under the BSD 2-Clause License; you may not use this file except in
* compliance with the BSD 2-Clause License.
*/
'use strict';
var IncomingMessage = require('http').IncomingMessage;
var util = require('util');
var test = require('tape');
const Agent = require('../lib/agent');
const { MockAPMServer } = require('./_mock_apm_server');
const { NoopApmClient } = require('../lib/apm-client/noop-apm-client');
const { ENV_TABLE } = require('../lib/config/schema');
const config = require('../lib/config/config');
// Options to pass to `agent.start()` to turn off some default agent behavior
// that is unhelpful for these tests.
const agentOpts = {
centralConfig: false,
captureExceptions: false,
metricsInterval: '0s',
cloudProvider: 'none',
logLevel: 'warn',
};
const agentOptsNoopTransport = Object.assign({}, agentOpts, {
transport: function createNoopTransport() {
// Avoid accidentally trying to send data to an APM server.
return new NoopApmClient();
},
});
// ---- support functions
function assertEncodedTransaction(t, trans, result) {
t.comment('transaction');
t.strictEqual(result.id, trans.id, 'id matches');
t.strictEqual(result.trace_id, trans.traceId, 'trace id matches');
t.strictEqual(result.parent_id, trans.parentId, 'parent id matches');
t.strictEqual(result.name, trans.name, 'name matches');
t.strictEqual(result.type, trans.type || 'custom', 'type matches');
t.strictEqual(result.duration, trans._duration, 'duration matches');
t.strictEqual(result.timestamp, trans.timestamp, 'timestamp matches');
t.strictEqual(result.result, trans.result, 'result matches');
t.strictEqual(result.sampled, trans.sampled, 'sampled matches');
}
function assertEncodedSpan(t, span, result) {
t.comment('span');
t.strictEqual(result.id, span.id, 'id matches');
t.strictEqual(
result.transaction_id,
span.transaction.id,
'transaction id matches',
);
t.strictEqual(result.trace_id, span.traceId, 'trace id matches');
t.strictEqual(result.parent_id, span.parentId, 'parent id matches');
t.strictEqual(result.name, span.name, 'name matches');
t.strictEqual(result.type, span.type || 'custom', 'type matches');
t.strictEqual(result.duration, span._duration, 'duration matches');
t.strictEqual(result.timestamp, span.timestamp, 'timestamp matches');
}
function assertEncodedError(t, error, result, trans, parent) {
t.comment('error');
t.ok(result.id, 'has a valid id');
t.strictEqual(result.trace_id, trans.traceId, 'trace id matches');
t.strictEqual(result.transaction_id, trans.id, 'transaction id matches');
t.strictEqual(result.parent_id, parent.id, 'parent id matches');
t.ok(result.exception, 'has an exception object');
t.strictEqual(
result.exception.message,
error.message,
'exception message matches',
);
t.strictEqual(
result.exception.type,
error.constructor.name,
'exception type matches',
);
t.ok(result.culprit, 'has a valid culprit');
t.ok(result.timestamp, 'has a valid timestamp');
}
// ---- tests
test('should overwrite option property active by ELASTIC_APM_ACTIVE', function (t) {
var agent = new Agent();
var opts = { serviceName: 'foo', secretToken: 'baz', active: true };
process.env.ELASTIC_APM_ACTIVE = 'false';
agent.start(Object.assign({}, agentOptsNoopTransport, opts));
t.strictEqual(agent._conf.active, false);
delete process.env.ELASTIC_APM_ACTIVE;
agent.destroy();
t.end();
});
var captureBodyTests = [
{ value: 'off', errors: '[REDACTED]', transactions: '[REDACTED]' },
{ value: 'transactions', errors: '[REDACTED]', transactions: 'test' },
{ value: 'errors', errors: 'test', transactions: '[REDACTED]' },
{ value: 'all', errors: 'test', transactions: 'test' },
];
captureBodyTests.forEach(function (captureBodyTest) {
test('captureBody => ' + captureBodyTest.value, function (t) {
const apmServer = new MockAPMServer();
apmServer.start(function (serverUrl) {
const agent = new Agent().start(
Object.assign({}, agentOpts, {
serverUrl,
captureBody: captureBodyTest.value,
}),
);
var req = new IncomingMessage();
req.socket = { remoteAddress: '127.0.0.1' };
req.headers['transfer-encoding'] = 'chunked';
req.headers['content-length'] = 4;
req.body = 'test';
var trans = agent.startTransaction();
trans.req = req;
trans.end();
agent.captureError(new Error('wat'), { request: req }, function () {
t.equal(apmServer.events.length, 3, 'apmServer got 3 events');
let data = apmServer.events[1].transaction;
t.ok(data, 'event 1 is a transaction');
t.strictEqual(
data.context.request.body,
captureBodyTest.transactions,
'transaction.context.request.body is ' + captureBodyTest.transactions,
);
data = apmServer.events[2].error;
t.ok(data, 'event 2 is an error');
t.strictEqual(
data.context.request.body,
captureBodyTest.errors,
'error.context.request.body is ' + captureBodyTest.errors,
);
agent.destroy();
apmServer.close();
t.end();
});
});
});
});
var usePathAsTransactionNameTests = [
{ value: true, url: '/foo/bar?baz=2', transactionName: 'GET /foo/bar' },
{ value: false, url: '/foo/bar?baz=2', transactionName: 'GET unknown route' },
];
usePathAsTransactionNameTests.forEach(function (usePathAsTransactionNameTest) {
test(
'usePathAsTransactionName => ' + usePathAsTransactionNameTest.value,
function (t) {
var sentTrans;
var agent = new Agent();
agent.start(
Object.assign({}, agentOptsNoopTransport, {
usePathAsTransactionName: usePathAsTransactionNameTest.value,
transport() {
return {
sendTransaction(trans, cb) {
sentTrans = trans;
if (cb) process.nextTick(cb);
},
flush(opts, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = {};
} else if (!opts) {
opts = {};
}
if (cb) process.nextTick(cb);
},
};
},
}),
);
var req = new IncomingMessage();
req.socket = { remoteAddress: '127.0.0.1' };
req.url = usePathAsTransactionNameTest.url;
req.method = 'GET';
var trans = agent.startTransaction();
trans.req = req;
trans.end();
agent.flush(function () {
t.ok(sentTrans, 'sent a transaction');
t.strictEqual(
sentTrans.name,
usePathAsTransactionNameTest.transactionName,
'transaction.name is ' + usePathAsTransactionNameTest.transactionName,
);
agent.destroy();
t.end();
});
},
);
});
test('disableInstrumentaions', function (t) {
var agent = new Agent();
// Spot-check some cases.
agent.start(
Object.assign({}, agentOptsNoopTransport, {
disableInstrumentations: 'foo,express , bar',
}),
);
t.strictEqual(agent._instrumentation._isModuleEnabled('express'), false);
t.strictEqual(agent._instrumentation._isModuleEnabled('http'), true);
agent.destroy();
t.end();
});
test('custom transport', function (t) {
class MyTransport {
constructor() {
this.transactions = [];
this.spans = [];
this.errors = [];
}
sendTransaction(data, cb) {
this.transactions.push(data);
if (cb) setImmediate(cb);
}
sendSpan(data, cb) {
this.spans.push(data);
if (cb) setImmediate(cb);
}
sendError(data, cb) {
this.errors.push(data);
if (cb) setImmediate(cb);
}
config() {}
flush(opts, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = {};
} else if (!opts) {
opts = {};
}
if (cb) setImmediate(cb);
}
supportsKeepingUnsampledTransaction() {
return true;
}
supportsActivationMethodField() {
return true;
}
}
const myTransport = new MyTransport();
var agent = new Agent();
agent.start(Object.assign({}, agentOpts, { transport: () => myTransport }));
var error = new Error('error');
var trans = agent.startTransaction('transaction');
var span = agent.startSpan('span');
agent.captureError(error);
span.end();
trans.end();
agent.flush(function () {
t.equal(
myTransport.transactions.length,
1,
'received correct number of transactions',
);
assertEncodedTransaction(t, trans, myTransport.transactions[0]);
t.equal(myTransport.spans.length, 1, 'received correct number of spans');
assertEncodedSpan(t, span, myTransport.spans[0]);
t.equal(myTransport.errors.length, 1, 'received correct number of errors');
assertEncodedError(t, error, myTransport.errors[0], trans, span);
agent.destroy();
t.end();
});
});
test('addPatch', function (t) {
const before = require('express');
const patch = require('./_patch');
delete require.cache[require.resolve('express')];
const agent = new Agent();
agent.start(
Object.assign({}, agentOptsNoopTransport, {
addPatch: 'express=./test/_patch.js',
}),
);
t.deepEqual(require('express'), patch(before));
agent.destroy();
t.end();
});
test('globalLabels should be received by transport', function (t) {
var globalLabels = {
foo: 'bar',
};
const apmServer = new MockAPMServer();
apmServer.start(function (serverUrl) {
const agent = new Agent().start(
Object.assign({}, agentOpts, {
serverUrl,
globalLabels,
}),
);
agent.captureError(new Error('trigger metadata'), function () {
t.equal(apmServer.events.length, 2, 'apmServer got 2 events');
const data = apmServer.events[0].metadata;
t.ok(data, 'first event is metadata');
t.deepEqual(
data.labels,
globalLabels,
'metadata.labels has globalLabels',
);
agent.destroy();
apmServer.close();
t.end();
});
});
});
test('instrument: false allows manual instrumentation', function (t) {
const apmServer = new MockAPMServer();
apmServer.start(function (serverUrl) {
const agent = new Agent().start(
Object.assign({}, agentOpts, {
serverUrl,
instrument: false,
}),
);
const trans = agent.startTransaction('trans');
trans.end();
agent.flush(function () {
t.equal(apmServer.events.length, 2, 'apmServer got 2 events');
const data = apmServer.events[1].transaction;
t.ok(data, 'second event is a transaction');
assertEncodedTransaction(t, trans, data);
agent.destroy();
apmServer.close();
t.end();
});
});
});
test('parsing of ARRAY and KEY_VALUE opts', function (t) {
var cases = [
{
opts: { transactionIgnoreUrls: ['foo', 'bar'] },
expect: { transactionIgnoreUrls: ['foo', 'bar'] },
},
{
opts: { transactionIgnoreUrls: 'foo' },
expect: { transactionIgnoreUrls: ['foo'] },
},
{
opts: { transactionIgnoreUrls: 'foo,bar' },
expect: { transactionIgnoreUrls: ['foo', 'bar'] },
},
{
env: { ELASTIC_APM_TRANSACTION_IGNORE_URLS: 'foo, bar' },
expect: { transactionIgnoreUrls: ['foo', 'bar'] },
},
{
opts: { transactionIgnoreUrls: ' \tfoo , bar ' },
expect: { transactionIgnoreUrls: ['foo', 'bar'] },
},
{
opts: { transactionIgnoreUrls: 'foo, bar bling' },
expect: { transactionIgnoreUrls: ['foo', 'bar bling'] },
},
{
opts: { elasticsearchCaptureBodyUrls: '*/_search, */_msearch/template ' },
expect: {
elasticsearchCaptureBodyUrls: ['*/_search', '*/_msearch/template'],
},
},
{
opts: { disableInstrumentations: 'foo, bar' },
expect: { disableInstrumentations: ['foo', 'bar'] },
},
{
opts: { addPatch: 'foo=./foo.js,bar=./bar.js' },
expect: {
addPatch: [
['foo', './foo.js'],
['bar', './bar.js'],
],
},
},
{
opts: { addPatch: ' foo=./foo.js, bar=./bar.js ' },
expect: {
addPatch: [
['foo', './foo.js'],
['bar', './bar.js'],
],
},
},
{
env: { ELASTIC_APM_ADD_PATCH: ' foo=./foo.js, bar=./bar.js ' },
expect: {
addPatch: [
['foo', './foo.js'],
['bar', './bar.js'],
],
},
},
{
opts: { globalLabels: 'foo=bar, spam=eggs' },
expect: {
globalLabels: [
['foo', 'bar'],
['spam', 'eggs'],
],
},
},
];
cases.forEach(function testOneCase({ opts, env, expect }) {
var origEnv = process.env;
try {
if (env) {
process.env = Object.assign({}, origEnv, env);
}
var cfg = config.createConfig(opts);
for (var field in expect) {
t.deepEqual(
cfg[field],
expect[field],
util.format('opts=%j env=%j -> %j', opts, env, expect),
);
}
} finally {
process.env = origEnv;
}
});
t.end();
});
test('transactionSampleRate precision', function (t) {
var cases = [
{
opts: { transactionSampleRate: 0 },
expect: { transactionSampleRate: 0 },
},
{
env: { ELASTIC_APM_TRANSACTION_SAMPLE_RATE: '0' },
expect: { transactionSampleRate: 0 },
},
{
opts: { transactionSampleRate: 0.0001 },
expect: { transactionSampleRate: 0.0001 },
},
{
opts: { transactionSampleRate: 0.00002 },
expect: { transactionSampleRate: 0.0001 },
},
{
env: { ELASTIC_APM_TRANSACTION_SAMPLE_RATE: '0.00002' },
expect: { transactionSampleRate: 0.0001 },
},
{
opts: { transactionSampleRate: 0.300000002 },
expect: { transactionSampleRate: 0.3 },
},
{
opts: { transactionSampleRate: 0.444444 },
expect: { transactionSampleRate: 0.4444 },
},
{
opts: { transactionSampleRate: 0.555555 },
expect: { transactionSampleRate: 0.5556 },
},
{
opts: { transactionSampleRate: 1 },
expect: { transactionSampleRate: 1 },
},
];
cases.forEach(function testOneCase({ opts, env, expect }) {
var origEnv = process.env;
try {
if (env) {
process.env = Object.assign({}, origEnv, env);
}
var cfg = config.createConfig(opts);
for (var field in expect) {
t.deepEqual(
cfg[field],
expect[field],
util.format('opts=%j env=%j -> %j', opts, env, expect),
);
}
} finally {
process.env = origEnv;
}
});
t.end();
});
test('should accept and normalize cloudProvider', function (t) {
const agentDefault = new Agent();
agentDefault.start({
disableSend: true,
});
t.equals(
agentDefault._conf.cloudProvider,
'auto',
'cloudProvider config defaults to auto',
);
agentDefault.destroy();
const agentGcp = new Agent();
agentGcp.start({
disableSend: true,
cloudProvider: 'gcp',
});
agentGcp.destroy();
t.equals(
agentGcp._conf.cloudProvider,
'gcp',
'cloudProvider can be set to gcp',
);
const agentAzure = new Agent();
agentAzure.start({
disableSend: true,
cloudProvider: 'azure',
});
agentAzure.destroy();
t.equals(
agentAzure._conf.cloudProvider,
'azure',
'cloudProvider can be set to azure',
);
const agentAws = new Agent();
agentAws.start({
disableSend: true,
cloudProvider: 'aws',
});
agentAws.destroy();
t.equals(
agentAws._conf.cloudProvider,
'aws',
'cloudProvider can be set to aws',
);
const agentNone = new Agent();
agentNone.start({
disableSend: true,
cloudProvider: 'none',
});
agentNone.destroy();
t.equals(
agentNone._conf.cloudProvider,
'none',
'cloudProvider can be set to none',
);
const agentUnknown = new Agent();
agentUnknown.start({
disableSend: true,
logLevel: 'off', // Silence the log.warn for the invalid cloudProvider value.
cloudProvider: 'this-is-not-a-thing',
});
agentUnknown.destroy();
t.equals(
agentUnknown._conf.cloudProvider,
'auto',
'unknown cloudProvider defaults to auto',
);
const agentGcpFromEnv = new Agent();
process.env.ELASTIC_APM_CLOUD_PROVIDER = 'gcp';
agentGcpFromEnv.start({
disableSend: true,
});
t.equals(
agentGcpFromEnv._conf.cloudProvider,
'gcp',
'cloudProvider can be set via env',
);
delete process.env.ELASTIC_APM_CLOUD_PROVIDER;
agentGcpFromEnv.destroy();
t.end();
});
test('should accept and normalize ignoreMessageQueues', function (suite) {
suite.test('ignoreMessageQueues defaults', function (t) {
const agent = new Agent();
agent.start(agentOptsNoopTransport);
t.equals(
agent._conf.ignoreMessageQueues.length,
0,
'ignore message queue defaults empty',
);
t.equals(
agent._conf.ignoreMessageQueuesRegExp.length,
0,
'ignore message queue regex defaults empty',
);
agent.destroy();
t.end();
});
suite.test('ignoreMessageQueues via configuration', function (t) {
const agent = new Agent();
agent.start(
Object.assign({}, agentOptsNoopTransport, {
ignoreMessageQueues: ['f*o', 'bar'],
}),
);
t.equals(
agent._conf.ignoreMessageQueues.length,
2,
'ignore message picks up configured values',
);
t.equals(
agent._conf.ignoreMessageQueuesRegExp.length,
2,
'ignore message queue regex picks up configured values',
);
t.ok(
agent._conf.ignoreMessageQueuesRegExp[0].test('faooooo'),
'wildcard converted to regular expression',
);
agent.destroy();
t.end();
});
suite.test('ignoreMessageQueues via env', function (t) {
const agent = new Agent();
process.env.ELASTIC_APM_IGNORE_MESSAGE_QUEUES = 'f*o,bar,baz';
agent.start(agentOptsNoopTransport);
t.equals(
agent._conf.ignoreMessageQueues.length,
3,
'ignore message queue picks up env values',
);
t.equals(
agent._conf.ignoreMessageQueuesRegExp.length,
3,
'ignore message queue regex picks up env values',
);
t.ok(
agent._conf.ignoreMessageQueuesRegExp[0].test('faooooo'),
'wildcard converted to regular expression',
);
agent.destroy();
t.end();
});
suite.end();
});
// `spanStackTraceMinDuration` is synthesized from itself and two deprecated
// config vars (`captureSpanStackTraces` and `spanFramesMinDuration`).
test('spanStackTraceMinDuration', (suite) => {
const spanStackTraceMinDurationTestScenarios = [
{
name: 'spanStackTraceMinDuration defaults to -1',
startOpts: {},
env: {},
expectedVal: -1,
},
{
name: 'spanStackTraceMinDuration defaults to milliseconds',
startOpts: {
spanStackTraceMinDuration: 40,
},
env: {},
expectedVal: 0.04,
},
{
name: 'ELASTIC_APM_SPAN_STACK_TRACE_MIN_DURATION defaults to milliseconds',
startOpts: {},
env: {
ELASTIC_APM_SPAN_STACK_TRACE_MIN_DURATION: '30',
},
expectedVal: 0.03,
},
{
name: 'a given spanStackTraceMinDuration=0s wins',
startOpts: {
spanStackTraceMinDuration: '0s',
captureSpanStackTraces: false,
spanFramesMinDuration: '500ms',
},
env: {},
expectedVal: 0,
},
{
name: 'a given spanStackTraceMinDuration=0s wins over envvars',
startOpts: {
spanStackTraceMinDuration: '0s',
},
env: {
ELASTIC_APM_CAPTURE_SPAN_STACK_TRACES: 'false',
ELASTIC_APM_SPAN_FRAMES_MIN_DURATION: '500ms',
},
expectedVal: 0,
},
{
name: 'a given spanStackTraceMinDuration=-1s wins',
startOpts: {
spanStackTraceMinDuration: '-1s',
captureSpanStackTraces: true,
},
env: {},
expectedVal: -1,
},
{
name: 'a given spanStackTraceMinDuration=50ms wins',
startOpts: {
spanStackTraceMinDuration: '50ms',
captureSpanStackTraces: false,
spanFramesMinDuration: '300ms',
},
env: {},
expectedVal: 0.05,
},
{
name: 'captureSpanStackTraces=true alone results in spanStackTraceMinDuration=10ms',
startOpts: {
captureSpanStackTraces: true,
},
env: {},
expectedVal: 0.01,
},
{
name: 'captureSpanStackTraces=false results in spanStackTraceMinDuration=-1',
startOpts: {
captureSpanStackTraces: false,
spanFramesMinDuration: '50ms', // this value is ignored
},
env: {},
expectedVal: -1,
},
{
name: 'ELASTIC_APM_CAPTURE_SPAN_STACK_TRACES=false results in spanStackTraceMinDuration=-1',
startOpts: {},
env: {
ELASTIC_APM_CAPTURE_SPAN_STACK_TRACES: 'false',
},
expectedVal: -1,
},
{
name: 'spanFramesMinDuration=0s results in spanStackTraceMinDuration=-1',
startOpts: {
spanFramesMinDuration: '0s',
},
env: {},
expectedVal: -1,
},
{
name: 'ELASTIC_APM_SPAN_FRAMES_MIN_DURATION=0s results in spanStackTraceMinDuration=-1',
startOpts: {},
env: {
ELASTIC_APM_SPAN_FRAMES_MIN_DURATION: '0s',
},
expectedVal: -1,
},
{
name: 'spanFramesMinDuration value takes if captureSpanStackTraces=true',
startOpts: {
spanFramesMinDuration: '55ms',
captureSpanStackTraces: 'true',
},
env: {},
expectedVal: 0.055,
},
{
name: 'spanFramesMinDuration value takes if ELASTIC_APM_CAPTURE_SPAN_STACK_TRACES=true',
startOpts: {
spanFramesMinDuration: '56ms',
},
env: {
ELASTIC_APM_CAPTURE_SPAN_STACK_TRACES: 'true',
},
expectedVal: 0.056,
},
{
name: 'spanFramesMinDuration value takes if captureSpanStackTraces unspecified',
startOpts: {
spanFramesMinDuration: '57ms',
},
env: {},
expectedVal: 0.057,
},
{
name: 'spanFramesMinDuration<0 is translated',
startOpts: {
spanFramesMinDuration: '-3',
},
env: {},
expectedVal: 0,
},
{
name: 'spanFramesMinDuration==0 is translated',
startOpts: {},
env: {
ELASTIC_APM_SPAN_FRAMES_MIN_DURATION: '0m',
},
expectedVal: -1,
},
];
spanStackTraceMinDurationTestScenarios.forEach((scenario) => {
suite.test(scenario.name, (t) => {
const preEnv = Object.assign({}, process.env);
for (const [k, v] of Object.entries(scenario.env)) {
process.env[k] = v;
}
const agent = new Agent();
agent.start(
Object.assign({}, agentOptsNoopTransport, scenario.startOpts),
);
t.notOk(
agent._conf.captureSpanStackTraces,
'captureSpanStackTraces is not set on agent._conf',
);
t.notOk(
agent._conf.spanFramesMinDuration,
'spanFramesMinDuration is not set on agent._conf',
);
t.strictEqual(
agent._conf.spanStackTraceMinDuration,
scenario.expectedVal,
`spanStackTraceMinDuration=${scenario.expectedVal}`,
);
agent.destroy();
for (const k of Object.keys(scenario.env)) {
if (k in preEnv) {
process.env[k] = preEnv[k];
} else {
delete process.env[k];
}
}
t.end();
});
});
suite.end();
});
test('contextManager', (suite) => {
const contextManagerTestScenarios = [
{
name: 'contextManager defaults to empty',
startOpts: {},
env: {},
expectedVal: undefined,
},
{
name: 'contextManager=asynchooks is valid',
startOpts: {
contextManager: 'asynchooks',
},
env: {},
expectedVal: 'asynchooks',
},
{
name: 'contextManager=asynclocalstorage is valid',
startOpts: {
contextManager: 'asynclocalstorage',
},
env: {},
expectedVal: 'asynclocalstorage',
},
{
name: 'ELASTIC_APM_CONTEXT_MANAGER works',
startOpts: {},
env: {
ELASTIC_APM_CONTEXT_MANAGER: 'asynchooks',
},
expectedVal: 'asynchooks',
},
{
name: 'contextManager=bogus',
startOpts: {
contextManager: 'bogus',
},
env: {},
expectedVal: undefined,
},
];
contextManagerTestScenarios.forEach((scenario) => {
suite.test(scenario.name, (t) => {
const preEnv = Object.assign({}, process.env);
// Tests run in Jenkins CI sets `ELASTIC_APM_CONTEXT_MANAGER`, which
// interferes with these tests.
delete process.env.ELASTIC_APM_CONTEXT_MANAGER;
for (const [k, v] of Object.entries(scenario.env)) {
process.env[k] = v;
}
const agent = new Agent();
agent.start(
Object.assign({}, agentOptsNoopTransport, scenario.startOpts),
);
t.strictEqual(
agent._conf.contextManager,
scenario.expectedVal,
`contextManager=${scenario.expectedVal}`,
);
agent.destroy();
for (const k of Object.keys(process.env)) {
if (!(k in preEnv)) {
delete process.env[k];
} else if (process.env[k] !== preEnv[k]) {
process.env[k] = preEnv[k];
}
}
t.end();
});
});
suite.end();
});
test('env variable names start with "ELASTIC_APM_" prefix', (t) => {
const names = Object.values(ENV_TABLE).flat();
const exclusions = [
// The KUBERNETES_ envvars are intentionally not prefixed per
// https://github.com/elastic/apm/blob/main/specs/agents/metadata.md#containerkubernetes-metadata
'KUBERNETES_NAMESPACE',
'KUBERNETES_NODE_NAME',
'KUBERNETES_POD_NAME',
'KUBERNETES_POD_UID',
];
for (const name of names) {
if (exclusions.indexOf(name) !== -1) {
continue;
}
t.ok(
name.indexOf('ELASTIC_APM_') === 0,
`${name} starts with ELASTIC_APM_`,
);
}
t.end();
});