forked from elastic/apm-agent-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: instrument core modules imported with require('node:...') (elas…
…tic#2868) Starting in node v14.18.0, support for identifying core node modules with the 'node:'-prefix was added: https://nodejs.org/api/modules.html#core-modules Closes: elastic#2816
- Loading branch information
Showing
4 changed files
with
110 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
test/instrumentation/modules/http/node-prefixed-http-require.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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' | ||
|
||
// Test that instrumentation of core modules works when required with the | ||
// 'node:' prefix. | ||
|
||
const { CapturingTransport } = require('../../../_capturing_transport') | ||
|
||
const apm = require('../../../..').start({ | ||
serviceName: 'test-node-prefixed-http-require', | ||
captureExceptions: false, | ||
metricsInterval: 0, | ||
centralConfig: false, | ||
cloudProvider: 'none', | ||
transport () { | ||
return new CapturingTransport() | ||
} | ||
}) | ||
|
||
try { | ||
var http = require('node:http') | ||
} catch (_requireErr) { | ||
console.log(`# SKIP node ${process.version} doesn't support require('node:...')`) | ||
process.exit() | ||
} | ||
|
||
const test = require('tape') | ||
const { findObjInArray } = require('../../../_utils') | ||
|
||
test('node:http instrumentation works', function (t) { | ||
var server = http.createServer(function (req, res) { | ||
res.end('pong') | ||
}) | ||
|
||
server.listen(function onListen () { | ||
const aTrans = apm.startTransaction('aTrans', 'manual') | ||
const port = server.address().port | ||
const url = 'http://localhost:' + port | ||
http.get(url, function (res) { | ||
res.resume() | ||
res.on('end', function () { | ||
aTrans.end() | ||
server.close() | ||
apm.flush(() => { | ||
const aTrans_ = findObjInArray(apm._transport.transactions, 'name', 'aTrans') | ||
const httpClientSpan = findObjInArray(apm._transport.spans, 'name', `GET localhost:${port}`) | ||
const httpServerTrans = findObjInArray(apm._transport.transactions, 'type', 'request') | ||
t.ok(aTrans_ && httpClientSpan && httpServerTrans, 'received the expected trace objs') | ||
t.equal(httpClientSpan.parent_id, aTrans_.id, 'http client span is a child of the manual trans') | ||
t.equal(httpServerTrans.parent_id, httpClientSpan.id, 'http server trans is a child of the http client span') | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |