Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wrap 'fs.realpath.native' when configured with asyncHooks=false #2403

Merged
merged 3 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ Notes:
[float]
===== Bug fixes

* Wrap `fs.realpath.native` when configured with `asyncHooks=false`. This
fixes using that function (which was undefined before this fix) and a
crash when importing fs-extra@10. ({issues}2401[#2401])

* A significant change was made to internal run context tracking (a.k.a. async
context tracking). There are no configuration changes or API changes for
custom instrumentation. ({pull}2181[#2181])
Expand Down
9 changes: 9 additions & 0 deletions lib/instrumentation/patch-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ module.exports = function (ins) {
if (dns.resolveNaptr) wrap(dns, 'resolveNaptr', activator)

var fs = require('fs')
var wrappedFsRealpathNative
if (fs.realpath.native) {
wrappedFsRealpathNative = wrap(fs.realpath, 'native', activator)
}

massWrap(
fs,
[
Expand Down Expand Up @@ -287,6 +292,10 @@ module.exports = function (ins) {
activator
)

if (wrappedFsRealpathNative) {
fs.realpath.native = wrappedFsRealpathNative
}

// only wrap lchown and lchmod on systems that have them.
if (fs.lchown) wrap(fs, 'lchown', activator) // eslint-disable-line node/no-deprecated-api
if (fs.lchmod) wrap(fs, 'lchmod', activator) // eslint-disable-line node/no-deprecated-api
Expand Down
33 changes: 33 additions & 0 deletions test/instrumentation/fs.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'

// Some testing of run-context tracking through 'fs' methods.

const apm = require('../..').start({
serviceName: 'test-fs',
captureExceptions: false,
metricsInterval: '0s',
centralConfig: false,
cloudProvider: 'none',
disableSend: true,
asyncHooks: false
})

const fs = require('fs')

const tape = require('tape')

if (typeof fs.realpath.native === 'function') {
// Before https://github.com/elastic/apm-agent-nodejs/issues/2401 this test
// would crash with asyncHooks=false
tape.test('fs.realpath.native', function (t) {
var trans = apm.startTransaction('t0')
var span = apm.startSpan('s1')
fs.realpath.native(__filename, function (err, resolvedPath) {
t.error(err, 'no error from fs.realpath.native')
t.equal(apm.currentSpan, span, 'apm.currentSpan is as expected')
span.end()
trans.end()
t.end()
})
})
}