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

feat: Added support for fs.glob in Node 22+ #2369

Merged
merged 4 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions lib/instrumentation/core/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ function initialize(agent, fs, moduleName, shim) {
'ftruncate'
]

if (Object.hasOwnProperty.call(fs, 'glob') === true) {
// The `glob` method was added in Node 22.
methods.push('glob')
}

const nonRecordedMethods = ['write', 'read']

shim.record(fs, methods, recordFs)
Expand Down
70 changes: 70 additions & 0 deletions test/unit/instrumentation/core/fs.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'

const tap = require('tap')
const crypto = require('node:crypto')
const path = require('node:path')
const os = require('node:os')
const helper = require('../../../lib/agent_helper')
const { removeModules } = require('../../../lib/cache-buster')

const nodeMajor = parseInt(process.version.split('.')[0].replace('v', ''), 10)

tap.beforeEach((t) => {
t.context.agent = helper.instrumentMockedAgent()
t.context.fs = require('node:fs')
})

tap.afterEach((t) => {
helper.unloadAgent(t.context.agent)
removeModules(['node:fs'])
})

tap.test('stat method gets instrumented', (t) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already have tests in tests/integration/fs.tap.js. I would add the glob tests there and remove this

const { agent, fs } = t.context
const tmpdir = os.tmpdir()
const tmpfile = path.join(tmpdir, crypto.randomBytes(16).toString('hex'))
t.teardown(() => fs.unlinkSync(tmpfile))

fs.writeFileSync(tmpfile, 'foo')

helper.runInTransaction(agent, (tx) => {
fs.stat(tmpfile, (error) => {
t.error(error)
const segment = tx.trace.root.children.find((child) => child.name === 'Filesystem/stat')
t.ok(segment, 'has stat segment')

tx.end()
t.end()
})
})
})

tap.test('glob method gets instrumented', { skip: nodeMajor < 22 }, (t) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when migrating this test over, the assertions are with segments and metrics

const { agent, fs } = t.context
const tmpdir = os.tmpdir()
const tmpfileName = crypto.randomBytes(16).toString('hex')
const tmpfile = path.join(tmpdir, tmpfileName)
t.teardown(() => fs.unlinkSync(tmpfile))

fs.writeFileSync(tmpfile, 'foo')

helper.runInTransaction(agent, (tx) => {
fs.glob(`${tmpdir}${path.sep}*`, (error, matches) => {
t.error(error)

const match = matches.find((m) => m.includes(tmpfileName))
t.ok(match, 'glob found file')

const segment = tx.trace.root.children.find((child) => child.name === 'Filesystem/glob')
t.ok(segment, 'has glob segment')

tx.end()
t.end()
})
})
})
Loading