Database Manual / Reference / mongosh Methods / Databases

db.setProfilingLevel() (mongosh method方法)

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. If the profiler is disabled, the method configures how slow operations are logged to the diagnostic log.分析器捕获并记录正在运行的mongod实例上写操作、游标和数据库命令的性能数据。如果禁用分析器,该方法将配置如何将慢速操作记录到诊断日志中。

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 and 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

You can specify a filter on both mongod and mongos instances to control which operations are logged by the profiler. 您可以在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, 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开始,使用profile命令或db.setProfilingLevel()包装器方法对数据库分析器levelslowmssampleRatefilter所做的更改将记录在日志文件中。

Compatibility兼容性

This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Important

This command is not supported in M0 and Flex clusters. For more information, see Unsupported Commands.M0和Flex集群不支持此命令。有关详细信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

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:配置数据库分析器级别。以下分析器级别可用:

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 exceed the slowms threshold or match a specified 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.分析器仅捕获与筛选器匹配的操作。
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.因为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. The following options are available:如果传递整数值作为options参数而不是文档,则该值将分配给slowms。以下选项可用:

slowms

Default: 默认值:100
Type: 类型:integer

The slow operation time threshold, in milliseconds. Operations that run for longer than this threshold are considered slow.慢速操作时间阈值,单位为毫秒。运行时间超过此阈值的操作被认为很慢。

Slow operations are logged based on workingMillis, which is the amount of time that MongoDB spends working on that operation. This means that factors such as waiting for locks and flow control do not affect whether an operation exceeds the slow operation threshold.慢速操作是基于workingMillis记录的,这是MongoDB在该操作上花费的时间。这意味着等待锁和流量控制等因素不会影响操作是否超过慢速操作阈值。

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上没有分析功能。

This argument affects the same setting as the configuration file option slowOpThresholdMs.此参数影响与配置文件选项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.接受介于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上没有分析功能。

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上没有分析功能。

Starting in MongoDB 8.0, you can specify workingMillis as a filter parameter to log operations based on the amount of time MongoDB spends working on that operation.从MongoDB 8.0开始,您可以将workingMillis指定为筛选器参数,以根据MongoDB在该操作上花费的时间来记录操作。

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

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.该方法返回一个包含先前设置值的文档。

Standalone
{
"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
}
Replica Set Member
{
"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" : Long("6755945537557495811")
}
},
"operationTime" : Timestamp(1572991238, 1)
}
mongos Instance
{
"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" : Long("6755946491040235540")
}
}
}

Where:其中

  • was is the previous level setting.
  • slowms is the previous slowms setting.
  • sampleRate is the previous sampleRate setting.
  • filter is the previous filter setting.
  • note is a string explaining the behavior of filter. 是一个解释filter行为的字符串。This field only appears in the output when filter is also present.只有当filter也存在时,此字段才会出现在输出中。

Note

The filter and note fields only appear in the output if they were present in the previous level setting.只有在之前的级别设置中存在filternote字段时,它们才会出现在输出中。

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设置筛选器以确定配置文件操作

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 } }筛选器,它使分析器只记录耗时超过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.setProfilingLevel()

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()