question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Transaction auto-monitoring on MongoDB not working with promises

See original GitHub issue

Description

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): image

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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
bizob2828commented, Jun 29, 2021

Ok I think I have a fix. This looks better

screenshot 2021-06-29 at 12 22 35 PM screenshot 2021-06-29 at 12 22 26 PM

I will open a PR and get it reviewed by the team

0reactions
bizob2828commented, Jun 29, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found