Closed
Description
Discussed in #14607
Originally posted by koresar May 20, 2024
We needed to add comment
to each mongodb command. This helps debugging all performance bottlenecks.
- We use Node.js
async_hooks
to set a context string (request ID) when a HTTP request comes. - Then, we use the context to set any DB operation comment:
options.comment = stringContext;
- Then, in MongoDB Atlas or local "system.profile" we clearly see the request ID which caused a long running query.
Here is the code:
mongoose.connection.on(
"operation-start",
function setCommentToStringContext({ params /*, _id, modelName, collectionName, method*/ } = {}) {
if (Array.isArray(params))
params[params.length - 1].comment = getTraceStringContext();
}
);
Since we were able to instrument each individual DB command, we found these 7 lines of code to be extraordinary helpful in solving a lot of complex issues. I just open Atlas -> Monitoring -> Query Insights -> click on the slow performing query -> "comment" value.
AMAZING!
However, there is no any documentation about the `operation-start" event. I assume it's on purpose.
I suggest making this part of the Mongoose API.