Reputation: 730
I'm trying to create trace spans of my node.js with Mongoose application. One specific trace I would like to get is how long one Mongoose query, update or document.save method takes time.
What I know is that Mongoose has pre and post hooks for these methods: https://mongoosejs.com/docs/middleware.html
However I'm struggling to find a good example how to keep timer in context between the hooks so that I get the trace of one and the same operation execution. Is there a good way to accomplish this?
Upvotes: 2
Views: 5826
Reputation: 582
schema.pre('find', function() {
this._startTime = Date.now();
});
schema.post('find', function() {
if (this._startTime != null) {
console.log('Runtime in MS: ', Date.now() - this._startTime);
}
});
Reference From: https://github.com/Automattic/mongoose/issues/6376#issuecomment-385083763
Upvotes: 4
Reputation: 821
Use Insomnia, this will let you know the total time a query used to run, but most time it depend on your network and if you are working with local mongoDB or online. Hope it help
Upvotes: 0