Navigation
This version of the documentation is archived and no longer supported.

db.setProfilingLevel()

Definition

db.setProfilingLevel(level, options)

The method configures database profiler level, the slowms, and the sampleRate.

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

If the database profiler level is 0 (specifically, database profiler is disabled), the slowms and the sampleRate affect only the diagnostic log.

Although profiling is unavailable on mongos instance, starting in MongoDB 4.0, you can run db.setProfilingLevel() on mongos to set the slowms and sampleRate for the diagnostic log. That is, for a mongos instance, you must specify 0 for level.

db.setProfilingLevel() provides a wrapper around the profile command.

Starting in MongoDB 4.0.22, changes made to the database profiler profile level, slowms, or sampleRate using the profile command or db.setProfilingLevel() wrapper method are recorded in the log file.

Syntax

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

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

Parameters

Parameter Type Description
level integer

Configures the database profiler level. The following profiler levels are available:

Level Description
0 The profiler is off and does not collect any data. This is the default profiler level.
1 The profiler collects data for operations that take longer than the value of slowms.
2 The 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:

slowms
Default: 100
Type: integer

The slow operation time threshold, in milliseconds. Operations that run for longer than this threshold are considered slow.

When logLevel is set to 0, MongoDB records slow operations to the diagnostic log at a rate determined by slowOpSampleRate. Starting in MongoDB 4.0.6, the secondaries of replica sets log all oplog entry messages that take longer than the slow operation threshold to apply regardless of the sample rate.

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.

Note

This argument affects the same setting as the configuration file option slowOpThresholdMs.

sampleRate
Default: 1.0
Type: double

The 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.

Note

This argument affects the same setting as the configuration option slowOpSampleRate.

Returns

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

{ "was" : 0, "slowms" : 100, "sampleRate" : 1, "ok" : 1 }
{
   "was" : 0,
   "slowms" : 100,
   "sampleRate" : 1,
   "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,
   "ok" : 1,
   "operationTime" : Timestamp(1572991499, 2),
   "$clusterTime" : {
      "clusterTime" : Timestamp(1572991499, 2),
      "signature" : {
         "hash" : BinData(0,"nhCquIxUw7thlrBudXe3PnsnvP0="),
         "keyId" : NumberLong("6755946491040235540")
      }
   }
}

Where:

  • was is the previous level setting.
  • slowms is the previous slowms setting.
  • sampleRate is the previous sampleRate setting.

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

Behavior

Important

Profiling can impact performance and shares settings with 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.

Examples

Enable Profiler and Set Slow Operation Threshhold and Sample Rate

The following example sets for a mongod instance:

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

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

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

Disable Profiler and Set Slow Operation Threshhold and Sample Rate

The following example sets for a mongod or mongos instance:

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

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

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