db.setProfilingLevel()
On this page本页内容
Definition定义Syntax语法Parameters参数Returns返回Behavior行为Examples实例Enable Profiler and Set Slow Operation Threshold and Sample Rate启用探查器并设置慢速操作阈值和采样率Disable Profiler and Set Slow Operation Threshold and Sample Rate禁用探查器并设置慢速操作阈值和采样率Set a Filter to Determine Profiled Operations设置筛选器以确定分析的操作Unset a Filter取消设置筛选器
Definition定义
db.setProfilingLevel(level, options)Changed in version 5.05.0版更改.For a对于mongodinstance, 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探查器捕获并记录有关正在运行的mongodinstance.mongod实例上的写操作、游标和数据库命令的性能的数据。If the profiler is disabled, the method configures how slow operations are logged to the diagnostic log.如果探查器被禁用,则该方法配置将慢速操作记录到诊断日志的方式。NoteChanges 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 theoperationProfiling.modesetting or the--profilecommand-line option.0(默认值),或由operationProfiling.mode设置或--profile命令行选项设置的值。If the database profiler level is如果数据库探查器级别为1or2(specifically, the database profiler is enabled), the slowms, sampleRate affect the behavior of both the profiler and thediagnostic log.1或2(特别是启用了数据库探查器),则slowms、sampleRate会影响探查器和诊断日志的行为。If the database profiler level is如果数据库探查器级别为0(specifically, database profiler is disabled), the slowms and sampleRate, affect only the diagnostic log.0(特别是禁用了数据库探查器),则slowms和sampleRate仅影响诊断日志。With对于mongosinstances, the method sets theslowms,sampleRateandfilterconfiguration settings, which configure how operations get written to the diagnostic log.mongos实例,该方法设置slowms、sampleRate和filter配置设置,这些设置配置如何将操作写入诊断日志。You cannot enable the Database Profiler on a您无法在mongosinstance becausemongosdoes not have any collections that the profiler can write to.mongos实例上启用数据库探查器,因为mongos没有任何探查器可以写入的集合。Theprofilelevel must be0for amongosinstance.mongos实例的profile级别必须为0。Starting in MongoDB 4.4.2, you can specify a filter on both从MongoDB 4.4.2开始,您可以在mongodandmongosinstances to control which operations are logged by the profiler.mongod和mongos实例上指定一个filter,以控制探查器记录哪些操作。When you specify a为探查器指定filterfor the profiler, the slowms, and sampleRate options are not used for profiling and slow-query log lines.filter时,slowms和sampleRate选项不用于探查和慢速查询日志行。db.setProfilingLevel()provides a wrapper around the提供了profilecommand.profile命令的包装器。Starting in MongoDB 5.0 (also available starting in 4.4.2, and 4.2.12), changes made to the database profiler从MongoDB 5.0开始(也可从4.4.2和4.2.12开始使用),使用level,slowms,sampleRate, orfilterusing theprofilecommand ordb.setProfilingLevel()wrapper method are recorded in thelog file.profile命令或db.setProfilingLevel()包装方法对数据库探查器level、slowms、sampleRate或filter所做的更改将记录在日志文件中。
Syntax语法
The db.setProfilingLevel() method has the following form:db.setProfilingLevel()方法的形式如下:
db.setProfilingLevel(<level>, <options>)
Parameters参数
level | integer |
| ||||||||
options | document or integer | options argument instead of a document, the value is assigned to slowms. The following options are available:options参数而不是文档,则会将该值分配给slowms。以下选项可用:
|
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:其中:
wasis the previous level setting.was是以前的level设置。slowmsis the previous slowms setting.是以前的slowms设置。sampleRateis the previous sampleRate setting.是以前的sampleRate设置。filteris the previous filter setting. (New in MongoDB 4.4.2)filter是以前的filter设置。(MongoDB 4.4.2新增)noteis a string explaining the behavior offilter.note是一个解释filter行为的字符串。This field only appears in the output when只有当filteris also present. (New in MongoDB 4.4.2)filter也存在时,此字段才会出现在输出中。(MongoDB 4.4.2新增)
To view the current profiling level, see 要查看当前的评测级别,请参阅db.getProfilingStatus().db.getProfilingStatus()。
Behavior行为
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实例的示例集:
the profiling level to配置文件级别为1,1,the slow operation threshold slowms to慢操作阈值slowms为20milliseconds, and20毫秒,并且the sampleRate to0.42.sampleRate为0.42。
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:mongod或mongos实例的示例集:
the profiling level to将配置文件级别设置为0,0,the slow operation threshold slowms to慢操作阈值20milliseconds, andslowms为20毫秒,并且the sampleRate to0.42.sampleRate设置为0.42。
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 recordqueryoperations 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()。