elastic / elastic/apm-agent-nodejs
After hook bug
- Dominant language
- JavaScript
- Stars
- 594
- Forks
- 244
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 16
Description
**Describe the bug**
1. [AsyncHooksRunContextManager._after](https://github.com/elastic/apm-agent-nodejs/blob/v3.24.0/lib/instrumentation/run-context/AsyncHooksRunContextManager.js#L92) get undefined `asyncId`.
```js
_after (asyncId) {
if (asyncId === undefined) {
console.log('asyncId is undefined')
}
this._exitRunContext()
}
```
2. Calling `this._exitRunContext()` pops `BasicRunContextManager._stack`.
3. It makes `BasicRunContextManager._stack` empty.
4. Few initial transactions get `null` from `apm.startSpan` until all undefiend `asyncId` exhausted.
**To Reproduce**
Sorry, I couldn't find which module generates undefined `asyncId`. I guess bundler inserts some code before async hook is attached.
**Expected behavior**
If `asyncId` or `context` is `undefined`, `AsyncHooksRunContextManager._after` should skip `_exitRunContext`
```js
_after (asyncId) {
const context = this._runContextFromAsyncId.get(asyncId)
if (context !== undefined) {
this._exitRunContext()
}
}
```
**Environment (please complete the following information)**
- OS: Windows 11 Pro, 21H2, 22000.258
- Node.js version: 14.16.1
- APM Server version: 7.15.1
- Agent version: 3.24.0
**How are you starting the agent? (please tick one of the boxes)**
- [x] 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
```
{
serviceName: 'test',
secretToken: 'x',
serverUrl: 'https://x.apm.ap-northeast-2.aws.elastic-cloud.com:443',
}
```
- `package.json` dependencies:
Click to expand
```
"dependencies": {
"@nestjs/common": "^8.0.0",
"@nestjs/core": "^8.0.0",
"@nestjs/platform-express": "^8.0.0",
"elastic-apm-node": "^3.24.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0"
},
"devDependencies": {
"@nestjs/cli": "^8.0.0",
"@nestjs/schematics": "^8.0.0",
"@nestjs/testing": "^8.0.0",
"@types/express": "^4.17.13",
"@types/jest": "^26.0.24",
"@types/node": "^16.0.0",
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "^4.28.2",
"@typescript-eslint/parser": "^4.28.2",
"eslint": "^7.30.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"jest": "27.0.6",
"prettier": "^2.3.2",
"supertest": "^6.1.3",
"ts-jest": "^27.0.3",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"tsconfig-paths": "^3.10.1",
"typescript": "^4.3.5"
}
```
Contributor guide
Assessment
This issue has not been assessed yet.