db.setProfilingLevel()

Definition

If the database profiler level is1 or 2 (i.e. the database profiler is enabled), theslowms and thesampleRate affectthe behavior of both the profiler and the diagnostic log.

If the database profiler level is0 (i.e. database profiler is disabled), theslowms and thesampleRate affectonly the diagnostic log.

Although profiling is unavailable on mongos instance,starting in MongoDB 4.0, you can rundb.setProfilingLevel() on mongos to set theslowms andsampleRate for thediagnostic log. That is, for a mongos instance, youmust specify 0 for level.

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

Syntax

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

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

Parameters

ParameterTypeDescription
levelintegerConfigures the database profiler level. The followingprofiler levels are available:
LevelDescription
0The profiler is off and does not collect any data.This is the default profiler level.
1The profiler collects data for operations that take longerthan the value of slowms.
2The profiler collects data for all operations.

Since profiling is not available on mongos,db.setProfilingLevel() cannot be used to set the profilinglevel to a value other than 0 on a mongos instance.

optionsdocument or integerOptional. Accepts an integer or an options document. If an integer value ispassed as the options argument instead of a document, the value isassigned to slowms.The following options are available:

slowmsDefault: 100Type: integerThe slow operation time threshold, in milliseconds. Operationsthat 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 byslowOpSampleRate. Starting in MongoDB4.2, the secondaries of replica sets log all oplog entry messagesthat take longer than the slow operation threshold to apply regardless of the sample rate.At higher logLevel settings, all operations appear inthe diagnostic log regardless of their latency with the followingexception: the logging of slow oplog entry messages by thesecondaries. The secondaries log only the slow oplogentries; increasing the logLevel does not log alloplog entries.For mongod instances, the setting affects both thediagnostic log and, if enabled, the profiler.For mongos instances, the setting affects thediagnostic log only and not the profiler since profiling is notavailable on mongos.NoteThis argument affects the same setting as the configurationfile 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 thediagnostic log and, if enabled, the profiler.For mongos instances, the setting affects thediagnostic log only and not the profiler since profiling is notavailable on mongos.NoteThis argument affects the same setting as the configuration optionslowOpSampleRate.

Returns

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

  • Standalone
  • Replica Set Member
  • mongos Instance
  1. { "was" : 0, "slowms" : 100, "sampleRate" : 1, "ok" : 1 }
  1. {
  2. "was" : 0,
  3. "slowms" : 100,
  4. "sampleRate" : 1,
  5. "ok" : 1,
  6. "$clusterTime" : {
  7. "clusterTime" : Timestamp(1572991238, 1),
  8. "signature" : {
  9. "hash" : BinData(0,"hg6GnlrVhV9MAhwWdeHmHQ4T4qU="),
  10. "keyId" : NumberLong("6755945537557495811")
  11. }
  12. },
  13. "operationTime" : Timestamp(1572991238, 1)
  14. }
  1. {
  2. "was" : 0,
  3. "slowms" : 100,
  4. "sampleRate" : 1,
  5. "ok" : 1,
  6. "operationTime" : Timestamp(1572991499, 2),
  7. "$clusterTime" : {
  8. "clusterTime" : Timestamp(1572991499, 2),
  9. "signature" : {
  10. "hash" : BinData(0,"nhCquIxUw7thlrBudXe3PnsnvP0="),
  11. "keyId" : NumberLong("6755946491040235540")
  12. }
  13. }
  14. }

Where:

  • was is the previouslevelsetting.
  • slowms is the previousslowms setting.
  • sampleRate is the previoussampleRate setting.

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

Behavior

Important

Profiling can impact performance and shares settings with the systemlog. Carefully consider any performance and security implicationsbefore configuring and enabling the profiler on a productiondeployment.

See Profiler Overhead for more information onpotential performance degradation.

Examples

Enable Profiler and Set Slow Operation Threshhold and Sample Rate

The following example sets for a mongod instance:

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

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

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 ormongos instance:

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

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

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