Profiling Amazon DocumentDB Operations
可以使用 Amazon DocumentDB(与 MongoDB 兼容) 中的分析器来记录在您集群上执行的操作的执行时间和详细信息。对于监控集群上速度最慢的操作以帮助您提高单个查询的性能和整体集群性能,分析器非常有用。
默认情况下,分析器功能处于禁用状态。在启用时,分析器将用时超过客户定义的阈值(例如,100 毫秒)的操作记录到 Amazon CloudWatch Logs 中。记录的详细信息包括分析的命令、时间、计划摘要和客户端元数据。在操作记录到 CloudWatch Logs 中之后,您可以使用 CloudWatch Logs Insights 来分析、监控和存档 Amazon DocumentDB 分析数据。Common Queries部分中提供了常见的查询。
在启用时,分析器会使用集群中的其他资源。我们建议您从较高的阈值(例如,500 毫秒)开始,然后逐步降低该值以确定缓慢的操作。对于高吞吐量应用程序,从 50 毫秒阈值开始会导致集群性能问题。分析器在集群级别启用,并对集群中的所有实例和数据库执行分析。Amazon DocumentDB 尽可能将操作记录到 Amazon CloudWatch Logs 中。
虽然启用分析器不会让 Amazon DocumentDB 产生任何额外的费用,但会向您收取使用 CloudWatch Logs 的标准费率。有关 CloudWatch Logs 定价的信息,请参阅 Amazon CloudWatch 定价。
Supported Operations
Amazon DocumentDB 分析器支持以下操作:
aggregatecountdeletedistinctfind(OP_QUERY and command)findAndModifyinsertupdate
Limitations
如果查询的整个结果集能够适合一个批次,并且如果结果集低于16MB(最大的BSON大小),则缓慢查询分析器仅能够发送Profiler日志。结果集大于16MB将自动分成多个批次。
大多数驱动程序或外壳可以设置较小的默认批量大小。您可以指定作为查询的一部分的批量大小。为了捕获缓慢查询日志,我们建议批量大小超过预期结果集大小。如果您不确定结果集大小,或者如果它不同,您还可以将批量设置为大号(例如100K)。
但是,使用更大的批量大小意味着在发送响应给客户端之前,将从数据库检索更多结果。对于某些查询,在获得结果之前可能会产生更长的延迟。如果您不计划使用整个结果集,则可能会花更多I/O来处理查询并丢弃结果。
Enabling the Amazon DocumentDB Profiler
在集群上启用分析器的过程包含三个步骤。确保所有步骤均完成,否则分析日志不会发送到 CloudWatch Logs。分析器在集群级别设置,对集群的所有数据库和实例执行分析。
在集群上启用分析器
- 由于您无法修改默认集群参数组,请确保您有可用的自定义集群参数组。有关更多信息,请参阅 创建 Amazon DocumentDB 集群参数组。)
- 使用可用的自定义集群参数组,修改以下参数。
profiler,profiler_threshold_ms,和profiler_sampling_rate。有关详细信息,请参阅 修改 Amazon DocumentDB 集群参数组. - 创建或修改集群以使用自定义集群参数组,并允许将
profiler日志导出到 CloudWatch Logs。
以下部分说明如何使用 AWS 管理控制台和 AWS Command Line Interface (AWS CLI) 实施这些步骤。
- 开始之前,请先创建一个 Amazon DocumentDB 集群和一个自定义集群参数组(如果您还没有)。有关更多信息,请参阅 创建 Amazon DocumentDB 集群参数组和创建 Amazon DocumentDB集群。
- 使用可用的自定义集群参数组,修改以下参数。有关更多信息,请参阅 修改 Amazon DocumentDB 集群参数组。)
profiler— Enables or disables query profiling. Permitted values areenabledanddisabled. The default value isdisabled. To enable profiling, set the value toenabled.profiler_threshold_ms— Whenprofileris set toenabled, all commands that are taking longer thanprofiler-threshold-msare logged to CloudWatch. Permitted values are[50-INT_MAX]. The default value is100.profiler_sampling_rate— The fraction of slow operations that should be profiled or logged. Permitted values are[0.0-1.0]. The default value is1.0. 3. 修改您的集群以使用自定义集群参数组,并将分析器日志导出设置为发布到 Amazon CloudWatch。
- 在导航窗格中,选择 Clusters (集群) 以将自定义参数组添加到集群。
- 选择要与您的参数组关联的集群名称左边的按钮。选择 Actions (操作),然后选择 Modify (修改) 以修改您的集群。
- 在 Cluster options (集群选项) 下,选择上一步中的自定义参数组以将其添加到集群中。
- 在 Log exports (志日导出) 下,选择 Profiler logs (析器日志) 以发布到 Amazon CloudWatch 中。
- 选择 Continue (继续) 以查看修改摘要。
- 在确认您的更改后,您可以立即应用这些更改,也可以在 Scheduling of modifications (修改计划) 下的下一个维护时段内应用这些更改。
- 选择 Modify cluster (修改集群) 以使用新参数组更新您的集群。
以下过程对集群 sample-cluster 上的所有支持操作启用分析器。
- 在开始之前,请运行以下命令,并查看对于名称中不包含
default且具有docdb3.6作为参数组系列的集群参数组的输出,以确保您拥有可用的自定义集群参数组。如果您没有非默认集群参数组,请参阅创建 Amazon DocumentDB 集群参数组。
```
- aws docdb describe-db-cluster-parameter-groups \
- --query 'DBClusterParameterGroups[*].[DBClusterParameterGroupName,DBParameterGroupFamily]' ```
在以下输出中,仅 sample-parameter-group 满足这两个条件。
```
- [
- [
- "default.docdb3.6",
- "docdb3.6"
- ],
- [
- "sample-parameter-group",
- "docdb3.6"
- ]
- ] ```
- 使用您的自定义集群参数组,修改以下参数。
profiler— Enables or disables query profiling. Permitted values areenabledanddisabled. The default value isdisabled. To enable profiling, set the value toenabled.profiler_threshold_ms— Whenprofileris set toenabled, all commands taking longer thanprofiler -threshold-msare logged to CloudWatch. Permitted values are[0-INT_MAX]. Setting this value to0profiles all supported operations. The default value is100.profiler_sampling_rate— The fraction of slow operations that should be profiled or logged. Permitted values are[0.0-1.0]. The default value is1.0.
```
- aws docdb modify-db-cluster-parameter-group \
- --db-cluster-parameter-group-name sample-parameter-group \
- --parameters ParameterName=profiler,ParameterValue=enabled,ApplyMethod=immediate \
- ParameterName=profiler_threshold_ms,ParameterValue=100,ApplyMethod=immediate \
- ParameterName=profiler_sampling_rate,ParameterValue=0.5,ApplyMethod=immediate ```
- 修改您的 Amazon DocumentDB 集群,使其使用上一步中提到的
sample-parameter-group自定义集群参数组,并将参数--enable-cloudwatch-logs-exports设置为profiler。
以下代码会修改 sample-cluster 集群,使其使用上一步中提到的 sample-parameter-group,并将 profiler 添加到已启用的 CloudWatch Logs 导出。
```
- aws docdb modify-db-cluster \
- --db-cluster-identifier sample-cluster \
- --db-cluster-parameter-group-name sample-parameter-group \
- --cloudwatch-logs-export-configuration '{"EnableLogTypes":["profiler"]}' ```
此操作的输出将类似于下文。
```
- {
- "DBCluster": {
- "AvailabilityZones": [
- "us-east-1c",
- "us-east-1b",
- "us-east-1a"
- ],
- "BackupRetentionPeriod": 1,
- "DBClusterIdentifier": "sample-cluster",
- "DBClusterParameterGroup": "sample-parameter-group",
- "DBSubnetGroup": "default",
- "Status": "available",
- "EarliestRestorableTime": "2020-04-07T02:05:12.479Z",
- "Endpoint": "sample-cluster.node.us-east-1.docdb.amazonaws.com",
- "ReaderEndpoint": "sample-cluster.node.us-east-1.docdb.amazonaws.com",
- "MultiAZ": false,
- "Engine": "docdb",
- "EngineVersion": "3.6.0",
- "LatestRestorableTime": "2020-04-08T22:08:59.317Z",
- "Port": 27017,
- "MasterUsername": "test",
- "PreferredBackupWindow": "02:00-02:30",
- "PreferredMaintenanceWindow": "tue:09:50-tue:10:20",
- "DBClusterMembers": [
- {
- "DBInstanceIdentifier": "sample-instance-1",
- "IsClusterWriter": true,
- "DBClusterParameterGroupStatus": "in-sync",
- "PromotionTier": 1
- },
- {
- "DBInstanceIdentifier": "sample-instance-2",
- "IsClusterWriter": true,
- "DBClusterParameterGroupStatus": "in-sync",
- "PromotionTier": 1
- }
- ],
- "VpcSecurityGroups": [
- {
- "VpcSecurityGroupId": "sg-abcd0123",
- "Status": "active"
- }
- ],
- "HostedZoneId": "ABCDEFGHIJKLM",
- "StorageEncrypted": true,
- "KmsKeyId": "arn:aws:kms:us-east-1:
:key/sample-key", - "DbClusterResourceId": "cluster-ABCDEFGHIJKLMNOPQRSTUVWXYZ",
- "DBClusterArn": "arn:aws:rds:us-east-1:
:cluster:sample-cluster", - "AssociatedRoles": [],
- "ClusterCreateTime": "2020-01-10T22:13:38.261Z",
- "EnabledCloudwatchLogsExports": [
- "profiler"
- ],
- "DeletionProtection": true
- }
- } ```
Disabling the Amazon DocumentDB Profiler
要禁用分析器,您必须禁用 profiler 参数并禁止将 profiler 日志导出到 CloudWatch Logs。
Disabling the Profiler
您可以使用 AWS 管理控制台或 AWS CLI 禁用 profiler 参数,如下所示。
以下过程使用 AWS 管理控制台禁用 Amazon DocumentDB profiler。
- 通过以下网址登录 AWS 管理控制台并打开 Amazon DocumentDB 控制台:https://console.aws.amazon.com/docdb。
- 在导航窗格中,选择参数组。然后选择您要在其上禁用分析器的集群参数组的名称。
- 在生成的 Cluster parameters (集群参数) 页面中,选择
profiler参数左侧的按钮,然后选择 Edit (编辑)。 - 在 Modify Profiler (修改分析器) 对话框中,在列表中选择
disabled。 - 选择 Modify cluster parameter (修改集群参数)。
要使用 AWS CLI 在集群上禁用 profiler,请如下所示修改集群。
1. aws docdb modify-db-cluster-parameter-group \
2. --db-cluster-parameter-group-name sample-parameter-group \
3. --parameters ParameterName=profiler,ParameterValue=disabled,ApplyMethod=immediate
Disabling Profiler Logs Export
您可以使用 AWS 管理控制台或 AWS CLI 禁止将 profiler 日志导出到 CloudWatch Logs,如下所示。
以下过程使用 AWS 管理控制台禁止 Amazon DocumentDB 将日志导出到 CloudWatch。
- 从 https://console.aws.amazon.com/docdb 打开 Amazon DocumentDB 控制台。
- 在导航窗格中,选择集群。选择要禁用导出日志的集群名称左侧的按钮。
- 在 Actions (操作) 菜单上,选择 Modify (修改)。
- 向下滚动到 Log exports (日志导出) 部分并取消选择 Profiler logs (分析器日志)。
- 选择 Continue (继续)。
- 检查更改,然后选择何时将该更改应用到集群:
- Apply during the next scheduled maintenance window (在下一个计划的维护时段内应用)
- Apply immediately (立即应用) 7. 选择修改集群。
以下代码修改集群 sample-cluster 并禁用 CloudWatch 分析器日志。
对于 Linux、macOS 或 Unix:
1. aws docdb modify-db-cluster \
2. --db-cluster-identifier sample-cluster \
3. --cloudwatch-logs-export-configuration '{"DisableLogTypes":["profiler"]}'
对于 Windows:
1. aws docdb modify-db-cluster ^
2. --db-cluster-identifier sample-cluster ^
3. --cloudwatch-logs-export-configuration '{"DisableLogTypes":["profiler"]}'
此操作的输出将类似于下文。
1. {
2. "DBCluster": {
3. "AvailabilityZones": [
4. "us-east-1c",
5. "us-east-1b",
6. "us-east-1a"
7. ],
8. "BackupRetentionPeriod": 1,
9. "DBClusterIdentifier": "sample-cluster",
10. "DBClusterParameterGroup": "sample-parameter-group",
11. "DBSubnetGroup": "default",
12. "Status": "available",
13. "EarliestRestorableTime": "2020-04-08T02:05:17.266Z",
14. "Endpoint": "sample-cluster.node.us-east-1.docdb.amazonaws.com",
15. "ReaderEndpoint": "sample-cluster.node.us-east-1.docdb.amazonaws.com",
16. "MultiAZ": false,
17. "Engine": "docdb",
18. "EngineVersion": "3.6.0",
19. "LatestRestorableTime": "2020-04-09T05:14:44.356Z",
20. "Port": 27017,
21. "MasterUsername": "test",
22. "PreferredBackupWindow": "02:00-02:30",
23. "PreferredMaintenanceWindow": "tue:09:50-tue:10:20",
24. "DBClusterMembers": [
25. {
26. "DBInstanceIdentifier": "sample-instance-1",
27. "IsClusterWriter": true,
28. "DBClusterParameterGroupStatus": "in-sync",
29. "PromotionTier": 1
30. },
31. {
32. "DBInstanceIdentifier": "sample-instance-2",
33. "IsClusterWriter": true,
34. "DBClusterParameterGroupStatus": "in-sync",
35. "PromotionTier": 1
36. }
37. ],
38. "VpcSecurityGroups": [
39. {
40. "VpcSecurityGroupId": "sg-abcd0123",
41. "Status": "active"
42. }
43. ],
44. "HostedZoneId": "ABCDEFGHIJKLM",
45. "StorageEncrypted": true,
46. "KmsKeyId": "arn:aws:kms:us-east-1:<accountID>:key/sample-key",
47. "DbClusterResourceId": "cluster-ABCDEFGHIJKLMNOPQRSTUVWXYZ",
48. "DBClusterArn": "arn:aws:rds:us-east-1:<accountID>:cluster:sample-cluster",
49. "AssociatedRoles": [],
50. "ClusterCreateTime": "2020-01-10T22:13:38.261Z",
51. "DeletionProtection": true
52. }
53. }
Accessing Your Amazon DocumentDB Profiler Logs
执行以下步骤可访问您在 Amazon CloudWatch 上的分析日志。
- 通过以下网址打开 CloudWatch 控制台:https://console.aws.amazon.com/cloudwatch/。
- 确保您与 Amazon DocumentDB 集群位于同一区域。
- 在导航窗格中,选择 Logs (日志)。
- 要查找集群的分析器日志,请在列表中选择
/aws/docdb/yourClusterName/profile。
此时,每个实例名称的下方将显示该实例的分析日志。
Common Queries
以下是您可以用来分析您的已分析命令的常见查询。有关 CloudWatch Logs Insights 的更多信息,请参阅使用 CloudWatch Logs Insights 分析日志数据和示例查询。
Get the 10 Slowest Operations on a Specified Collection
1. filter ns="test.foo" | sort millis desc | limit 10
Get All the Update Operations on a Collection That Took More Than 60 ms
1. filter millis > 60 and op = "update"
Get the 10 Slowest Operations in the Last Month
1. sort millis desc | limit 10
Get All the Queries with a COLLSCAN Plan Summary
1. filter planSummary="COLLSCAN"
