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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With