profile

Definition

  • profile

Changed in version 4.0: The command can be run on mongos with profilelevel:

  • 0 to set the slowms and sampleRate for the systemlog; i.e. you cannot enable the profiler on amongos.
  • -1 to read the current settings.

For a mongod instance, the command enables, disables,or configures the Database Profiler. Theprofiler captures and records data on the performance of writeoperations, cursors, and database commands on a runningmongod instance. If the profiler is disabled, thecommand sets the slowms and sampleRate for logging slowoperations to the diagnostic log.

For mongos instance, the command sets the slowmsand sampleRate configuration settings, which configure howoperations get written to the diagnostic log.

The profiler is off by default.

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.

The profile command has the following syntax:

  1. {
  2. profile: <level>,
  3. slowms: <threshold>,
  4. sampleRate: <rate>
  5. }

FieldTypeDescriptionprofileintConfigures the profiler level.The following profiler levels are available:

LevelDescription0The 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, theprofile command cannot be used to set the profilinglevel to a value other than 0 on a mongos instance.slowmsintOptional. Default: 100

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

Note

This argument affects the same setting as the configuration optionslowOpThresholdMs.

sampleRatedoubleOptional. Default: 1.0

The fraction of slow operations that should be profiled or logged.

sampleRate accepts values between 0 and 1, inclusive.

Note

This argument affects the same setting as the configuration optionslowOpSampleRate and does notaffect the slow oplog entry log messages on secondaries(available starting in MongoDB 4.2).

New in version 3.6.

The db.getProfilingStatus() anddb.setProfilingLevel()shell methods provide wrappers around the profilecommand.

Behavior

The profile command obtains a write lock on the affecteddatabase while enabling or disabling the profiler. This is typically ashort operation. The lock blocks other operations until theprofile command has completed.

See also

Database Profiling.