Skip to content

require("elastic-apm-node") influences stacktrace generated with "node --enable-source-maps ..." #2833

Closed

Description

Describe the bug

To Reproduce

A. package.json

{
  "name": "testbug-stacktrace",
  "version": "0.0.0",
  "private": false,
  "dependencies": {
    "elastic-apm-node": "3.36.0"
  },
  "devDependencies": {
    "@types/node": "18.0.6",
    "typescript": "4.7.4"
  }
}

B. tsconfig.json

{
  "include": ["src/**/*.ts"],
  "compilerOptions": {
    "allowJs": true,
    "checkJs": false,
    "declaration": true,
    "emitDeclarationOnly": false,
    "sourceMap": true,
    // Generate JS compatible with Node >= 12 - see https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping
    "lib": ["ES2019"],
    "module": "commonjs",
    "target": "ES2019"
  }
}

C. src/main.js

const apm = require('elastic-apm-node');

const mod1 = require("./mod1");
mod1();

const { test } = require("./mod2");
test();

D. src/mod1.ts (CJS module)

module.exports = function test() {
  try {
    throw Error();
  } catch (err) {
    console.error(err);
  }
}

E. src/mod2.ts (ES6 module)

export async function test() {
  try {
    throw Error();
  } catch (err) {
    console.error(err);
  }
}

Then using node 16 :

npm install
node_modules\.bin\tsc
node --enable-source-maps src\main.js

Running above script results in :

Error
    at test (C:\Users\dtoch\work\pga\testbug-correct-stacktrace\src\mod1.js:3:15)
    at Object.<anonymous> (C:\Users\dtoch\work\pga\testbug-correct-stacktrace\src\main.js:4:1)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47
Error
    at test (C:\Users\dtoch\work\pga\testbug-correct-stacktrace\src\mod2.js:6:15)
    at Object.<anonymous> (C:\Users\dtoch\work\pga\testbug-correct-stacktrace\src\main.js:7:1)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

However when in src\main.js the first line (const apm = require('elastic-apm-node')) is put in comment, you will get the right stack trace:

C:\Users\dtoch\work\pga\testbug-correct-stacktrace\src\mod1.ts:3
    throw Error();
          ^

Error
    at null.test (C:\Users\dtoch\work\pga\testbug-correct-stacktrace\src\mod1.ts:3:11)
    at Object.<anonymous> (C:\Users\dtoch\work\pga\testbug-correct-stacktrace\src\main.js:4:1)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47
C:\Users\dtoch\work\pga\testbug-correct-stacktrace\src\mod2.ts:3
    throw Error();
          ^

Error
    at null.test (C:\Users\dtoch\work\pga\testbug-correct-stacktrace\src\mod2.ts:3:11)
    at Object.<anonymous> (C:\Users\dtoch\work\pga\testbug-correct-stacktrace\src\main.js:7:1)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

Expected behavior

Doing require('elastic-apm-node') should not influence the stacktrace generated by "node --enable-source-maps ...".

Environment (please complete the following information)

  • OS: [e.g. Linux] Windows
  • Node.js version: 16.14.2
  • APM Server version: Not important (don't need server to simulate problem)
  • Agent version: 3.36.0

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

  • 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
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions