Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"operation-start" event of the MongooseConnection #14648

Closed
vkarpov15 opened this issue Jun 3, 2024 Discussed in #14607 · 0 comments · Fixed by #14659
Closed

"operation-start" event of the MongooseConnection #14648

vkarpov15 opened this issue Jun 3, 2024 Discussed in #14607 · 0 comments · Fixed by #14659
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Milestone

Comments

@vkarpov15
Copy link
Collaborator

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.

  1. We use Node.js async_hooks to set a context string (request ID) when a HTTP request comes.
  2. Then, we use the context to set any DB operation comment: options.comment = stringContext;
  3. 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.

@vkarpov15 vkarpov15 added docs This issue is due to a mistake or omission in the mongoosejs.com documentation enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature labels Jun 3, 2024
@vkarpov15 vkarpov15 added this to the Parking Lot milestone Jun 3, 2024
@vkarpov15 vkarpov15 modified the milestones: Parking Lot, 8.4.2 Jun 13, 2024
vkarpov15 added a commit that referenced this issue Jun 13, 2024
fix(connection): fix up some inconsistencies in operation-end event and add to docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Projects
None yet
1 participant