

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 在适用于 Pro OpenSearch metheus 的亚马逊托管服务中使用采集管道
<a name="configure-client-prometheus"></a>

您可以使用适用于 Prometheus 的亚马逊托管服务作为采集渠道的目的地，以 OpenSearch 时间序列格式存储指标。Prometheus 接收器允许您将管道中的指标或其他时间序列数据 OpenTelemetry 发送到适用于 Prometheus 的亚马逊托管服务工作区，以进行监控、提醒和分析。

通过`prometheus`接收器插件， OpenSearch Ingestion 管道能够使用 Prometheus 远程写入协议将指标数据写入适用于 Prometheus 工作空间的亚马逊托管服务。此集成使您能够：
+ 将时间序列指标数据存储在适用于 Prometheus 的亚马逊托管服务中
+ 使用适用于 Prometheus 的亚马逊托管服务和亚马逊托管 Grafana 监控指标并发出警报
+ 同时将指标路由到多个目的地（例如， OpenSearch 以及适用于 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 接收器之前，请确保具备以下条件：
+ **适用于 Prometheus 的亚马逊托管服务**工作空间：在与您的摄取管道 AWS 账户 相同 AWS 区域 的工作空间中创建一个工作空间。 OpenSearch 有关说明，请参阅《适用于 *Prometheus 的亚马逊托管服务用户*指南》中的[创建工作空间](https://docs.aws.amazon.com/prometheus/latest/userguide/AMP-onboard-create-workspace.html)。
+ **IAM 权限**：将 IAM 角色配置为具有写入适用于 Prometheus 的亚马逊托管服务的权限。有关更多信息，请参阅 [步骤 1：配置管道角色](#prometheus-pipeline-role)。

**注意**  
适用于 Prometheus 工作空间的亚马逊托管服务必须使用服务托管密钥。 AWS AWS KMS Amazon Prometheus 托管服务目前不支持客户管理的 AWS KMS 密钥 Prometheus 接收管道中的接收渠道。 OpenSearch 

## 步骤 1：配置管道角色
<a name="prometheus-pipeline-role"></a>

Prometheus 接收器会自动继承[管道角色](pipeline-security-overview.md#pipeline-security-sink)的 IAM 身份验证权限，因此无需在接收器设置中进行其他角色配置（`sts_role_arn`例如）。

以下示例策略显示了使用适用于 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 账户 身份证
+ `workspace-id`: 您的亚马逊托管服务 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 采集管道配置为使用适用于 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`选项，即适用于 Prometheus 的亚马逊托管服务远程写入端点。要格式化 URL，请在适用于 Prometheus 的亚马逊托管服务控制台中找到您的工作空间 ID，然后按如下方式构建 URL：。`https://aps-workspaces.region.amazonaws.com/workspaces/workspace-id/api/v1/remote_write`

### 配置选项
<a name="prometheus-config-options"></a>

使用以下选项为 Prometheus 接收器配置批处理和刷新行为：


**Prometheus 水槽配置选项**  

| Option | 必需 | Type | 说明 | 
| --- | --- | --- | --- | 
| max\$1events | 否 | 整数 | 在刷新到 Prometheus 之前要累积的最大事件数。默认值为 1000。 | 
| max\$1request\$1size | 否 | 字节数 | 刷新前请求负载的最大大小。默认值为 1mb。 | 
| flush\$1interval | 否 | Duration | 在刷新事件之前等待的最长时间。默认值为 10s。允许的最大值为60s。 | 

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

**示例 1：适用于 Prometheus 的亚马逊托管服务的 OpenTelemetry 指标**

此管道接收来自外部代理的 OpenTelemetry 指标，并将其写入适用于 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 以及适用于 Prometheus 的亚马逊托管服务**

此渠道将指标传送到两者 OpenSearch 以及适用于 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：带筛选功能的指标**

在将指标发送到亚马逊 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 Prometheus 托管服务蓝图来创建这些管道。有关更多信息，请参阅 [使用蓝图](pipeline-blueprint.md)。

### 使用亚马逊托管服务为 Prometheus sink 创建管道
<a name="prometheus-create-pipeline"></a>

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

1. 导航到 OpenSearch 服务控制台。

1. 在 “接入” 下选择 “****管**道**”。

1. 选择 **Create pipeline（创建管道）**。

1. 选择 “**使用蓝图构建**”，然后选择 **Amazon Prometheus 蓝图的OpenTelemetry 指标**。

1. 配置管道：
   + 输入你的 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`: 成功写入亚马逊 Prometheus 托管服务的指标数量
+ `DocumentsWriteFailed`: 写入失败的指标数量
+ `RequestLatency`: 远程写入请求的延迟

### 常见问题
<a name="prometheus-troubleshooting"></a>

**问题**：Pipeline 无法写信给 Prometheus 的亚马逊托管服务

**解决方案**
+ 验证 URL 中的工作空间 ID 和区域是否正确
+ 确保管道角色拥有`aps:RemoteWrite`权限
+ 检查工作空间是否使用服务管理 AWS KMS 的密钥
+ 验证管道和工作空间是否相同 AWS 账户

**问题**：身份验证错误

**解决方案**
+ 验证信任关系是否`osis-pipelines.amazonaws.com`允许担任管道角色
+ 确保管道角色具有所需的`aps:RemoteWrite`权限

**问题**：高延迟或限流

**解决方案**
+ 增加管道容量单位
+ 在处理器中实现批处理
+ 查看亚马逊托管服务 Prometheus 服务配额

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

为适用于 Prometheus 的亚马逊托管服务设置 OpenSearch 摄取管道时，请考虑以下限制：
+ 适用于 Prometheus 工作空间的亚马逊托管服务必须使用服务托管密钥。 AWS AWS KMS 目前不支持客户管理的 AWS KMS 密钥。
+ Pipeline 和 Amazon Prometheus 托管服务工作空间必须相同。 AWS 账户

## 最佳实践
<a name="prometheus-best-practices"></a>
+ **使用相同的 IAM 角色**：Prometheus 接收器会自动使用管道角色。如果使用其他水槽`sts_role_arn`，请确保与管道角色相同
+ **监控指标**：为写入失败和高延迟设置 CloudWatch 警报
+ **实施筛选**：在发送到亚马逊 Prometheus 托管服务之前，使用处理器筛选不必要的指标
+ **适当调整容量**：从最小容量开始，然后根据指标容量进行扩展
+ **使用蓝图**：将预先配置的蓝图用于常见用例