Transaction auto-monitoring on MongoDB not working with promises
See original GitHub issueDescription
When using callbacks, transaction times are correctly reported on MongoDB operations, but these are not shown when using the promise implementation.
Either of these is working:
function withMongoose() {
return mongoose.model(x).find(query).lean();
}
function withCallbacks() {
return new Promise((resolve, result) => {
mongo.db.collection('x').find(query).toArray((err, res) => {
if (err) {
reject(err);
return;
}
resolve(res);
});
});
}
However this is not working:
function withPromises() {
return mongo.db.collection('x').find(query).toArray();
}
Here’s what it looks like when we switched from mongoose (withMongoose) to native driver promises (withPromises):
Expected Behavior
I expect the same result with the three methods above.
Steps to Reproduce
See code snippets.
Your Environment
NewRelic: 7.5.1 MongoDB (driver): 3.6.4 Mongoose: 5.11.17 Node: 14.16.0
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top Results From Across the Web
MongoDB transactions don't seem to be working
I have to update two collections. I want to make sure that either both the collections get updated or they don't.
Read more >mongoose doesn't abort the transaction when promise.all rejects
Try with startTransaction and commitTransaction . Something like this: const session = await mongoose.startSession(); session.
Read more >Transactions with Promise.all throwing ... - GitHub
I need help working with transactions and mongodb replica sets. ... and throws 'TransientTransactionError: Transaction 1 does not exist'.
Read more >Using Promises, async / await with MongoDB | by Ross Bulat
You are writing a backend service for your web app and you need to fetch data from your mongo cluster. The problem is,...
Read more >findOne transactions in Promise.all occur "NoSuchTransaction"
Description. MongoDB v4.4. Node.js driver: 3.6.6. As i know, find query don ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Ok I think I have a fix. This looks better
I will open a PR and get it reviewed by the team
Thanks for the code snippet @richardgarnier! I can reproduce. I’m still new on the Node.js agent team but I think the issue is that the mongodb instrumentation isn’t built to support
toArray
not having a callback. I will start working on a fix later today and open a PR against this. Thanks for submitting the issue and bringing it to our attention! If you can keep the code snippet where you wrap the call in a native Promise that would be a good workaround for the moment.