-
Notifications
You must be signed in to change notification settings - Fork 409
/
Copy pathoutbound.test.js
672 lines (572 loc) · 19.9 KB
/
outbound.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
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
'use strict'
const assert = require('node:assert')
const test = require('node:test')
const http = require('http')
const url = require('url')
const events = require('events')
const helper = require('../../../lib/agent_helper')
const NAMES = require('../../../../lib/metrics/names')
const instrumentOutbound = require('../../../../lib/instrumentation/core/http-outbound')
const hashes = require('../../../../lib/util/hashes')
const nock = require('nock')
const Segment = require('../../../../lib/transaction/trace/segment')
const { DESTINATIONS } = require('../../../../lib/config/attribute-filter')
const symbols = require('../../../../lib/symbols')
const HOSTNAME = 'localhost'
const PORT = 8890
const testSignatures = require('./outbound-utils')
function addSegment({ agent }) {
const transaction = agent.getTransaction()
transaction.type = 'web'
transaction.baseSegment = new Segment({
config: agent.config,
name: 'base-segment',
root: transaction.trace.root
})
}
test('instrumentOutbound', async (t) => {
t.beforeEach((ctx) => {
ctx.nr = {}
ctx.nr.agent = helper.loadMockedAgent()
})
t.afterEach((ctx) => {
helper.unloadAgent(ctx.nr.agent)
})
await t.test(
'should omit query parameters from path if attributes.enabled is false',
(t, end) => {
const { agent } = t.nr
agent.config.attributes.enabled = false
const req = new events.EventEmitter()
helper.runInTransaction(agent, function (transaction) {
instrumentOutbound(agent, { host: HOSTNAME, port: PORT }, makeFakeRequest)
const [child] = transaction.trace.getChildren(transaction.trace.root.id)
assert.deepEqual(child.getAttributes(), {})
function makeFakeRequest() {
req.path = '/asdf?a=b&another=yourself&thing&grownup=true'
return req
}
end()
})
}
)
await t.test('should omit query parameters from path if high_security is true', (t, end) => {
const { agent } = t.nr
agent.config.high_security = true
const req = new events.EventEmitter()
helper.runInTransaction(agent, function (transaction) {
instrumentOutbound(agent, { host: HOSTNAME, port: PORT }, makeFakeRequest)
const [child] = transaction.trace.getChildren(transaction.trace.root.id)
assert.deepEqual(child.getAttributes(), {
procedure: 'GET',
url: `http://${HOSTNAME}:${PORT}/asdf`
})
function makeFakeRequest() {
req.path = '/asdf?a=b&another=yourself&thing&grownup=true'
return req
}
end()
})
})
await t.test('should obfuscate url path if url_obfuscation regex pattern is set', (t, end) => {
const { agent } = t.nr
agent.config.url_obfuscation = {
enabled: true,
regex: {
pattern: '.*',
replacement: '/***'
}
}
const req = new events.EventEmitter()
helper.runInTransaction(agent, function (transaction) {
instrumentOutbound(agent, { host: HOSTNAME, port: PORT }, makeFakeRequest)
const [child] = transaction.trace.getChildren(transaction.trace.root.id)
assert.deepEqual(child.getAttributes(), {
procedure: 'GET',
url: `http://${HOSTNAME}:${PORT}/***`
})
function makeFakeRequest() {
req.path = '/asdf/foo/bar/baz?test=123&test2=456'
return req
}
end()
})
})
await t.test('should strip query parameters from path in transaction trace segment', (t, end) => {
const { agent } = t.nr
const req = new events.EventEmitter()
helper.runInTransaction(agent, function (transaction) {
const path = '/asdf'
const name = NAMES.EXTERNAL.PREFIX + HOSTNAME + ':' + PORT + path
instrumentOutbound(agent, { host: HOSTNAME, port: PORT }, makeFakeRequest)
const [child] = transaction.trace.getChildren(transaction.trace.root.id)
assert.equal(child.name, name)
function makeFakeRequest() {
req.path = '/asdf?a=b&another=yourself&thing&grownup=true'
return req
}
end()
})
})
await t.test('should save query parameters from path if attributes.enabled is true', (t, end) => {
const { agent } = t.nr
const req = new events.EventEmitter()
helper.runInTransaction(agent, function (transaction) {
agent.config.attributes.enabled = true
instrumentOutbound(agent, { host: HOSTNAME, port: PORT }, makeFakeRequest)
const [child] = transaction.trace.getChildren(transaction.trace.root.id)
assert.deepEqual(
child.attributes.get(DESTINATIONS.SPAN_EVENT),
{
hostname: HOSTNAME,
port: PORT,
url: `http://${HOSTNAME}:${PORT}/asdf`,
procedure: 'GET',
'request.parameters.a': 'b',
'request.parameters.another': 'yourself',
'request.parameters.thing': true,
'request.parameters.grownup': 'true'
},
'adds attributes to spans'
)
function makeFakeRequest() {
req.path = '/asdf?a=b&another=yourself&thing&grownup=true'
return req
}
end()
})
})
await t.test('should not accept an undefined path', (t, end) => {
const { agent } = t.nr
const req = new events.EventEmitter()
helper.runInTransaction(agent, function () {
assert.throws(
() => instrumentOutbound(agent, { host: HOSTNAME, port: PORT }, makeFakeRequest),
Error
)
end()
})
function makeFakeRequest() {
return req
}
})
await t.test('should accept a simple path with no parameters', (t, end) => {
const { agent } = t.nr
const req = new events.EventEmitter()
const path = '/newrelic'
helper.runInTransaction(agent, function (transaction) {
const name = NAMES.EXTERNAL.PREFIX + HOSTNAME + ':' + PORT + path
req.path = path
instrumentOutbound(agent, { host: HOSTNAME, port: PORT }, makeFakeRequest)
const [child] = transaction.trace.getChildren(transaction.trace.root.id)
assert.equal(child.name, name)
end()
})
function makeFakeRequest() {
req.path = path
return req
}
})
await t.test('should purge trailing slash', (t, end) => {
const { agent } = t.nr
const req = new events.EventEmitter()
const path = '/newrelic/'
helper.runInTransaction(agent, function (transaction) {
const name = NAMES.EXTERNAL.PREFIX + HOSTNAME + ':' + PORT + '/newrelic'
req.path = path
instrumentOutbound(agent, { host: HOSTNAME, port: PORT }, makeFakeRequest)
const [child] = transaction.trace.getChildren(transaction.trace.root.id)
assert.equal(child.name, name)
})
function makeFakeRequest() {
req.path = path
return req
}
end()
})
await t.test('should not throw if hostname is undefined', (t, end) => {
const { agent } = t.nr
const req = new events.EventEmitter()
helper.runInTransaction(agent, function () {
let req2 = null
assert.doesNotThrow(() => {
req2 = instrumentOutbound(agent, { port: PORT }, makeFakeRequest)
})
assert.equal(req2, req)
assert.ok(!req2[symbols.transactionInfo])
})
function makeFakeRequest() {
req.path = '/newrelic'
return req
}
end()
})
await t.test('should not throw if hostname is null', (t, end) => {
const { agent } = t.nr
const req = new events.EventEmitter()
helper.runInTransaction(agent, function () {
let req2 = null
assert.doesNotThrow(() => {
req2 = instrumentOutbound(agent, { host: null, port: PORT }, makeFakeRequest)
})
assert.equal(req2, req)
assert.ok(!req2[symbols.transactionInfo])
})
function makeFakeRequest() {
req.path = '/newrelic'
return req
}
end()
})
await t.test('should not throw if hostname is an empty string', (t, end) => {
const { agent } = t.nr
const req = new events.EventEmitter()
helper.runInTransaction(agent, function () {
let req2 = null
assert.doesNotThrow(() => {
req2 = instrumentOutbound(agent, { host: '', port: PORT }, makeFakeRequest)
})
assert.equal(req2, req)
assert.ok(!req2[symbols.transactionInfo])
})
function makeFakeRequest() {
req.path = '/newrelic'
return req
}
end()
})
await t.test('should not throw if port is undefined', (t, end) => {
const { agent } = t.nr
const req = new events.EventEmitter()
helper.runInTransaction(agent, function () {
let req2 = null
assert.doesNotThrow(() => {
req2 = instrumentOutbound(agent, { host: 'hostname' }, makeFakeRequest)
})
assert.equal(req2, req)
assert.ok(!req2[symbols.transactionInfo])
})
function makeFakeRequest() {
req.path = '/newrelic'
return req
}
end()
})
await t.test('should not crash when req.headers is null', (t, end) => {
const { agent } = t.nr
const req = new events.EventEmitter()
helper.runInTransaction(agent, function () {
const path = '/asdf'
instrumentOutbound(agent, { headers: null, host: HOSTNAME, port: PORT }, makeFakeRequest)
function makeFakeRequest(opts) {
assert.ok(opts.headers, 'should assign headers when null')
assert.ok(opts.headers.traceparent, 'traceparent should exist')
req.path = path
return req
}
})
end()
})
})
test('should add data from cat header to segment', async (t) => {
const encKey = 'gringletoes'
const appData = ['123#456', 'abc', 0, 0, -1, 'xyz']
t.beforeEach((ctx) => {
ctx.nr = {}
ctx.nr.agent = helper.instrumentMockedAgent({
cross_application_tracer: { enabled: true },
distributed_tracing: { enabled: false },
encoding_key: encKey,
trusted_account_ids: [123]
})
const obfData = hashes.obfuscateNameUsingKey(JSON.stringify(appData), encKey)
const server = http.createServer(function (req, res) {
res.writeHead(200, { 'x-newrelic-app-data': obfData })
res.end()
req.resume()
})
ctx.nr.server = server
return new Promise((resolve) => {
helper.randomPort((port) => {
server.listen(port, resolve)
})
})
})
t.afterEach((ctx) => {
helper.unloadAgent(ctx.nr.agent)
return new Promise((resolve) => {
ctx.nr.server.close(resolve)
})
})
await t.test('should use config.obfuscatedId as the x-newrelic-id header', (t, end) => {
const { agent, server } = t.nr
helper.runInTransaction(agent, function () {
addSegment({ agent })
const port = server.address().port
http
.get({ host: 'localhost', port }, function (res) {
const segment = agent.tracer.getSegment()
assert.equal(segment.catId, '123#456')
assert.equal(segment.catTransaction, 'abc')
assert.equal(segment.name, `ExternalTransaction/localhost:${port}/123#456/abc`)
assert.equal(segment.getAttributes().transaction_guid, 'xyz')
res.resume()
agent.getTransaction().end()
end()
})
.end()
})
})
await t.test('should not explode with invalid data', (t, end) => {
const { agent, server } = t.nr
helper.runInTransaction(agent, function () {
addSegment({ agent })
const port = server.address().port
http
.get({ host: 'localhost', port }, function (res) {
const segment = agent.tracer.getSegment()
assert.equal(segment.catId, '123#456')
assert.equal(segment.catTransaction, 'abc')
// TODO: port in metric is a known bug. issue #142
assert.equal(segment.name, `ExternalTransaction/localhost:${port}/123#456/abc`)
assert.equal(segment.getAttributes().transaction_guid, 'xyz')
res.resume()
agent.getTransaction().end()
end()
})
.end()
})
})
await t.test('should collect errors only if they are not being handled', (t, end) => {
const { agent } = t.nr
const emit = events.EventEmitter.prototype.emit
events.EventEmitter.prototype.emit = function (evnt) {
if (evnt === 'error') {
this.once('error', function () {})
}
return emit.apply(this, arguments)
}
t.afterEach(() => {
events.EventEmitter.prototype.emit = emit
})
helper.runInTransaction(agent, handled)
const expectedCode = 'ECONNREFUSED'
function handled(transaction) {
const req = http.get({ host: 'localhost', port: 12345 }, function () {})
req.on('close', function () {
assert.equal(transaction.exceptions.length, 0)
unhandled(transaction)
})
req.on('error', function (err) {
assert.equal(err.code, expectedCode)
})
req.end()
}
function unhandled(transaction) {
const req = http.get({ host: 'localhost', port: 12345 }, function () {})
req.on('close', function () {
assert.equal(transaction.exceptions.length, 1)
assert.equal(transaction.exceptions[0].error.code, expectedCode)
end()
})
req.end()
}
})
})
test('when working with http.request', async (t) => {
t.beforeEach((ctx) => {
ctx.nr = {}
ctx.nr.agent = helper.instrumentMockedAgent()
ctx.nr.tracer = helper.getTracer()
nock.disableNetConnect()
})
t.afterEach((ctx) => {
nock.enableNetConnect()
helper.unloadAgent(ctx.nr.agent)
})
await t.test('should accept port and hostname', (t, end) => {
const { agent, tracer } = t.nr
const host = 'http://www.google.com'
const path = '/index.html'
nock(host).get(path).reply(200, 'Hello from Google')
helper.runInTransaction(agent, function (transaction) {
http.get('http://www.google.com/index.html', function (res) {
const segment = tracer.getSegment()
assert.equal(segment.name, 'External/www.google.com/index.html')
res.resume()
transaction.end()
end()
})
})
})
await t.test('should conform to external segment spec', (t, end) => {
const { agent } = t.nr
const host = 'http://www.google.com'
const path = '/index.html'
nock(host).post(path).reply(200)
helper.runInTransaction(agent, function (transaction) {
const opts = url.parse(`${host}${path}`)
opts.method = 'POST'
const req = http.request(opts, function (res) {
const [child] = transaction.trace.getChildren(transaction.trace.root.id)
const attributes = child.getAttributes()
assert.equal(attributes.url, 'http://www.google.com/index.html')
assert.equal(attributes.procedure, 'POST')
res.resume()
transaction.end()
end()
})
req.end()
})
})
await t.test('should start and end segment', (t, end) => {
const { agent, tracer } = t.nr
const host = 'http://www.google.com'
const path = '/index.html'
nock(host).get(path).delay(10).reply(200, 'Hello from Google')
helper.runInTransaction(agent, function (transaction) {
http.get('http://www.google.com/index.html', function (res) {
const segment = tracer.getSegment()
assert.ok(segment.timer.hrstart instanceof Array)
assert.equal(segment.timer.hrDuration, null)
res.resume()
res.on('end', function onEnd() {
assert.ok(segment.timer.hrDuration instanceof Array)
assert.ok(segment.timer.getDurationInMillis() > 0)
transaction.end()
end()
})
})
})
})
await t.test('should not modify parent segment when parent segment opaque', (t, end) => {
const { agent, tracer } = t.nr
const host = 'http://www.google.com'
const paramName = 'testParam'
const path = `/index.html?${paramName}=value`
nock(host).get(path).reply(200, 'Hello from Google')
helper.runInTransaction(agent, (transaction) => {
const parentSegment = agent.tracer.createSegment({
name: 'ParentSegment',
parent: transaction.trace.root,
transaction
})
parentSegment.opaque = true
tracer.setSegment({ transaction, segment: parentSegment }) // make the current active segment
http.get(`${host}${path}`, (res) => {
const segment = tracer.getSegment()
assert.equal(segment, parentSegment)
assert.equal(segment.name, 'ParentSegment')
const attributes = segment.getAttributes()
assert.ok(!attributes.url)
assert.ok(!attributes[`request.parameters.${paramName}`])
res.resume()
transaction.end()
end()
})
})
})
await t.test('generates dt and w3c trace context headers to outbound request', (t, end) => {
const { agent } = t.nr
agent.config.trusted_account_key = 190
agent.config.account_id = 190
agent.config.primary_application_id = '389103'
const host = 'http://www.google.com'
const path = '/index.html'
let headers
nock(host)
.get(path)
.reply(200, function () {
const { transaction, segment } = agent.tracer.getContext()
assert.equal(segment.name, 'External/www.google.com')
headers = this.req.headers
assert.ok(headers.traceparent, 'traceparent header')
const [version, traceId, parentSpanId, sampledFlag] = headers.traceparent.split('-')
assert.equal(version, '00')
assert.equal(traceId, transaction.traceId)
assert.equal(parentSpanId, segment.id)
assert.equal(sampledFlag, '01')
assert.ok(headers.tracestate, 'tracestate header')
assert.ok(!headers.tracestate.includes('null'))
assert.ok(!headers.tracestate.includes('true'))
assert.ok(headers.newrelic, 'dt headers')
})
helper.runInTransaction(agent, (transaction) => {
http.get(`${host}${path}`, (res) => {
res.resume()
transaction.end()
const tc = transaction.traceContext
const valid = tc._validateAndParseTraceStateHeader(headers.tracestate)
assert.ok(valid.entryValid)
end()
})
})
})
await t.test('should only add w3c header when exclude_newrelic_header: true', (t, end) => {
const { agent } = t.nr
agent.config.distributed_tracing.exclude_newrelic_header = true
agent.config.trusted_account_key = 190
agent.config.account_id = 190
agent.config.primary_application_id = '389103'
const host = 'http://www.google.com'
const path = '/index.html'
let headers
nock(host)
.get(path)
.reply(200, function () {
headers = this.req.headers
assert.ok(headers.traceparent)
assert.equal(headers.traceparent.split('-').length, 4)
assert.ok(headers.tracestate)
assert.ok(!headers.tracestate.includes('null'))
assert.ok(!headers.tracestate.includes('true'))
assert.ok(!headers.newrelic)
})
helper.runInTransaction(agent, (transaction) => {
http.get(`${host}${path}`, (res) => {
res.resume()
transaction.end()
const tc = transaction.traceContext
const valid = tc._validateAndParseTraceStateHeader(headers.tracestate)
assert.equal(valid.entryValid, true)
end()
})
})
})
})
test('Should properly handle http(s) get and request signatures', async (t) => {
function beforeTest(ctx) {
ctx.nr = {}
ctx.nr.agent = helper.instrumentMockedAgent()
ctx.nr.tracer = helper.getTracer()
nock.disableNetConnect()
}
function afterTest(ctx) {
nock.enableNetConnect()
helper.unloadAgent(ctx.nr.agent)
}
await t.test('http.get', async (t) => {
t.beforeEach(beforeTest)
t.afterEach(afterTest)
await testSignatures('http', 'get', t)
})
await t.test('http.request', async (t) => {
t.beforeEach(beforeTest)
t.afterEach(afterTest)
await testSignatures('http', 'request', t)
})
await t.test('https.get', async (t) => {
t.beforeEach(beforeTest)
t.afterEach(afterTest)
await testSignatures('https', 'get', t)
})
await t.test('https.request', async (t) => {
t.beforeEach(beforeTest)
t.afterEach(afterTest)
await testSignatures('https', 'request', t)
})
})