Docs HomeMongoDB Manual

db.setProfilingLevel()

Definition定义

db.setProfilingLevel(level, options)

Changed in version 5.05.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.如果探查器被禁用,则该方法配置将慢速操作记录到诊断日志的方式。

Note

Changes made to the profiling level with db.setProfilingLevel() do not persist. 使用db.setProfilingLevel()对分析级别所做的更改不会持久存在。When the server restarts, it reverts to 0 (the default), or the value set by either the operationProfiling.mode setting or the --profile command-line option.当服务器重新启动时,它将恢复为0(默认值),或由operationProfiling.mode设置或--profile命令行选项设置的值。

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仅影响诊断日志。

With mongos instances, the method sets the slowms, sampleRate and filter configuration settings, which configure how operations get written to the diagnostic log. 对于mongos实例,该方法设置slowmssampleRatefilter配置设置,这些设置配置如何将操作写入诊断日志。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实例上指定一个filter,以控制探查器记录哪些操作。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, and 4.2.12), 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开始使用),使用profile命令或db.setProfilingLevel()包装方法对数据库探查器levelslowmssampleRatefilter所做的更改将记录在日志文件中。

Syntax语法

The db.setProfilingLevel() method has the following form:db.setProfilingLevel()方法的形式如下:

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

Parameters参数

Parameter参数Type类型Description描述
levelintegerConfigures the database profiler level. 配置数据库探查器级别。The following profiler levels are available:以下探查器级别可用:
Level级别Description描述
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 or that match a filter.探查器为耗时超过slowms值或与筛选器匹配的操作集合数据。
When a filter is set:设置筛选器时:
  • The slowms and sampleRate options are not used for profiling.slowmssampleRate选项不用于分析。
  • The profiler only captures operations that match the filter.探查器仅捕获与筛选器匹配的操作。
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 integerOptional. 可选的。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:如果传递一个整数值作为options参数而不是文档,则会将该值分配给slowms。以下选项可用:
slowmsDefault: 默认值: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设置下,所有操作都会显示在诊断日志中,无论其延迟如何,但以下情况除外:由辅助设备记录慢速操作日志条目消息。The secondaries log only the slow oplog entries; increasing the logLevel does not log all oplog entries.辅助设备只记录慢速操作日志条目;增加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相同的设置。
sampleRateDefault: 默认值:1.0
Type: 类型:double
The fraction of slow operations that should be profiled or logged. 应该对慢速操作进行分析或记录的部分。
sampleRate accepts values between 0 and 1, inclusive.接受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相同的设置。
filterType: 类型: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. 设置分析filter时,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.was是以前的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是以前的filter设置。(MongoDB 4.4.2新增
  • note is a string explaining the behavior of filter. note是一个解释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.filternote字段只有在以前的level设置中存在时才会出现在输出中。

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

Behavior行为

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.有关潜在性能下降的更多信息,请参阅Profiler开销

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 operation 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 operation 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 1,配置文件级别1
  • 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 } }filter,这会导致探查器只记录耗时超过2秒的query操作。
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, see db.getProfilingStatus().要查看当前的评测级别,请参阅db.getProfilingStatus()

Unset a Filter取消设置筛选器

To clear a profile filter, run db.setProfilingLevel() with the filter: "unset" option.要清除配置文件筛选器,请使用filter: "unset"选项运行db.setProfileLevel()

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

The operation returns a document with the previous values for the settings.该操作返回一个具有以前设置值的文档。

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