Skip to main content
Profiler can be used to setup logging on the mongo instance.

Events captured by the profiler:
*CRUD
*Administrative Operations
*Configuration Operations



If we check the profiling level method definition we can see how the profiling method is to be used.

> db.setProfilingLevel
function (level, options) {
        if (level < 0 || level > 2) {
            var errorText = "input level " + level + " is out of range [0..2]";
            var errorObject = new Error(errorText);
            errorObject['dbSetProfilingException'] = errorText;
            throw errorObject;
        }

        var cmd = {profile: level};
        if (isNumber(options)) {
            cmd.slowms = options;
        } else {
            cmd = Object.extend(cmd, options);
        }
        return assert.commandWorked(this._dbCommand(cmd));
    }

db.commandHelp method can be used to know details about different methods used in mongo.For profile setup the details about the command can be found as below:

> db.commandHelp("profile")
help for: profile controls the behaviour of the performance profiler, the fraction of eligible operations which are sampled for logging/profiling, and the threshold duration at which ops become eligible. See http://docs.mongodb.org/manual/reference/command/profile

Setting up profiling on the databases as level 2 can be done as below:
> db.setProfilingLevel(2)
{ "was" : 0, "slowms" : 100, "sampleRate" : 1, "ok" : 1 }

or

> db.setProfilingLevel(1,3)
{ "was" : 2, "slowms" : 100, "sampleRate" : 1, "ok" : 1 }

The details about the profiling set can be seen by queryinig the system collection
> db.system.profile.find().sort({$natural:-1}).limit(1).pretty()
{
        "op" : "query",
        "ns" : "config.system.profile",
        "command" : {
                "find" : "system.profile",
                "filter" : {

                },
                "lsid" : {
                        "id" : UUID("f563a6c1-6877-447b-93c5-59ff4bf75308")
                },
                "$db" : "config"
        },
        "keysExamined" : 0,
        "docsExamined" : 1,
        "cursorExhausted" : true,
        "numYield" : 0,
        "nreturned" : 1,
        "locks" : {
                "Global" : {
                        "acquireCount" : {
                                "r" : NumberLong(1)
                        }
                },
                "Database" : {
                        "acquireCount" : {
                                "r" : NumberLong(1)
                        }
                },
                "Collection" : {
                        "acquireCount" : {
                                "r" : NumberLong(1)
                        }
                }
        },
        "responseLength" : 1191,
        "protocol" : "op_msg",
        "millis" : 0,
        "planSummary" : "COLLSCAN",
        "execStats" : {
                "stage" : "COLLSCAN",
                "nReturned" : 1,
                "executionTimeMillisEstimate" : 0,
                "works" : 3,
                "advanced" : 1,
                "needTime" : 1,
                "needYield" : 0,
                "saveState" : 0,
                "restoreState" : 0,
                "isEOF" : 1,
                "invalidates" : 0,
                "direction" : "forward",
                "docsExamined" : 1
        },
        "ts" : ISODate("2019-03-23T15:37:27.842Z"),
        "client" : "127.0.0.1",
        "appName" : "MongoDB Shell",
        "allUsers" : [ ],
        "user" : ""
}

We can also see the last set of operations on the database by querying the system collection 
> db.system.profile.find({},{op:1}).sort({$natural:-1}).limit(4).pretty()
{ "op" : "query" }
{ "op" : "update" }
{ "op" : "query" }
{ "op" : "query" }

We can also check details about the profiling using
>show profile

To check what level of profiling is set on the instance we can use
> db.getProfilingStatus()
{ "was" : 2, "slowms" : 100, "sampleRate" : 1 }

In Profiling data will be writing from beginning again , once it reaches end in profile collection.It has no indexes also, to speed up write to profile collection. 

Comments