

# 集成 Amazon OpenSearch Service
<a name="managed-prometheus-collectors-opensearch-setup"></a>

使用 Amazon CloudWatch 托管式 Prometheus 收集器，您可以自动收集 Amazon OpenSearch Service 域中与 Prometheus 兼容的指标。托管式收集器连接到 VPC，从域中收集集群、节点和索引指标，并将这些指标直接传输至 CloudWatch。借助此集成，无需部署任何代理或导出程序，即可在 CloudWatch 中监控域的运行状况和性能。

**注意**  
当 Amazon CloudWatch 托管式 Prometheus 收集器将 Amazon OpenSearch Service 指标传输至 CloudWatch 时，会自动针对每个指标丰富用于标识其来源的属性。每个指标都包含收集器的检测范围、记录 AWS 账户和区域的云属性，以及收集器从指标名称推断出的单位。使用 PromQL 查询指标时，可以对这些属性进行筛选和分组。

## 先决条件
<a name="managed-prometheus-collectors-opensearch-prerequisites"></a>

此过程假设您已熟悉 Amazon OpenSearch Service 域管理和 Amazon VPC 联网概念。
+ 具有 VPC 访问权限的 Amazon OpenSearch Service 域。托管式收集器仅支持具有 VPC 访问权限的域。不支持具有公有访问权限的域。
+ 至少两个子网位于不同的可用区
+ 允许收集器通过 HTTPS（端口 443）访问域端点的安全组规则。向域的安全组添加入站规则，以允许来自为收集器提供的安全组的 HTTPS 流量。

## 步骤 1：配置抓取配置
<a name="managed-prometheus-collectors-opensearch-scrape-config"></a>

以下是用于 Amazon OpenSearch Service 的抓取配置示例。托管式收集器会连接到指定的域，并自动收集其指标，因此无需在配置中指定抓取目标。配置必须包含 `scrape_configs` 部分，其中任务的 `job_name` 必须恰好为 `opensearch-exporter`。在下一步创建抓取程序时，您将引用此配置。有关 配置选项的更多信息，请参阅 [抓取程序配置](managed-prometheus-collectors-scraper-configuration.md)。

```
global:
  external_labels:
    domain_name: {{my-opensearch-domain}}

scrape_configs:
  - job_name: opensearch-exporter
    scrape_interval: 60s
```

## 步骤 2：创建抓取程序
<a name="managed-prometheus-collectors-opensearch-create-scraper"></a>

创建具有 CloudWatch 目标的抓取程序。您可以在 `exporters` 字段中指定要从中收集的域，并在 `source` 字段中提供网络配置（子网和安全组）。

------
#### [ AWS API ]

使用 `CreateScraper` API 操作，以便创建具有 CloudWatch 目标的抓取程序。将子网、安全组、域及数据集信息替换为您自己的值。

```
POST /scrapers HTTP/1.1

{
  "alias": "opensearch-metrics-scraper",
  "source": {
    "vpcConfiguration": {
      "subnetIds": ["{{subnet-subnet-id-1}}", "{{subnet-subnet-id-2}}"],
      "securityGroupIds": ["{{sg-security-group-id}}"]
    }
  },
  "exporters": [
    {
      "openSearchConfiguration": {
        "domainArn": "arn:aws:es:{{us-west-2}}:{{123456789012}}:domain/{{my-opensearch-domain}}"
      }
    }
  ],
  "destination": {
    "cloudWatchConfiguration": {
      "datasetArn": "arn:aws:cloudwatch:{{us-west-2}}:{{123456789012}}:dataset/default"
    }
  },
  "scrapeConfiguration": {
    "configurationBlob": "{{base64-encoded-blob}}"
  }
}
```

------
#### [ AWS CLI ]

使用 `create-scraper` 命令，以便创建具有 CloudWatch 目标的抓取程序。将子网、安全组、域及数据集信息替换为您自己的值。

```
aws amp create-scraper \
  --alias "opensearch-metrics-scraper" \
  --source '{
    "vpcConfiguration": {
      "subnetIds": ["{{subnet-subnet-id-1}}", "{{subnet-subnet-id-2}}"],
      "securityGroupIds": ["{{sg-security-group-id}}"]
    }
  }' \
  --exporters '[
    {
      "openSearchConfiguration": {
        "domainArn": "arn:aws:es:{{us-west-2}}:{{123456789012}}:domain/{{my-opensearch-domain}}"
      }
    }
  ]' \
  --scrape-configuration configurationBlob=$(base64 -w 0 {{opensearch-config.yaml}}) \
  --destination '{
    "cloudWatchConfiguration": {
      "datasetArn": "arn:aws:cloudwatch:{{us-west-2}}:{{123456789012}}:dataset/default"
    }
  }'
```

------

## 可用指标
<a name="managed-prometheus-collectors-opensearch-available-metrics"></a>
+ **集群指标**：集群运行状况和状态、节点数量和数据节点数量、活动分片和重新放置分片以及待处理任务。
+ **节点指标**：CPU 使用量、JVM 堆使用量和垃圾回收、操作系统内存、磁盘使用量以及每个节点的线程池活动。
+ **索引指标**：索引和搜索速率及延迟、文档数量、合并和刷新活动以及缓存使用量。

有关可用指标的完整列表，请参阅《Amazon Managed Service for Prometheus 用户指南》**中的[从 Amazon OpenSearch Service 收集的指标](https://docs.aws.amazon.com/prometheus/latest/userguide/prom-opensearch-integration.html#prom-opensearch-metrics)。

## 验证指标收集
<a name="managed-prometheus-collectors-opensearch-validate"></a>

要确认收集器正在传输域中的指标，需在 CloudWatch 中使用 [Query Studio](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-PromQL-QueryStudio.html) 运行以下查询。查询会返回域的集群运行状况。如果返回数据点，则表示收集器已成功从域中收集指标。

```
opensearch_cluster_health_status
```

## 跨账户可观测性
<a name="managed-prometheus-collectors-opensearch-cross-account"></a>

对于跨账户 Amazon OpenSearch Service 监控，建议使用 Amazon CloudWatch 指标集中化。有关更多信息，请参阅 [CloudWatch 指标集中化](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Unified-Cross-Account.html)。

有关使用角色链接的其他跨账户抓取程序配置的更多信息，请参阅《Amazon Managed Service for Prometheus 用户指南》**中的[集成 Amazon OpenSearch Service](https://docs.aws.amazon.com/prometheus/latest/userguide/prom-opensearch-integration.html)。

## 目前的局限性
<a name="managed-prometheus-collectors-opensearch-limitations"></a>
+ 托管式收集器仅支持具有 VPC 访问权限的域。
+ 抓取程序从单个 Amazon OpenSearch Service 域中收集指标。要从多个域收集指标，请为每个域创建单独的抓取程序。