CJS and ESM hooks for orchestrion
npm install @apm-js-collab/tracing-hooksTo load esm loader:
``js
// esm-loader.mjs
import { register } from 'node:module';
const instrumentations = [
{
channelName: 'channel1',
module: { name: 'pkg1', verisonRange: '>=1.0.0', filePath: 'index.js' },
functionQuery: {
className: 'Class1',
methodName: 'method1',
kind: 'Async'
}
},
{
channelName: 'channel2',
module: { name: 'pkg2', verisonRange: '>=1.0.0', filePath: 'index.js' },
functionQuery: {
className: 'Class2,
methodName: 'method2',
kind: 'Sync'
}
}
]
register('@apm-js-collab/tracing-hooks/hook.mjs', import.meta.url, {
data: { instrumentations }
});
`
To use the loader, you can run your Node.js application with the --import flag:
`bash`
node --import esm-loader.mjs your-app.js
To load CJS patch:
`js
// cjs-patch.js
const ModulePatch = require('@apm-js-collab/tracing-hooks')
const instrumentations = [
{
channelName: 'channel1',
module: { name: 'pkg1', verisonRange: '>=1.0.0', filePath: 'index.js' },
functionQuery: {
className: 'Class1',
methodName: 'method1',
kind: 'Async'
}
},
{
channelName: 'channel2',
module: { name: 'pkg2', verisonRange: '>=1.0.0', filePath: 'index.js' },
functionQuery: {
className: 'Class2',
methodName: 'method2',
kind: 'Sync'
}
}
]
const modulePatch = new ModulePatch({ instrumentations });
modulePatch.patch()
`
To use the CJS patch you can run your Node.js application with the --require flag:
`bash`
node --require cjs-patch.js your-app.js
The debug module is used to provide
insight into the patching process. Set DEBUG='@apm-js-collab*' to view these
logs.
Additionally, any patched files can be written out by enabling dump mode. This
is done by setting the environment variable TRACING_DUMP to any value. ByTRACING_DUMP_DIR
default, it will write out file to the system's temporary directory as the
parent directory. The target parent directory can be configured by setting
the environment variable to an absolute path. In eitherlib/index.js
case, the resolved filename of the module being patched is appended. For
example, if we are patching in the foo package, and we set/tmp/dump/
a base directory of , then the patched code will be written to/tmp/dump/foo/lib/index.js`.