apm-agent-nodejs icon indicating copy to clipboard operation
apm-agent-nodejs copied to clipboard

it is not instrumenting @koa/router when using ESM

Open sibelius opened this issue 8 months ago • 2 comments

Describe the bug

related to https://github.com/elastic/apm-agent-nodejs/issues/4518

__dirname breaks ESM

for (let info of MODULE_PATCHERS) {
    let patcher;
    if (info.patcher) {
      patcher = path.resolve(__dirname, info.patcher);
    } else {
      // Typically the patcher module for the APM agent's included
      // instrumentations is "./modules/${modPath}[.js]".
      patcher = path.resolve(
        __dirname,
        'modules',
        info.modPath + (info.modPath.endsWith('.js') ? '' : '.js'),
      );
    }

To Reproduce

try to instrument @koa/router

I'm using this config to enable apm

import { build } from 'esbuild';

build({
  bundle: true,
  platform: 'node',
  target: 'node22.12.0',
  format: 'esm',
  external: ['@koa/router', 'elastic-apm-node', 'mongodb', 'libxmljs2', 'bull'],
  banner: {
    js: `
    import { createRequire } from 'module'; 
    import { URL } from 'node:url';
    const require = createRequire(import.meta.url);
    const __filename = new URL('', import.meta.url).pathname;
    `,
  },
  sourcemap: true,
}).catch(() => process.exit(1));

ENV NODE_OPTIONS="--no-experimental-fetch --enable-source-maps --trace-warnings --trace-deprecation -r elastic-apm-node/start"

Expected behavior

it should instrument

Environment (please complete the following information)

  • OS: [e.g. Linux] linux
  • Node.js version: node 22
  • APM Server version: 4
  • Agent version: 4.1.11

How are you starting the agent? (please tick one of the boxes)

  • [ ] Calling agent.start() directly (e.g. require('elastic-apm-node').start(...))
  • [x] Requiring elastic-apm-node/start from within the source code
  • [x] Starting node with -r elastic-apm-node/start

Additional context

trying to fix @koa/router instrumentation

Agent config options:

Click to expand
replace this line with your agent config options

package.json dependencies:

Click to expand
replace this line with your dependencies section from package.json

sibelius avatar Mar 17 '25 19:03 sibelius

@sibelius has that ever worked? I had previously tried something very similar and it would not instrument because esbuild would eliminate the instrumentation code during bundling. I have to treat elastic-apm-node as external or use a lambda layer.

I wish this thing had instrumentation hooks we could invoke explicitly earlier on, that would fix a lot.... ie. instrumentKoa(); then even ESM would be viable

curtdept avatar Jun 12 '25 21:06 curtdept

it works if you make it external

sibelius avatar Jun 12 '25 22:06 sibelius