定义
db.setProfilingLevel(level, options)在版本5.0中进行了更改。
对于
mongod实例,该方法可启用、禁用或配置数据库分析器。分析器捕获并记录正在运行的mongod实例上的写入操作、游标和数据库命令性能的数据。如果分析器被禁用,该方法将配置如何将慢操作记录到诊断日志中。注意
使用
db.setProfilingLevel()对分析级别所做的更改不会保留。当服务器重新启动时,它会恢复为0(默认值),或者由operationProfiling.mode设置或--profile命令行选项设置的值。If the database profiler level is
1or2(specifically, the database profiler is enabled), the slowms and sampleRate affect the behavior of both the profiler and thediagnostic log.If the database profiler level is
0(specifically, database profiler is disabled), the slowms and sampleRate, affect only the diagnostic log.使用
mongos实例,该方法设置slowms、sampleRate和filter配置设置,从而配置如何将操作写入诊断日志。您无法在mongos实例上启用数据库分析器,因为mongos没有分析器可以写入的任何集合。对于mongos实例,profile等级必须为0。You can specify a filter on both
mongodandmongosinstances to control which operations are logged by the profiler. When you specify afilterfor the profiler, the slowms, and sampleRate options are not used for profiling and slow-query log lines.db.setProfilingLevel()provides a wrapper around theprofilecommand.从MongoDB5.0 开始,使用
levelslowmssampleRatefilter命令或profile包装器方法对数据库分析器 、db.setProfilingLevel()、 或 所做的更改将记录在log file中。
兼容性
此方法可用于以下环境中托管的部署:
- MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
重要
M0 和 Flex 集群不支持此命令。有关更多信息,请参阅不支持的命令。
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
The db.setProfilingLevel() method has the following form:
db.setProfilingLevel(<level>, <options>)
参数
Parameter | 类型 | 说明 | ||||||
|---|---|---|---|---|---|---|---|---|
整型 | 配置数据库分析器级别。以下分析器级别可用:
Because profiling is not available on | |||||||
文档或整数 | 可选。接受整数或选项文档。如果将整数值作为
|
返回:
该方法返回一个包含先前设置值的文档。
{ "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" : Long("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" : Long("6755946491040235540") } } }
其中:
was是上一个级别设置。slowms是之前的 slowms 设置。sampleRate是之前的 sampleRate 设置。filter是之前的过滤器设置。note是解释filter行为的字符串。只有当filter也存在时,此字段才会出现在输出中。
注意
filter 和 note 字段仅在先前的级别设置中存在时才会出现在输出中。
要查看当前分析级别,请参阅 db.getProfilingStatus()。
行为
示例
启用分析器并设置慢操作阈值和采样率
以下示例集用于 mongod 实例:
the profiling level to
1,the slow operation threshold slowms to
20milliseconds, andthe sampleRate to
0.42.
db.setProfilingLevel(1, { slowms: 20, sampleRate: 0.42 })
该操作返回一个包含先前设置值的文档。
要查看当前分析级别,请参阅 db.getProfilingStatus()。
禁用性能分析器并设置慢操作阈值和采样率
下面的示例为 mongod 或 mongos 实例进行了设置:
the profiling level to
0,the slow operation threshold slowms to
20milliseconds, andthe sampleRate to
0.42.
db.setProfilingLevel(0, { slowms: 20, sampleRate: 0.42 })
该操作返回一个包含先前设置值的文档。
要查看当前分析级别,请参阅 db.getProfilingStatus()。
设置过滤器以确定要进行分析的操作
以下示例集用于 mongod 实例:
the profiling level to
1,过滤
{ op: "query", millis: { $gt: 2000 } },这会导致分析器仅记录耗时超过query2秒的 操作。
db.setProfilingLevel( 1, { filter: { op: "query", millis: { $gt: 2000 } } } )
该操作返回一个包含先前设置值的文档。
要查看当前分析级别,请参阅 db.getProfilingStatus()。
设置过滤器而不启用分析器
You can set a filter without enabling the database profiler. The following example uses a filter with a profiling level of 0.
db.setProfilingLevel( 0, { filter: { op: "query", millis: { $gt: 2000 } } } )
由于分析级别为 0,分析器已禁用,因此不会将匹配的操作记录到分析器集合。过滤器仍然应用于诊断日志。
该操作返回一个包含先前设置值的文档。
要查看当前分析级别,请参阅 db.getProfilingStatus()。
取消设置筛选器
要清除分析筛选器,请运行带有 filter: "unset" 选项的 db.setProfilingLevel()。
db.setProfilingLevel( 1, { filter: "unset" } )
该操作返回一个包含先前设置值的文档。
要查看当前分析级别,请参阅 db.getProfilingStatus()。