Skip to content

it is not instrumenting @koa/router when using ESM #4519

@sibelius

Description

@sibelius

Describe the bug

related to #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(...))
  • Requiring elastic-apm-node/start from within the source code
  • 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions