elastic / elastic/apm-agent-nodejs

there are no spans for queries w/out exec() for mongoose 5.6.3

Open
#1,550 9 comments 0 reactions 1 assignee Claimed by @david-luna View on GitHub
agent-nodejs bug
Dominant language
JavaScript
Stars
594
Forks
244
Avg merge
1d 8h
Merged PRs (30d)
16

Description

**Describe the bug**
The library does not automatically creates span if there is no explicit `exec()` function call on query object.

Mongoose uses native Promise.
```javascript
mongoose.Promise = global.Promise;
```

Following code snippet is instrumented perfectly and span is created
```javascript
async function test() {
const result = await Model.find().exec();
console.log(result);
}
```

This code snippet cannot be automatically instrumented (span cannot be created)
```javascript
async function test() {
const result = await Model.find();
console.log(result);
}
```

Mongoose automatically calls `exec` when `then` is called https://github.com/Automattic/mongoose/blob/5.6.3/lib/query.js#L4297-L4299

**To Reproduce**

Run code snippets below in your app.
If you run with explicit `exec` call then span is created otherwise it won't.

**Expected behavior**

Span should be created w/ and w/out explicit `exec` function call.

**Environment (please complete the following information)**

- OS: Mac OS 10.15.1
- Node.js version: 12.13.0
- APM Server version: 7.4.0
- Agent version: 3.1.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`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.