Skip to content

Commit 0c3cfd8

Browse files
authored
feat: instrument core modules imported with require('node:...') (elastic#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
1 parent 1b19cfe commit 0c3cfd8

File tree

4 files changed

+110
-25
lines changed

4 files changed

+110
-25
lines changed

CHANGELOG.asciidoc

+21
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,27 @@ Notes:
3131
[[release-notes-3.x]]
3232
=== Node.js Agent version 3.x
3333
34+
35+
==== Unreleased
36+
37+
[float]
38+
===== Breaking changes
39+
40+
[float]
41+
===== Features
42+
43+
- Support instrumenting core modules when require'd with the optional
44+
https://nodejs.org/api/modules.html#core-modules['node:'-prefix].
45+
For example `require('node:http')` will now be instrumented.
46+
({issues}2816[#2816])
47+
48+
[float]
49+
===== Bug fixes
50+
51+
[float]
52+
===== Chores
53+
54+
3455
[[release-notes-3.38.0]]
3556
==== 3.38.0 2022/08/11
3657

package-lock.json

+26-23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"lint:license-files": "./dev-utils/gen-notice.sh --lint . # requires node >=16",
1515
"coverage": "COVERAGE=true ./test/script/run_tests.sh",
1616
"test": "./test/script/run_tests.sh",
17-
"test:deps": "dependency-check start.js index.js 'lib/**/*.js' 'test/**/*.js' --no-dev -i async_hooks -i perf_hooks -i parseurl",
17+
"test:deps": "dependency-check start.js index.js 'lib/**/*.js' 'test/**/*.js' --no-dev -i async_hooks -i perf_hooks -i parseurl -i node:http",
1818
"test:tav": "tav --quiet",
1919
"test:docs": "./test/script/docker/run_docs.sh",
2020
"test:types": "tsc --project test/types/tsconfig.json && tsc --project test/types/transpile/tsconfig.json && node test/types/transpile/index.js && tsc --project test/types/transpile-default/tsconfig.json && node test/types/transpile-default/index.js",
@@ -106,7 +106,7 @@
106106
"original-url": "^1.2.3",
107107
"pino": "^6.11.2",
108108
"relative-microtime": "^2.0.0",
109-
"require-in-the-middle": "^5.0.3",
109+
"require-in-the-middle": "^5.2.0",
110110
"semver": "^6.3.0",
111111
"set-cookie-serde": "^1.0.0",
112112
"shallow-clone-shim": "^2.0.0",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and other contributors where applicable.
3+
* Licensed under the BSD 2-Clause License; you may not use this file except in
4+
* compliance with the BSD 2-Clause License.
5+
*/
6+
7+
'use strict'
8+
9+
// Test that instrumentation of core modules works when required with the
10+
// 'node:' prefix.
11+
12+
const { CapturingTransport } = require('../../../_capturing_transport')
13+
14+
const apm = require('../../../..').start({
15+
serviceName: 'test-node-prefixed-http-require',
16+
captureExceptions: false,
17+
metricsInterval: 0,
18+
centralConfig: false,
19+
cloudProvider: 'none',
20+
transport () {
21+
return new CapturingTransport()
22+
}
23+
})
24+
25+
try {
26+
var http = require('node:http')
27+
} catch (_requireErr) {
28+
console.log(`# SKIP node ${process.version} doesn't support require('node:...')`)
29+
process.exit()
30+
}
31+
32+
const test = require('tape')
33+
const { findObjInArray } = require('../../../_utils')
34+
35+
test('node:http instrumentation works', function (t) {
36+
var server = http.createServer(function (req, res) {
37+
res.end('pong')
38+
})
39+
40+
server.listen(function onListen () {
41+
const aTrans = apm.startTransaction('aTrans', 'manual')
42+
const port = server.address().port
43+
const url = 'http://localhost:' + port
44+
http.get(url, function (res) {
45+
res.resume()
46+
res.on('end', function () {
47+
aTrans.end()
48+
server.close()
49+
apm.flush(() => {
50+
const aTrans_ = findObjInArray(apm._transport.transactions, 'name', 'aTrans')
51+
const httpClientSpan = findObjInArray(apm._transport.spans, 'name', `GET localhost:${port}`)
52+
const httpServerTrans = findObjInArray(apm._transport.transactions, 'type', 'request')
53+
t.ok(aTrans_ && httpClientSpan && httpServerTrans, 'received the expected trace objs')
54+
t.equal(httpClientSpan.parent_id, aTrans_.id, 'http client span is a child of the manual trans')
55+
t.equal(httpServerTrans.parent_id, httpClientSpan.id, 'http server trans is a child of the http client span')
56+
t.end()
57+
})
58+
})
59+
})
60+
})
61+
})

0 commit comments

Comments
 (0)