Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅
中国的 Amazon Web Services 服务入门
(PDF)。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
分析 Amazon DocumentDB 操作
可以使用 Amazon DocumentDB(与 MongoDB 兼容)中的分析器来记录在您集群上执行的操作的执行时间和详细信息。对于监控集群上速度最慢的操作以帮助您提高单个查询的性能和整体集群性能,分析器非常有用。
默认情况下,分析器功能处于禁用状态。启用后,Profiler 会将花费超过客户定义的阈值(例如 100 毫秒)的操作记录到 Ama CloudWatch zon Logs 中。记录的详细信息包括分析的命令、时间、计划摘要和客户端元数据。将操作记录到 CloudWatch 日志后,您可以使用 CloudWatch Logs Insights 来分析、监控和存档您的 Amazon DocumentDB 分析数据。常见查询 部分中提供了常见的查询。
在启用时,分析器会使用集群中的其他资源。我们建议您从较高的阈值(例如,500 毫秒)开始,然后逐步降低该值以确定缓慢的操作。对于高吞吐量应用程序,从 50 毫秒阈值开始会导致集群性能问题。分析器在集群级别启用,并对集群中的所有实例和数据库执行分析。Amazon DocumentDB 会尽力将操作记录到亚马逊 CloudWatch 日志。
尽管 Amazon DocumentDB 不会为启用分析器收取任何额外费用,但您需要按标准费率支付日志使用费。 CloudWatch 有关 CloudWatch 日志定价的信息,请参阅 Amazon CloudWatch 定价。
支持的操作
Amazon DocumentDB 分析器支持以下操作:
aggregate
count
delete
distinct
find
(OP_QUERY 和命令)
findAndModify
insert
update
限制
仅当查询的整个结果集能够容纳在一个批处理中,并且结果集小于 16MB(最大 BSON 大小)时,慢速查询分析器才能够生成分析器日志。大于 16MB 的结果集会自动拆分为多个批处理。
大多数驱动程序或 Shell 可能会设置一个较小的默认批处理大小。您可以在查询中指定批处理大小。为了捕获慢速查询日志,我们建议设置一个超过您预期结果集大小的批处理大小。如果不确定结果集大小,或者结果集大小不同,也可以将批处理大小设置为较大的数字(例如,100k)。
但是,使用较大的批大小意味着在将响应发送到客户端之前,必须从数据库中检索更多结果。对于某些查询,这可能会在获得结果之前造成更长的延迟。如果您不打算使用整个结果集,则可能会花费更多的 I/O 来处理查询并丢弃结果。
启用 Amazon DocumentDB 分析器
在集群上启用分析器的过程包含三个步骤。确保所有步骤都已完成,否则分析日志将不会发送到 CloudWatch 日志。分析器在集群级别设置,对集群的所有数据库和实例执行分析。
以下各节介绍如何使用 Amazon Web Services Management Console 和 Amazon Command Line Interface (Amazon CLI) 实现这些步骤。
- Using the Amazon Web Services Management Console
-
-
开始之前,请先创建一个 Amazon DocumentDB 集群和一个自定义集群参数组(如果您还没有)。有关更多信息,请参阅创建 Amazon DocumentDB 集群参数组 和创建 Amazon DocumentDB 集群。
-
使用可用的自定义集群参数组,修改以下参数。有关更多信息,请参阅 修改 Amazon DocumentDB 集群参数组。
-
profiler
— 启用或禁用查询分析。允许的值为 enabled
和 disabled
。默认值为 disabled
。要启用分析,请将值设置为 enabled
。
-
profiler_threshold_ms
— 如果设置profiler
为enabled
,则所有花费时间超过其时间的命令
profiler_threshold_ms
都将被记录到 CloudWatch。允许的值为 [50-INT_MAX]
。默认值为 100
。
-
profiler_sampling_rate
— 应该分析或记录的缓慢操作的部分。允许的值为 [0.0-1.0]
。默认值为 1.0
。
-
修改您的集群以使用自定义集群参数组,并将分析器日志导出设置为发布到 Amazon CloudWatch。
-
在导航窗格中,选择 Clusters (集群) 以将自定义参数组添加到集群。
-
选择要与您的参数组关联的集群名称左边的按钮。选择 Actions (操作),然后选择 Modify (修改) 以修改您的集群。
-
在 Cluster options (集群选项) 下,选择上一步中的自定义参数组以将其添加到集群中。
-
在 “日志导出” 下,选择要发布到 Amazon CloudWatch 的 Profiler 日志。
-
选择 Continue (继续) 以查看修改摘要。
-
在确认您的更改后,您可以立即应用这些更改,也可以在 Scheduling of modifications (修改计划) 下的下一个维护时段内应用这些更改。
-
选择 Modify cluster (修改集群) 以使用新参数组更新您的集群。
- Using the Amazon CLI
-
以下过程对集群 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
— 启用或禁用查询分析。允许的值为 enabled
和 disabled
。默认值为 disabled
。要启用分析,请将值设置为 enabled
。
-
profiler_threshold_ms
— 如果设置profiler
为enabled
,则所有命令花费的时间profiler_threshold_ms
都超过了记录到的时间 CloudWatch。允许的值为 [50-INT_MAX]
。默认值为 100
。
-
profiler_sampling_rate
— 应该分析或记录的缓慢操作的部分。允许的值为 [0.0-1.0]
。默认值为 1.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 日志导出中。
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:<accountID>:key/sample-key",
"DbClusterResourceId": "cluster-ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"DBClusterArn": "arn:aws:rds:us-east-1:<accountID>:cluster:sample-cluster",
"AssociatedRoles": [],
"ClusterCreateTime": "2020-01-10T22:13:38.261Z",
"EnabledCloudwatchLogsExports": [
"profiler"
],
"DeletionProtection": true
}
}
禁用 Amazon DocumentDB 分析器
要禁用探查器,您可以禁用profiler
参数和将日志导出到profiler
日 CloudWatch 志。
禁用分析器
您可以使用 Amazon Web Services Management Console 或禁用该profiler
参数 Amazon CLI,如下所示。
- Using the Amazon Web Services Management Console
-
以下过程使用来禁用 Amazon Documen profiler
tDB。 Amazon Web Services Management Console
- Using the Amazon CLI
-
要使用 Amazon CLI在集群上禁用 profiler
,请如下所示修改集群。
aws docdb modify-db-cluster-parameter-group \
--db-cluster-parameter-group-name sample-parameter-group \
--parameters ParameterName=profiler,ParameterValue=disabled,ApplyMethod=immediate
禁用分析器日志导出
您可以使用 Amazon Web Services Management Console 或禁用将profiler
CloudWatch 日志导出到日志 Amazon CLI,如下所示。
- Using the Amazon Web Services Management Console
-
以下过程使用禁用 Amazon DocumentDB Amazon Web Services Management Console 将日志导出到。 CloudWatch
- Using the Amazon CLI
-
以下代码修改集群sample-cluster
并禁用 CloudWatch 探查器日志。
对于 Linux、macOS 或 Unix:
aws docdb modify-db-cluster \
--db-cluster-identifier sample-cluster \
--cloudwatch-logs-export-configuration '{"DisableLogTypes":["profiler"]}'
对于 Windows:
aws docdb modify-db-cluster ^
--db-cluster-identifier sample-cluster ^
--cloudwatch-logs-export-configuration '{"DisableLogTypes":["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-08T02:05:17.266Z",
"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-09T05:14:44.356Z",
"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:<accountID>:key/sample-key",
"DbClusterResourceId": "cluster-ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"DBClusterArn": "arn:aws:rds:us-east-1:<accountID>:cluster:sample-cluster",
"AssociatedRoles": [],
"ClusterCreateTime": "2020-01-10T22:13:38.261Z",
"DeletionProtection": true
}
}
访问您的 Amazon DocumentDB 分析器日志
按照以下步骤访问您在Amazon上的个人资料日志 CloudWatch。
常见查询
以下是您可以用来分析您的已分析命令的常见查询。有关 Lo CloudWatch gs Insights 的更多信息,请参阅使用 CloudWatch 日志见解和示例查询分析日志数据。
获取指定集合上最慢的 10 个操作
filter ns="test.foo" | sort millis desc | limit 10
获取集合上用时超过 60 毫秒的所有更新操作
filter millis > 60 and op = "update"
获取上个月最慢的 10 个操作
sort millis desc | limit 10
获取具有 COLLSCAN 计划摘要的所有查询
filter planSummary="COLLSCAN"