db.setProfilingLevel()

On this page本页内容

Definition定义

db.setProfilingLevel(level, options)

Changed in version 5.0.在版本5.0中更改

For a mongod instance, the method enables, disables, or configures the Database Profiler. 对于mongod实例,该方法启用、禁用或配置数据库探查器The profiler captures and records data on the performance of write operations, cursors, and database commands on a running mongod instance. 探查器在运行的mongod实例上捕获并记录有关写操作、游标和数据库命令性能的数据。If the profiler is disabled, the method configures how slow operations are logged to the diagnostic log.如果禁用探查器,该方法将配置将操作记录到诊断日志中的速度。

If the database profiler level is 1 or 2 (specifically, the database profiler is enabled), the slowms, sampleRate affect the behavior of both the profiler and the diagnostic log.如果数据库探查器级别12(具体而言,数据库探查程序已启用),则slowmssampleRate会影响探查器和诊断日志的行为。

If the database profiler level is 0 (specifically, database profiler is disabled), the slowms and sampleRate, affect only the diagnostic log.如果数据库探查器级别0(具体而言,数据库探查程序被禁用),则slowmssampleRate仅影响诊断日志。

Starting in MongoDB 4.0, for a mongos instance, the method sets the slowms, sampleRate and filter configuration settings, which configure how operations get written to the diagnostic log. 从MongoDB 4.0开始,对于mongos实例,该方法设置slowmssampleRate和`配置设置,这些设置配置如何将操作写入诊断日志。You cannot enable the Database Profiler on a mongos instance because mongos does not have any collections that the profiler can write to. 无法在mongos实例上启用数据库探查器,因为mongos没有探查器可以写入的任何集合。The profile level must be 0 for a mongos instance.mongos实例的profile级别必须为0。

Starting in MongoDB 4.4.2, you can specify a filter on both mongod and mongos instances to control which operations are logged by the profiler. 从MongoDB 4.4.2开始,您可以在mongodmongos实例上指定一个筛选器,以控制探查器记录哪些操作。When you specify a filter for the profiler, the slowms, and sampleRate options are not used for profiling and slow-query log lines.为探查器指定filter时,slowmssampleRate选项不用于探查和慢速查询日志行。

db.setProfilingLevel() provides a wrapper around the profile command.提供profile命令的包装。

Starting in MongoDB 5.0 (also available starting in 4.4.2, 4.2.12, and 4.0.22), changes made to the database profiler level, slowms, sampleRate, or filter using the profile command or db.setProfilingLevel() wrapper method are recorded in the log file.从MongoDB 5.0(也可从4.4.2、4.2.12和4.0.22开始使用)开始,使用profile命令或db.setProfilingLevel()包装方法对数据库探查器levelslowmssampleRatefilter所做的更改将记录在日志文件中。

Syntax语法

The db.setProfilingLevel() method has the following form:db.setProfilingLevel()方法具有以下形式:

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

Parameters参数

Parameter参数Type类型Description描述
levelinteger

Configures the database profiler level. 配置数据库探查器级别。The following profiler 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 longer than the value of slowms. 探查器集合操作所用时间超过slowms值的数据。
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.由于mongos上无法进行分析,因此无法使用db.setProfilingLevel()mongos实例上的分析级别设置为0以外的值。

optionsdocument 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. 如果将整数值作为options参数而不是文档传递,则将该值分配给slowmsThe following options are available:以下选项可用:

slowms
Default: 100默认值: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.logLevel设置为0时,MongoDB以slowOpSampleRate确定的速率将慢速操作记录到诊断日志中。

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. 在较高的logLevel设置下,所有操作都会显示在诊断日志中,而不管其延迟如何,但以下例外情况除外:辅助设备记录慢速oplog条目消息The secondaries log only the slow oplog entries; increasing the logLevel does not log all oplog entries.辅助设备只记录慢速oplog条目;增加logLevel不会记录所有oplog条目。

For mongod instances, the setting affects both the diagnostic log and, if enabled, the profiler.对于mongod实例,该设置会影响诊断日志和探查器(如果启用)。

For mongos instances, the setting affects the diagnostic log only and not the profiler because profiling is not available on mongos.对于mongos实例,该设置仅影响诊断日志,而不影响探查器,因为在mongos上无法进行探查。

Note注意

This argument affects the same setting as the configuration file option slowOpThresholdMs.此参数影响与配置文件选项slowOpThresholdMs相同的设置。

sampleRate
Default: 1.0默认值:1.0
Type: 类型:double

The fraction of slow operations that should be profiled or logged. 应分析或记录的慢速操作的分数。sampleRate accepts values between 0 and 1, inclusive.sampleRate接受介于0和1之间的值(包括0和1)。

For mongod instances, the setting affects both the diagnostic log and, if enabled, the profiler.对于mongod实例,该设置会影响诊断日志和探查器(如果启用)。

For mongos instances, the setting affects the diagnostic log only and not the profiler because profiling is not available on mongos.对于mongos实例,该设置仅影响诊断日志,而不影响探查器,因为在mongos上无法进行探查。

Note注意

This argument affects the same setting as the configuration option slowOpSampleRate.此参数影响与配置选项slowOpSampleRate相同的设置。

filter

Type: object

A 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.对于mongod实例,该设置会影响诊断日志和探查器(如果启用)。

For mongos instances, the setting affects the diagnostic log only and not the profiler because profiling is not available on mongos.对于mongos实例,该设置仅影响诊断日志,而不影响探查器,因为在mongos上无法进行探查。

For an example of a filter used to control logged operations, see Set a Filter to Determine Profiled Operations.有关用于控制已记录操作的筛选器的示例,请参阅设置筛选器以确定已分析操作

New in version 4.4.2.在版本4.4.2中新增

Note注意

When a profiling filter is set, the slowms and sampleRate options do not affect the diagnostic log or the profiler.设置分析筛选器时,slowmssampleRate选项不会影响诊断日志或分析器。

Returns返回值

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:

  • was is the previous level setting.先前的level设置。
  • slowms is the previous slowms setting.先前的slowms设置。
  • sampleRate is the previous sampleRate setting.先前的sampleRate设置。
  • filter is the previous filter setting. (New in MongoDB 4.4.2)先前的filter设置。(MongoDB 4.4.2新增
  • note is a string explaining the behavior of filter. 是解释filter行为的字符串。This field only appears in the output when filter is also present. (New in MongoDB 4.4.2)仅当filter也存在时,此字段才会出现在输出中。(MongoDB 4.4.2新增
Note注意

The filter and note fields only appear in the output if they were present in the previous level setting.filter和注释字段仅在先前级别设置中出现时才会显示在输出中。

To view the current profiling level, see db.getProfilingStatus().要查看当前的分析级别,请参阅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 Threshold and Sample Rate启用探查器并设置慢速操作阈值和采样率

The following example sets for a mongod instance:以下示例为mongod实例设置:

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().要查看当前的分析级别,请参阅db.getProfilingStatus()

Disable Profiler and Set Slow Operation Threshold and Sample Rate禁用探查器并设置慢速操作阈值和采样率

The following example sets for a mongod or mongos instance:以下示例为mongodmongos实例设置:

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().要查看当前的分析级别,请参阅db.getProfilingStatus()

Set a Filter to Determine Profiled Operations设置筛选器以确定分析的操作

New in version 4.4.2.在版本4.4.2中新增

The following example sets for a mongod instance:以下示例为mongod实例设置:

  • the profiling level to 2,
  • a filter of { op: "query", millis: { $gt: 2000 } }, which causes the profiler to only record query operations that took longer than 2 seconds.{ op: "query", millis: { $gt: 2000 } }筛选器,这将导致探查器只记录花费超过2秒的query操作。
db.setProfilingLevel( 2, { filter: { op: "query", millis: { $gt: 2000 } } } )

The method returns a document with the previous values for the settings.该方法返回一个文档,其中包含设置的先前值。

To view the current profiling level, see db.getProfilingStatus().要查看当前的分析级别,请参阅db.getProfilingStatus()

←  db.setLogLevel()db.shutdownServer() →