

# CloudWatch Contributor Insights for DynamoDB 入门
<a name="contributorinsights_tutorial"></a>

本节介绍如何使用 Amazon DynamoDB 控制台或 AWS Command Line Interface（AWS CLI），以不同模式启用和使用 Amazon CloudWatch Contributor Insights 来满足您的监控需求。

在以下示例中，您将使用 [DynamoDB 入门](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStartedDynamoDB.html)教程中定义的 DynamoDB 表。

**Topics**
+ [选择 Contributor Insights 模式](#contributorinsights_tutorial.modes)
+ [使用 Contributor Insights（控制台）](#usecontributorinsights_console)
+ [使用 Contributor Insights (AWS CLI)](#usecontributorinsights_cli)

## 选择 Contributor Insights 模式
<a name="contributorinsights_tutorial.modes"></a>

在启用 Contributor Insights 之前，您应该对可用的两种模式有所了解。查看模式对比，选择与您的具体需求更相符的选项。


| 方面 | 访问的键和受限的键模式 | 受限的键模式 | 
| --- | --- | --- | 
| 监控 | 所有请求（成功且受限制） | 仅受限的请求 | 
| 图表： | 最常访问的项目 \$1 最受限的项目 | 仅最受限的项目 | 
| 适用于 | 针对性的分析和优化 | 节流监控 | 
| 使用场景 | 您需要全面监测访问模式。您在进行短期分析或调试。 | 您最关心的是识别和解决节流问题。您想保持启用 Contributor Insights 来获取实时节流警报。 | 

## 使用 Contributor Insights（控制台）
<a name="usecontributorinsights_console"></a>

控制台提供了直观的方法来启用 Contributor Insights，而且您可以根据监控需求选择相应的模式。

**在控制台中使用 Contributor Insights**

1. 登录 AWS 管理控制台，并打开 DynamoDB 控制台：[https://console.aws.amazon.com/dynamodb/](https://console.aws.amazon.com/dynamodb/)。

1. 在控制台左侧的导航窗格中，选择**表**。

1. 选择 `Music` 表。

1. 选择**监控**选项卡。

1. 选择**开启 CloudWatch Contributor Insights**。  
![\[显示监控选项卡和按钮的控制台屏幕截图。\]](http://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/images/CI_ChooseAndManageNew.PNG)

1. 在**管理 CloudWatch Contributor Insights 设置**对话框中，为 `Music` 基表和 `AlbumTitle-index` 全局二级索引切换到**开启**。

1. 对于这两者，将**仅限受限的键模式**开关保留在关闭位置，然后选择**保存更改**。  
![\[显示 Contributor Insights 状态列表选项的控制台屏幕截图。\]](http://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/images/CI_Enable.png)

   这会为表和 GSI 均启用默认的*访问的键和受限的键*模式，从而监控访问的项目和受限的项目。将**仅限受限的键模式**开关切换到启用*受限的键*模式的位置。

   如果操作失败，请参阅 *Amazon DynamoDB API 参考*中的 [DescribeContributorInsights FailureException](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DescribeContributorInsights.html#DDB-DescribeContributorInsights-response-FailureException) 了解可能的原因。

1. 现在，对于 `Music` 表，可在**监控**选项卡上查看 CloudWatch Contributor Insights 图表。由于您启用了*访问的键和受限的键*模式，因此您可以看到访问的项目和受限的项目的图表。  
![\[显示 Contributor Insights 选项卡和 music 表多个图表的控制台屏幕截图。\]](http://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/images/CI_Graphs.png)

### 切换模式
<a name="usecontributorinsights_console.switching-modes"></a>

您可以随时切换模式，而无需禁用 Contributor Insights。

**切换 Contributor Insights 模式**

1. 在表的**监控**选项卡上，选择**管理 CloudWatch Contributor Insights**。

1. 在**管理 Contributor Insights 设置**对话框中，对于每个基表或 GSI：
   + 开启或关闭**仅受限的键模式**，来启用*受限的键*模式或者返回默认的*访问的键和受限的键*模式。
   + 将**开启**开关关闭，对表或 GSI 禁用 CloudWatch Contributor Insight。

1. 选择**保存更改**。

   完成后，图表将反映新模式。

### 创建 CloudWatch 警报
<a name="usecontributorinsights_console_alarms"></a>

按照以下步骤创建 CloudWatch 警报，并在任何分区键使用了超过 5 万个 [ConsumedThroughputUnits](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/contributorinsights_HowItWorks.html#contributorinsights_HowItWorks.Graphs.most-accessed) 或出现节流时收到通知。

1. 登录 AWS 管理控制台，并打开 CloudWatch 控制台：[https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/)。

1. 在控制台左侧的导航窗格中，选择 **Contributor Insights**。

1. 根据您的模式和要监控的对象，选择相应的规则：
   + 要监控访问的项目（仅适用于访问的键和受限的键模式）：选择 **DynamoDBContributorInsights-PKC-Music**
   + 要监控受限的项目（两种模式）：选择 **DynamoDBContributorInsights-PKT-Music**

1. 选择**操作**下拉菜单。

1. 选择**在指标中查看**。

1. 选择**最大贡献者值**。
**注意**  
仅 `Max Contributor Value` 和 `Maximum` 返回有用的统计信息。此列表中的其他统计信息不返回有意义的值。  
![\[显示 Contributor Insights 选项卡和按钮的控制台屏幕截图。\]](http://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/images/CI_AlarmsViewinMetrics.png)

1. 在**操作**列选择**创建告警**。  
![\[显示 Contributor Insights 状态列表选项的控制台屏幕截图。\]](http://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/images/CI_AlarmsSetAlarm.png)

1. 输入适当的阈值，然后选择**下一步**：
   + 对于访问的项目（PKC 规则）：为 `ConsumedThroughputUnits` 输入 50000
   + 对于受限的项目（PKT 规则）：为 `ThrottleCount` 输入 1，这样就能在出现任何节流情况时收到警报  
![\[显示 Contributor Insights 选项卡和按钮的控制台屏幕截图。\]](http://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/images/CI_AlarmsSetAlarmThreashold.png)

1.  有关如何为警报配置通知的详细信息，请参阅[使用 Amazon CloudWatch 警报](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html)。

## 使用 Contributor Insights (AWS CLI)
<a name="usecontributorinsights_cli"></a>

AWS CLI 提供对 Contributor Insights 的程序化访问，同时完全支持这两种模式。您可以在启用 Contributor Insights 时指定模式，也可以在以后切换模式。

### 默认模式下的基本操作
<a name="usecontributorinsights_cli.basic"></a>

**使用 Contributor Insights 的默认设置**

1. 在 `Music` 基表上启用 CloudWatch Contributor Insights for DynamoDB，并使用*访问的键和受限的键*模式。由于 `ACCESSED_AND_THROTTLED_KEYS` 是默认模式，因此可以省略 `--contributor-insights-mode=ACCESSED_AND_THROTTLED_KEYS` 参数。

   ```
   aws dynamodb update-contributor-insights \
                       --table-name Music \
                       --contributor-insights-action=ENABLE
   ```

1. 在 `AlbumTitle-index` 全局二级索引启用 Contributor Insights for DynamoDB。

   ```
   aws dynamodb update-contributor-insights \
                       --table-name Music \
                       --index-name AlbumTitle-index \
                       --contributor-insights-action=ENABLE
   ```

1. 获取 `Music` 表及其所有索引的状态与规则。

   ```
   aws dynamodb describe-contributor-insights 
                       --table-name Music
   ```

   响应将包括 `ContributorInsightsMode` 字段，显示 `ACCESSED_AND_THROTTLED_KEYS`。

1. 列出 `Music` 表及其所有索引的状态。

   ```
   aws dynamodb list-contributor-insights --table-name Music
   ```

### 启用受限的键模式
<a name="usecontributorinsights_cli.throttled-mode"></a>

**以受限的键模式启用 Contributor Insights**

1. 在 `Music` 基表上启用 CloudWatch Contributor Insights for DynamoDB，并使用*受限的键*模式。

   ```
   aws dynamodb update-contributor-insights \
       --table-name Music \
       --contributor-insights-action=ENABLE \
       --contributor-insights-mode=THROTTLED_KEYS
   ```

1. 为 `AlbumTitle-index` 全局二级索引启用 Contributor Insights 并使用*受限的键*模式。

   ```
   aws dynamodb update-contributor-insights \
       --table-name Music \
       --index-name AlbumTitle-index \
       --contributor-insights-action=ENABLE \
       --contributor-insights-mode=THROTTLED_KEYS
   ```

1. 说明 Contributor Insights 配置来验证模式。

   ```
   aws dynamodb describe-contributor-insights --table-name Music
   ```

   与默认模式相比，响应显示的 `ContributorInsightsMode` 为 `THROTTLED_KEYS`，且规则更少。

### 切换模式
<a name="usecontributorinsights_cli.switching-modes"></a>

**切换 Contributor Insights 模式**

1. 从*受限的键*模式切换到*访问的键和受限的键*模式。

   ```
   aws dynamodb update-contributor-insights \
       --table-name Music \
       --contributor-insights-action=ENABLE \
       --contributor-insights-mode=ACCESSED_AND_THROTTLED_KEYS
   ```

1. 从*访问的键和受限的键*模式切换到*受限的键*模式。

   ```
   aws dynamodb update-contributor-insights \
       --table-name Music \
       --contributor-insights-action=ENABLE \
       --contributor-insights-mode=THROTTLED_KEYS
   ```

1. 检查切换期间的状态。

   ```
   aws dynamodb describe-contributor-insights --table-name Music
   ```

   在模式切换期间，`ContributorInsightsStatus` 将显示为 `ENABLING`。完成后，对于新的模式将显示 `ENABLED`。

### 管理 Contributor Insights
<a name="usecontributorinsights_cli.management"></a>

**管理 Contributor Insights 设置**

1. 在 `AlbumTitle-index` 全局二级索引禁用 CloudWatch Contributor Insights for DynamoDB。

   ```
   aws dynamodb update-contributor-insights \
                       --table-name Music --index-name AlbumTitle-index \
                       --contributor-insights-action=DISABLE
   ```

1. 列出您的账户中的所有 Contributor Insights 配置。

   ```
   aws dynamodb list-contributor-insights
   ```

   这将显示所有启用了 Contributor Insights 的表和索引，以及所用的模式。

1. 获取有关特定配置的详细信息。

   ```
   aws dynamodb describe-contributor-insights \
                       --table-name Music \
                       --index-name AlbumTitle-index
   ```

### 响应示例
<a name="usecontributorinsights_cli.examples"></a>

以下响应示例显示了各模式之间的不同：

#### 访问的键和受限的键模式响应
<a name="usecontributorinsights_cli.examples.accessed-throttled"></a>

```
{
    "TableName": "Music",
    "ContributorInsightsRuleList": [
        "DynamoDBContributorInsights-PKC-Music-1234567890123",
        "DynamoDBContributorInsights-PKT-Music-1234567890123",
        "DynamoDBContributorInsights-SKC-Music-1234567890123",
        "DynamoDBContributorInsights-SKT-Music-1234567890123"
    ],
    "ContributorInsightsStatus": "ENABLED",
    "ContributorInsightsMode": "ACCESSED_AND_THROTTLED_KEYS",
    "LastUpdateDateTime": "2024-01-15T10:30:00.000Z"
}
```

#### 受限的键模式响应
<a name="usecontributorinsights_cli.examples.throttled-only"></a>

```
{
    "TableName": "Music",
    "ContributorInsightsRuleList": [
        "DynamoDBContributorInsights-PKT-Music-1234567890123",
        "DynamoDBContributorInsights-SKT-Music-1234567890123"
    ],
    "ContributorInsightsStatus": "ENABLED",
    "ContributorInsightsMode": "THROTTLED_KEYS",
    "LastUpdateDateTime": "2024-01-15T10:35:00.000Z"
}
```

请注意，受限的键模式的规则较少（只有 PKT 和 SKT），这与更有侧重点的监控相对应。