db.setProfilingLevel() (original) (raw)

db.setProfilingLevel(level, options)

Changed in version 5.0.

For a mongod instance, the method enables, disables, or configures the Database Profiler. The profiler captures and records data on the performance of write operations, cursors, and database commands on a runningmongod instance. If the profiler is disabled, the method configures how slow operations are logged to the diagnostic log.

Note

Changes made to the profiling level with db.setProfilingLevel()do not persist. When the server restarts, it reverts to 0(the default), or the value set by either theoperationProfiling.mode setting or the--profile command-line option.

If the database profiler level is1 or 2 (specifically, the database profiler is enabled), theslowms,sampleRate affect the behavior of both the profiler and the diagnostic log.

If the database profiler level is0 (specifically, database profiler is disabled), theslowms andsampleRate, affect only the diagnostic log.

With mongos instances, the method sets the slowms, sampleRate and filterconfiguration settings, which configure how operations get written to the diagnostic log. You cannot enable theDatabase Profiler on amongos instance because mongos does not have any collections that the profiler can write to. The profile level must be 0 for a mongos instance.

You can specify a filter on bothmongod and mongos instances to control which operations are logged by the profiler. When you specify a filter for the profiler, the slowms, andsampleRate options are not used for profiling and slow-query log lines.

db.setProfilingLevel() provides a wrapper around theprofile command.

Starting in MongoDB 5.0, changes made to the database profiler level, slowms, sampleRate, orfilter using the profile command ordb.setProfilingLevel() wrapper method are recorded in thelog file.

This method is available in deployments hosted in the following environments:

Important

This command is not supported in M0, M2, M5, and Flex clusters. For more information, see Unsupported Commands.

The db.setProfilingLevel() method has the following form:


db.setProfilingLevel(<level>, <options>)

Parameter Type Description
level integer Configures the database profiler level. The following profiler levels are available:0The profiler is off and does not collect any data. This is the default profiler level.1The profiler collects data for operations that exceed theslowms threshold or match a specified filter.When a filter is set:The slowms and sampleRate options are not used for profiling.The profiler only captures operations that match thefilter.2The profiler collects data for all operations.Because profiling is not available on mongos,db.setProfilingLevel() cannot be used to set the profiling level to a value other than 0 on a mongos instance.
options document or integer Optional. Accepts an integer or an options document. If an integer value is passed as the options argument instead of a document, the value is assigned to slowms. The following options are available:slowmsDefault: 100Type: integerThe slow operation time threshold, in milliseconds. Operations that run for longer than this threshold are considered slow.Slow operations are logged based on workingMillis, which is the amount of time that MongoDB spends working on that operation. This means that factors such as waiting for locks and flow control do not affect whether an operation exceeds the slow operation threshold.When logLevel is set to 0, MongoDB records _slow_operations to the diagnostic log at a rate determined byslowOpSampleRate.At higher logLevel settings, all operations appear in the diagnostic log regardless of their latency with the following exception: the logging of slow oplog entry messages by the secondaries. The secondaries log only the slow oplog entries; increasing the logLevel does not log all oplog entries.For mongod instances, the setting affects both the diagnostic log and, if enabled, the profiler.For mongos instances, the setting affects the diagnostic log only and not the profiler because profiling is not available on mongos.This argument affects the same setting as the configuration file option slowOpThresholdMs.sampleRateDefault: 1.0Type: doubleThe fraction of slow operations that should be profiled or logged.sampleRate accepts values between 0 and 1, inclusive.For mongod instances, the setting affects both the diagnostic log and, if enabled, the profiler.For mongos instances, the setting affects the diagnostic log only and not the profiler because profiling is not available on mongos.This argument affects the same setting as the configuration optionslowOpSampleRate.filterType: objectA filter expression that controls which operations are profiled and logged. The field in the filter expression can be any field in the profiler output.For mongod instances, the setting affects both the diagnostic log and, if enabled, the profiler.For mongos instances, the setting affects the diagnostic log only and not the profiler because profiling is not available on mongos.Starting in MongoDB 8.0, you can specify workingMillisas a filter parameter to log operations based on the amount of time MongoDB spends working on that operation.For an example of a filter used to control logged operations, see Set a Filter to Determine Profiled Operations.When a profiling filter is set, theslowms andsampleRate options do not affect the diagnostic log or the profiler.

The method returns a document that contains the previous values of the settings.


{

   "was" : 2,

   "slowms" : 100,

   "sampleRate" : 1,

   "filter" : {

      "$and" : [

         {

            "op" : {

               "$eq" : "query"

            }

         },

         {

            "millis" : {

               "$gt" : 20000

            }

         }

      ]

   },

   "note" : "When a filter expression is set, slowms and sampleRate are not used for profiling and slow-query log lines.",

   "ok" : 1

}


{

   "was" : 0,

   "slowms" : 100,

   "sampleRate" : 1,

   "filter" : {

      "$and" : [

         {

            "op" : {

               "$eq" : "query"

            }

         },

         {

            "millis" : {

               "$gte" : 2000

            }

         }

      ]

   },

   "note" : "When a filter expression is set, slowms and sampleRate are not used for profiling and slow-query log lines.",

   "ok" : 1,

   "$clusterTime" : {

      "clusterTime" : Timestamp(1572991238, 1),

      "signature" : {

         "hash" : BinData(0,"hg6GnlrVhV9MAhwWdeHmHQ4T4qU="),

         "keyId" : NumberLong("6755945537557495811")

      }

   },

   "operationTime" : Timestamp(1572991238, 1)

}


{

   "was" : 0,

   "slowms" : 100,

   "sampleRate" : 1,

   "filter" : {

      "$and" : [

         {

            "op" : {

               "$eq" : "query"

            }

         },

         {

            "millis" : {

               "$gte" : 2000

            }

         }

      ]

   },

   "note" : "When a filter expression is set, slowms and sampleRate are not used for profiling and slow-query log lines.",

   "ok" : 1,

   "operationTime" : Timestamp(1572991499, 2),

   "$clusterTime" : {

      "clusterTime" : Timestamp(1572991499, 2),

      "signature" : {

         "hash" : BinData(0,"nhCquIxUw7thlrBudXe3PnsnvP0="),

         "keyId" : NumberLong("6755946491040235540")

      }

   }

}

Where:

Note

The filter and note fields only appear in the output if they were present in the previous level setting.

To view the current profiling level, see db.getProfilingStatus().

Warning

Profiling can degrade performance and expose unencrypted query data in the system log. Carefully consider any performance and security implications before configuring and enabling the profiler on a production deployment.

See Profiler Overhead for more information on potential performance degradation.

The following example sets for a mongod instance:


db.setProfilingLevel(1, { slowms: 20, sampleRate: 0.42 })

The operation returns a document with the previous values for the settings.

To view the current profiling level, seedb.getProfilingStatus().

The following example sets for a mongod ormongos instance:


db.setProfilingLevel(0, { slowms: 20, sampleRate: 0.42 })

The operation returns a document with the previous values for the settings.

To view the current profiling level, seedb.getProfilingStatus().

The following example sets for a mongod instance:


db.setProfilingLevel( 1, { filter: { op: "query", millis: { $gt: 2000 } } } )

The operation returns a document with the previous values for the settings.

To view the current profiling level, seedb.getProfilingStatus().

To clear a profile filter, run db.setProfilingLevel() with thefilter: "unset" option.


db.setProfilingLevel( 1, { filter: "unset" } )

The operation returns a document with the previous values for the settings.

To view the current profiling level, seedb.getProfilingStatus().