

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

# 搭配 Amazon Managed Service for Prometheus 使用 OpenSearch 擷取管道
<a name="configure-client-prometheus"></a>

您可以使用 Amazon Managed Service for Prometheus 做為 OpenSearch Ingestion 管道的目的地，以時間序列格式存放指標。Prometheus 接收器可讓您將 OpenTelemetry 指標或其他時間序列資料從管道傳送至 Amazon Managed Service for Prometheus 工作區，以進行監控、提醒和分析。

`prometheus` 接收器外掛程式可讓 OpenSearch Ingestion 管道使用 Prometheus 遠端寫入通訊協定，將指標資料寫入 Amazon Managed Service for Prometheus 工作區。此整合可讓您：
+ 在 Amazon Managed Service for Prometheus 中存放時間序列指標資料
+ 使用 Amazon Managed Service for Prometheus 和 Amazon Managed Grafana 監控指標並發出提醒
+ 同時將指標路由到多個目的地 （例如，OpenSearch 和 Amazon Managed Service for Prometheus)
+ 從外部客服人員處理 OpenTelemetry 指標或在管道中產生指標

**Topics**
+ [先決條件](#prometheus-prereqs)
+ [步驟 1：設定管道角色](#prometheus-pipeline-role)
+ [步驟 2：建立管道](#prometheus-pipeline)
+ [監控和疑難排解](#prometheus-monitoring)
+ [限制](#prometheus-limitations)
+ [最佳實務](#prometheus-best-practices)

## 先決條件
<a name="prometheus-prereqs"></a>

設定 Prometheus 接收器之前，請確定您有下列項目：
+ **Amazon Managed Service for Prometheus 工作區**： AWS 區域 在與 OpenSearch Ingestion 管道相同的 AWS 帳戶 和 中建立工作區。如需說明，請參閱《*Amazon Managed Service for Prometheus 使用者指南*》中的[建立工作區](https://docs.aws.amazon.com/prometheus/latest/userguide/AMP-onboard-create-workspace.html)。
+ **IAM 許可**：設定具有寫入 Amazon Managed Service for Prometheus 許可的 IAM 角色。如需詳細資訊，請參閱[步驟 1：設定管道角色](#prometheus-pipeline-role)。

**注意**  
Amazon Managed Service for Prometheus 工作區必須使用 AWS 服務受管 AWS KMS 金鑰。OpenSearch Ingestion 管道中的 Amazon Managed Service for Prometheus 接收器目前不支援客戶受管 AWS KMS 金鑰。

## 步驟 1：設定管道角色
<a name="prometheus-pipeline-role"></a>

Prometheus 接收器會自動繼承[管道角色的](pipeline-security-overview.md#pipeline-security-sink) IAM 身分驗證許可，因此在接收器設定中不需要額外的角色組態 （例如 `sts_role_arn`)。

下列範例政策顯示使用 Amazon Managed Service for Prometheus 做為接收器的必要許可：

```
{
  "Version": "2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "AMPRemoteWrite",
      "Effect": "Allow",
      "Action": [
        "aps:RemoteWrite"
      ],
      "Resource": "arn:aws:aps:region:account-id:workspace/workspace-id"
    }
  ]
}
```

取代下列預留位置：
+ `region`：您的 AWS 區域 （例如，`us-east-1`)
+ `account-id`：您的 AWS 帳戶 ID
+ `workspace-id`：您的 Amazon Managed Service for Prometheus 工作區 ID

您必須將這些許可連接到管道角色。

確保您的管道角色具有信任關係，允許 OpenSearch Ingestion 擔任它：

```
{
  "Version": "2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "osis-pipelines.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
```

## 步驟 2：建立管道
<a name="prometheus-pipeline"></a>

設定許可後，您可以設定 OpenSearch Ingestion 管道，以使用 Amazon Managed Service for Prometheus 做為接收器。

### 基本組態
<a name="prometheus-basic-config"></a>

下列範例顯示最小 Prometheus 接收器組態：

```
version: "2"
sink:
  - prometheus:
      url: "https://aps-workspaces.region.amazonaws.com/workspaces/workspace-id/api/v1/remote_write"
      aws:
        region: "region"
```

您必須在`prometheus`接收器組態中指定 `url`選項，也就是 Amazon Managed Service for Prometheus 遠端寫入端點。若要格式化 URL，請在 Amazon Managed Service for Prometheus 主控台中找到您的工作區 ID，並建構 URL，如下所示：`https://aps-workspaces.region.amazonaws.com/workspaces/workspace-id/api/v1/remote_write`。

### 組態選項
<a name="prometheus-config-options"></a>

使用下列選項來設定 Prometheus 接收器的批次處理和排清行為：


**Prometheus 接收器組態選項**  

| 選項 | 必要 | Type | 說明 | 
| --- | --- | --- | --- | 
| max\$1events | 否 | Integer | 排清至 Prometheus 之前要累積的事件數量上限。預設值為 1000。 | 
| max\$1request\$1size | 否 | 位元組計數 | 排清前請求承載的大小上限。預設值為 1mb。 | 
| flush\$1interval | 否 | 持續時間 | 排清事件之前等待的時間上限。預設值為 10s。允許的值上限為 60s。 | 

### 範例管道
<a name="prometheus-example-pipelines"></a>

**範例 1：Amazon Managed Service for Prometheus 的 OpenTelemetry 指標**

此管道會從外部代理程式接收 OpenTelemetry 指標，並將其寫入 Amazon Managed Service for Prometheus：

```
version: "2"
source:
  otel_metrics_source:
    path: "/v1/metrics"
    output_format: otel

sink:
  - prometheus:
      url: "https://aps-workspaces.us-east-1.amazonaws.com/workspaces/ws-a1b2c3d4-5678-90ab-cdef-EXAMPLE11111/api/v1/remote_write"
      aws:
        region: "us-east-1"
```

**範例 2：雙接收器 - OpenSearch 和 Amazon Managed Service for Prometheus**

此管道會將指標路由到 OpenSearch 和 Amazon Managed Service for Prometheus：

```
version: "2"
source:
  otel_metrics_source:
    path: "/v1/metrics"
    output_format: otel

sink:
  - opensearch:
      hosts:
        - "https://search-domain-endpoint.us-east-1.es.amazonaws.com"
      index: "metrics-%{yyyy.MM.dd}"
      aws:
        region: "us-east-1"
        sts_role_arn: "arn:aws:iam::123456789012:role/OSI-Pipeline-Role"

  - prometheus:
      url: "https://aps-workspaces.us-east-1.amazonaws.com/workspaces/ws-a1b2c3d4-5678-90ab-cdef-EXAMPLE11111/api/v1/remote_write"
      aws:
        region: "us-east-1"
```

**範例 3：具有篩選的指標**

此管道會在傳送至 Amazon Managed Service for Prometheus 之前篩選指標：

```
version: "2"
source:
  otel_metrics_source:
    path: "/v1/metrics"
    output_format: otel

processor:
  - drop_events:
      drop_when: '/name != "http.server.duration" and /name != "http.client.duration"'

sink:
  - prometheus:
      url: "https://aps-workspaces.us-east-1.amazonaws.com/workspaces/ws-a1b2c3d4-5678-90ab-cdef-EXAMPLE11111/api/v1/remote_write"
      aws:
        region: "us-east-1"
```

您可以使用預先設定的 Amazon Managed Service for Prometheus 藍圖來建立這些管道。如需詳細資訊，請參閱[使用藍圖](pipeline-blueprint.md)。

### 使用 Amazon Managed Service for Prometheus 接收器建立管道
<a name="prometheus-create-pipeline"></a>

#### 使用 AWS 主控台
<a name="prometheus-console"></a>

1. 導覽至 OpenSearch Service 主控台。

1. 選擇**擷取**下的**管道**。

1. 選擇 **Create pipeline (建立管道)**。

1. 選取**使用藍圖建置**，然後選擇 **OpenTelemetry 指標到 Amazon Prometheus** 藍圖。

1. 設定管道：
   + 輸入您的 Amazon Managed Service for Prometheus 工作區 ID
   + 指定管道角色 ARN
   + 視需要設定來源和處理器設定

1. 檢閱並建立管道。

#### 使用 AWS CLI
<a name="prometheus-cli"></a>

使用所需的組態建立管道組態檔案 （例如 `amp-pipeline.yaml`)，然後執行：

```
aws osis create-pipeline \
  --pipeline-name my-amp-pipeline \
  --min-units 2 \
  --max-units 4 \
  --pipeline-configuration-body file://amp-pipeline.yaml
```

#### 使用 AWS CloudFormation
<a name="prometheus-cfn"></a>

```
Resources:
  MyAMPPipeline:
    Type: AWS::OSIS::Pipeline
    Properties:
      PipelineName: my-amp-pipeline
      MinUnits: 2
      MaxUnits: 4
      PipelineConfigurationBody: |
        version: "2"
        source:
          otel_metrics_source:
            path: "/v1/metrics"
            output_format: otel
        sink:
          - prometheus:
              url: "https://aps-workspaces.us-east-1.amazonaws.com/workspaces/ws-a1b2c3d4-5678-90ab-cdef-EXAMPLE11111/api/v1/remote_write"
              aws:
                region: "us-east-1"
```

## 監控和疑難排解
<a name="prometheus-monitoring"></a>

### CloudWatch 指標
<a name="prometheus-cloudwatch-metrics"></a>

使用 CloudWatch 指標監控管道的效能：
+ `DocumentsWritten`：成功寫入 Amazon Managed Service for Prometheus 的指標數量
+ `DocumentsWriteFailed`：無法寫入的指標數量
+ `RequestLatency`：遠端寫入請求的延遲

### 常見問題
<a name="prometheus-troubleshooting"></a>

**問題**：管道無法寫入 Amazon Managed Service for Prometheus

**解決方案：**
+ 驗證 URL 中的工作區 ID 和區域是否正確
+ 確保管道角色具有 `aps:RemoteWrite` 許可
+ 檢查工作區是否使用服務受管 AWS KMS 金鑰
+ 驗證管道和工作區是否位於相同的 AWS 帳戶

**問題**：身分驗證錯誤

**解決方案：**
+ 驗證信任關係`osis-pipelines.amazonaws.com`允許 擔任管道角色
+ 確保管道角色具有必要的`aps:RemoteWrite`許可

**問題**：高延遲或限流

**解決方案：**
+ 增加管道容量單位
+ 在處理器中實作批次處理
+ 檢閱 Amazon Managed Service for Prometheus 服務配額

## 限制
<a name="prometheus-limitations"></a>

當您為 Amazon Managed Service for Prometheus 設定 OpenSearch Ingestion 管道時，請考慮下列限制：
+ Amazon Managed Service for Prometheus 工作區必須使用 AWS 服務受管 AWS KMS 金鑰。目前不支援客戶受管 AWS KMS 金鑰。
+ 管道和 Amazon Managed Service for Prometheus 工作區必須位於相同的 中 AWS 帳戶。

## 最佳實務
<a name="prometheus-best-practices"></a>
+ **使用相同的 IAM 角色**：Prometheus 接收器會自動使用管道角色。如果使用其他接收器，請確保 `sts_role_arn` 與管道角色相同
+ **監控指標**：針對失敗的寫入和高延遲設定 CloudWatch 警示
+ **實作篩選**：在傳送至 Amazon Managed Service for Prometheus 之前，使用處理器篩選不必要的指標
+ **適當大小的容量**：從最小容量開始，並根據指標磁碟區擴展
+ **使用藍圖**：利用預先設定的藍圖處理常見使用案例