

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 將 CloudWatch 指標與 Amazon Managed Service for Prometheus 整合
<a name="integrating-cw-firehose"></a>

這有助於將您的所有指標集中在一個位置。Amazon Managed Service for Prometheus 不會自動擷取 Amazon CloudWatch 指標。不過，您可以使用 Amazon Data Firehose 和 AWS Lambda 將 CloudWatch 指標推送至 Amazon Managed Service for Prometheus。

本節說明如何檢測 [Amazon CloudWatch 指標串流](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Metric-Streams.html)，並使用 [Amazon Data Firehose](https://docs.aws.amazon.com/firehose/latest/dev/what-is-this-service.html) 和 [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html)將指標擷取至 Amazon Managed Service for Prometheus。

您將使用[AWS 雲端開發套件 (CDK)](https://aws.amazon.com/cdk/) 設定堆疊來建立 Firehose 交付串流、Lambda 和 Amazon S3 儲存貯體，以示範完整的案例。

## 基礎設施
<a name="integ-cw-fh-infrastructure"></a>

您必須做的第一件事是為此配方設定基礎設施。

CloudWatch 指標串流允許將串流指標資料轉送到 HTTP 端點或 [Amazon S3 儲存貯體](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html)。

設定基礎設施將包括 4 個步驟：
+ 設定先決條件
+ 建立 Amazon Managed Service for Prometheus 工作區
+ 安裝相依項目
+ 部署堆疊

**先決條件**
+  AWS CLI 會在您的環境中[安裝](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html)和[設定](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html) 。
+ 已在您的環境中安裝 [AWS CDK Typescript](https://docs.aws.amazon.com/cdk/latest/guide/work-with-cdk-typescript.html)。
+ 已在您的環境中安裝 Node.js 和 Go。
+ [AWS 可觀測性 CloudWatch 指標匯出程式 github 儲存庫](https://github.com/aws-observability/observability-best-practices/tree/main/sandbox/CWMetricStreamExporter) (`CWMetricsStreamExporter`) 已複製到您的本機電腦。

**建立 Amazon Managed Service for Prometheus 工作區**

1. 此配方中的示範應用程式將在 Amazon Managed Service for Prometheus 最上方執行。透過下列命令建立 Amazon Managed Service for Prometheus 工作區：

   ```
   aws amp create-workspace --alias prometheus-demo-recipe
   ```

1. 確定已使用下列命令建立您的工作區：

   ```
   aws amp list-workspaces
   ```

   如需有關 Amazon Managed Service for Prometheus 的詳細資訊，請參閱 [Amazon Managed Service for Prometheus](https://docs.aws.amazon.com/prometheus/latest/userguide/AMP-getting-started.html) 使用者指南。

**安裝相依項目**

1. **安裝相依項目**

   從 `aws-o11y-recipes` 儲存庫的根目錄中，使用以下命令將目錄變更為 `CWMetricStreamExporter`：

   ```
   cd sandbox/CWMetricStreamExporter
   ```

   現在，這將視為儲存庫的根目錄，以後也是。

1. 透過下列命令將目錄變更為 `/cdk`：

   ```
   cd cdk
   ```

1. 執行以下命令以安裝 CDK 相依項目：

   ```
   npm install
   ```

1. 將目錄變更回儲存庫的根目錄，然後使用以下命令將目錄變更為 `/lambda`：

   ```
   cd lambda
   ```

1. 一旦進入 `/lambda` 資料夾後，使用以下命令安裝 Go 相依項目：

   ```
   go get
   ```

   現在已安裝所有相依項目。

**部署堆疊**

1. 在儲存庫的根目錄中，開啟 `config.yaml` 並修改 Amazon Managed Service for Prometheus 工作區 URL，方法是將 `{workspace}` 替換為新建立的工作區 ID ，以及您 Amazon Managed Service for Prometheus 工作區所在的區域。

   例如，將以下項目修改為：

   ```
   AMP: 
       remote_write_url: "https://aps-workspaces.us-east-2.amazonaws.com/workspaces/{workspaceId}/api/v1/remote_write" 
       region: us-east-2
   ```

   將 Firehose 交付串流和 Amazon S3 儲存貯體的名稱變更為喜歡的名稱。

1. 若要建置 AWS CDK 和 Lambda 程式碼，請在儲存庫的根目錄中執行下列命令：

   ```
   npm run build
   ```

   此建立步驟可確保建立 Go Lambda 二進制文件，並將 CDK 部署到 CloudFormation。

1. 若要完成部署，請檢閱並接受堆疊所需的 IAM 變更。

1. (選用) 若已透過執行下列命令建立堆疊，則可有所變化。

   ```
   aws cloudformation list-stacks
   ```

   名為 `CDK Stack` 的堆疊將會在清單中。

## 建立 Amazon CloudWatch 串流
<a name="integ-cw-fh-stream"></a>

既然您有處理指標的 lambda 函數，您可自 Amazon CloudWatch 建立指標串流。

**建立 CloudWatch 指標串流**

1. 導覽至 CloudWatch 主控台，網址為 [https://console.aws.amazon.com/cloudwatch/home\$1metric-streams:streamsList](https://console.aws.amazon.com/cloudwatch/home#metric-streams:streamsList)，然後選取**建立指標串流**。

1. 選取所需指標，可以是所有指標，或僅從所選命名空間選取。

1. 在 `Configuration` 下方，選擇**選取您帳戶擁有的現有 Firehose**。

1. 您將會使用 CDK 先前建立的 Firehose。在**選取您的 Kinesis 資料 Firehose 串流**下拉式清單中，選取先前建立的串流。名稱將會像是 `CdkStack-KinesisFirehoseStream123456AB-sample1234`。

1. 將輸出格式變更為 **JSON**。

1. 為指標串流賦予對您有意義的名稱。

1. 選擇 **Create metric stream** (建立指標串流)。

1. (選用) 若要驗證 Lambda 函數調用，請導覽至 [Lambda 主控台](https://console.aws.amazon.com/lambda/home)並選擇函數 `KinesisMessageHandler`。選取**監控**索引標籤和**日誌**子索引標籤，在**最近的調用**下應該會有要觸發的 Lambda 函數輸入項。
**注意**  
最多可能需要 5 分鐘才會開始在**監控**索引標籤中顯示調用。

您的指標現已從 Amazon CloudWatch 串流到 Amazon Managed Service for Prometheus。

## 清除
<a name="integ-cw-fh-cleanup"></a>

您可能想要清除本範例中使用的資源。下列程序說明如何執行此作業。這會停止您建立的指標串流。

**清理資源**

1. 首先，請使用下列命令刪除 CloudFormation 堆疊：

   ```
   cd cdk
   cdk destroy
   ```

1. 移除 Amazon Managed Service for Prometheus 工作區：

   ```
   aws amp delete-workspace --workspace-id \
       `aws amp list-workspaces --alias prometheus-sample-app --query 'workspaces[0].workspaceId' --output text`
   ```

1. 最後，使用 [Amazon CloudWatch 主控台](https://console.aws.amazon.com/cloudwatch/home#metric-streams:streamsList)移除 Amazon CloudWatch 指標串流。