Skip to content

"operation-start" event of the MongooseConnection #14648

Closed
@vkarpov15

Description

@vkarpov15

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsThis issue is due to a mistake or omission in the mongoosejs.com documentationenhancementThis issue is a user-facing general improvement that doesn't fix a bug or add a new feature

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions