

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

# 將指標擷取到您的 Amazon Managed Service for Prometheus 工作區
<a name="AMP-ingest-methods"></a>

您必須先將指標擷取到您的 Amazon Managed Service for Prometheus 工作區，才能查詢或提醒這些指標。本節說明如何設定擷取指標至工作區。

**注意**  
擷取到工作區的指標預設會儲存 150 天，然後自動刪除。您可以將工作區設定為最長 1095 天 （三年），以調整保留期間。如需詳細資訊，請參閱[設定工作區](https://docs.aws.amazon.com/prometheus/latest/userguide/AMP-workspace-configuration.html)。

有兩種方法可將指標擷取至 Amazon Managed Service for Prometheus 工作區。
+ **使用 AWS 受管收集器** – Amazon Managed Service for Prometheus 提供全受管、無代理程式的抓取器，可自動從 Amazon Elastic Kubernetes Service (Amazon EKS) 叢集*抓取*指標。抓取會自動從與 Prometheus 兼容的端點中提取指標。
+ **使用客戶管理的收集器**：您有許多管理自己收集器的選項。要使用的兩個最常見收集器是安裝您自己的 Prometheus 執行個體、在代理程式模式下執行，或使用 AWS Distro for OpenTelemetry。下節會詳細說明這些內容。

  收集器會使用 Prometheus 遠端寫入功能，將指標傳送至 Amazon Managed Service for Prometheus。您可以使用 Prometheus 遠端寫入您自己的應用程式，將指標直接傳送至 Amazon Managed Service for Prometheus。有關直接使用遠端寫入和遠端寫入組態的更多詳細資訊，請參閱 Prometheus 說明文件中的 [remote\$1write](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#remote_write)。

**Topics**
+ [使用 AWS 受管收集器擷取指標](AMP-collector.md)
+ [客戶受管收集器](self-managed-collectors.md)

# 使用 AWS 受管收集器擷取指標
<a name="AMP-collector"></a>

Amazon Managed Service for Prometheus 的常見使用案例是監控 Amazon Elastic Kubernetes Service (Amazon EKS) 管理的 Kubernetes 叢集。Kubernetes 叢集和 Amazon EKS 內執行的許多應用程式會自動匯出其指標，以供與 Prometheus 相容的湊集器存取。

**注意**  
Amazon EKS 會在叢集中公開 API 伺服器指標、`kube-controller-manager`指標和`kube-scheduler`指標。在 Kubernetes 環境中執行的許多其他技術和應用程式提供 Prometheus 相容指標。如需可用匯出工具的完整清單，請參閱 Prometheus 說明文件中的[匯出工具和整合](https://prometheus.io/docs/instrumenting/exporters/)。

Amazon Managed Service for Prometheus 提供全受管、代理程式較少的抓取器或*收集器*，可自動探索和提取與 Prometheus 相容的指標。您無需管理、安裝、修補或維護代理程式或湊集器。Amazon Managed Service for Prometheus 收集器可為您的 Amazon EKS 叢集提供可靠、穩定、高可用性、自動擴展的指標集合。Amazon Managed Service for Prometheus 受管收集器可與 Amazon EKS 叢集搭配使用，包括 EC2 和 Fargate。

Amazon Managed Service for Prometheus 收集器會在建立湊集器時，為指定的每個子網路建立彈性網路介面 (ENI)。收集器會透過這些 ENI 湊集指標，並使用 `remote_write` 將資料推送到使用 VPC 端點的 Amazon Managed Service for Prometheus 工作區。湊集的資料永遠不會在公有網際網路上傳輸。

下列主題提供有關如何在 Amazon EKS 叢集中使用 Amazon Managed Service for Prometheus 收集器，以及所收集指標的詳細資訊。

**Topics**
+ [設定 Amazon EKS 的受管收集器](AMP-collector-how-to.md)
+ [設定 Amazon MSK 的受管 Prometheus 收集器](prom-msk-integration.md)
+ [什麼是與 Prometheus 相容的指標？](prom-compatible-metrics.md)
+ [使用付費日誌監控收集器](AMP-collector-vended-logs.md)

# 設定 Amazon EKS 的受管收集器
<a name="AMP-collector-how-to"></a>

若要使用 Amazon Managed Service for Prometheus 收集器，您可以建立抓取器來探索和提取 Amazon EKS 叢集中的指標。您也可以建立與 Amazon Managed Streaming for Apache Kafka 整合的抓取器。如需詳細資訊，請參閱[整合 Amazon MSK](https://docs.aws.amazon.com/prometheus/latest/userguide/prom-msk-integration.html)。
+ 您可以建立湊集器作為 Amazon EKS 叢集建立作業的一部份。如需建立 Amazon EKS 叢集的詳細資訊，包括建立抓取器，請參閱《[Amazon EKS 使用者指南》中的建立 Amazon EKS 叢集](https://docs.aws.amazon.com/eks/latest/userguide/create-cluster.html)。 **
+ 您可以使用 AWS API 或使用 ，以程式設計方式建立自己的抓取器 AWS CLI。

Amazon Managed Service for Prometheus 收集器會抓取與 Prometheus 相容的指標。如需 Prometheus 相容指標的詳細資訊，請參閱 [什麼是與 Prometheus 相容的指標？](prom-compatible-metrics.md)。Amazon EKS 叢集會公開 API 伺服器的指標。Kubernetes 版本`1.28`或更高版本的 Amazon EKS 叢集也會公開 `kube-scheduler`和 的指標`kube-controller-manager`。如需詳細資訊，請參閱《*Amazon EKS 使用者指南*》中的[擷取 Prometheus 格式的控制平面原始指標](https://docs.aws.amazon.com/eks/latest/userguide/view-raw-metrics.html#scheduler-controller-metrics)。

**注意**  
從叢集擴展指標可能會產生網路用量的費用。最佳化這些成本的一種方法是設定您的`/metrics`端點來壓縮提供的指標 （例如，使用 gzip)，以減少必須跨網路移動的資料。如何執行此操作取決於提供指標的應用程式或程式庫。根據預設，某些程式庫 gzip。

下列主題說明如何建立、管理和設定湊集器。

**Topics**
+ [建立湊集器](#AMP-collector-create)
+ [設定 Amazon EKS 叢集](#AMP-collector-eks-setup)
+ [尋找並刪除湊集器](#AMP-collector-list-delete)
+ [湊集器組態](#AMP-collector-configuration)
+ [對湊集器組態進行移難排解](#AMP-collector-troubleshoot)
+ [湊集器限制](#AMP-collector-limits)

## 建立湊集器
<a name="AMP-collector-create"></a>

Amazon Managed Service for Prometheus 收集器包含一個湊集器，可從 Amazon EKS 叢集中探索和收集指標。Amazon Managed Service for Prometheus 可為您管理湊集器，提供所需的可擴展性、安全性和可靠性，而無需自行管理任何執行個體、代理程式或湊集器。

建立抓取器的方法有三種：
+ 當您[透過 Amazon EKS 主控台建立 Amazon EKS 叢集](https://docs.aws.amazon.com/eks/latest/userguide/create-cluster.html)並選擇開啟 Prometheus 指標時，系統會自動為您建立抓取器。
+ 您可以從 Amazon EKS 主控台為現有叢集建立抓取器。在 [Amazon EKS 主控台](https://console.aws.amazon.com/eks/home#/clusters)中開啟叢集，然後在**可觀測性**索引標籤上，選擇**新增抓取器**。

  如需可用設定的詳細資訊，請參閱《*Amazon EKS 使用者指南*》中的[開啟 Prometheus 指標](https://docs.aws.amazon.com/eks/latest/userguide/prometheus.html#turn-on-prometheus-metrics)。
+ 您可以使用 AWS API 或 建立抓取器 AWS CLI。

  這些選項會在下列程序中說明。

您需先滿足幾項先決條件，才能建立自己的湊集器：
+ 您必須已建立 Amazon EKS 叢集。
+ 您的 Amazon EKS 叢集必須設定[叢集端點存取控制](https://docs.aws.amazon.com/eks/latest/userguide/cluster-endpoint.html)以包含私有存取。它可以包括私有和公有，但必須包含私有。
+ Amazon EKS 叢集所在的 Amazon VPC 必須[啟用 DNS](https://docs.aws.amazon.com/vpc/latest/userguide/AmazonDNS-concepts.html)。

**注意**  
叢集將依其 Amazon 資源名稱 (ARN) 與抓取器建立關聯。如果您刪除叢集，然後使用相同名稱建立新的叢集，則 ARN 將重複使用於新叢集。因此，抓取器會嘗試收集新叢集的指標。[您可以將抓取器](#AMP-collector-list-delete)與刪除叢集分開刪除。

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

**使用 AWS API 建立 **

使用 `CreateScraper` API 操作來建立具有 AWS API 的抓取器。以下範例會在 `us-west-2` 地區中建立湊集器。您需要使用自己的 ID 取代 AWS 帳戶、工作區、安全性和 Amazon EKS 叢集資訊，並提供用於湊集器的組態。

**注意**  
安全群組和子網路應設定為您要連線之叢集的安全群組和子網路。  
您必須包含至少兩個子網路，至少位於兩個可用區域。

`scrapeConfiguration` 是一個 base64 編碼的 Prometheus 組態 YAML 檔案。您可以透過 `GetDefaultScraperConfiguration` API 作業下載一般用途設定。如需 的格式詳細資訊`scrapeConfiguration`，請參閱 [湊集器組態](#AMP-collector-configuration)。

```
POST /scrapers HTTP/1.1
Content-Length: 415 
Authorization: AUTHPARAMS
X-Amz-Date: 20201201T193725Z
User-Agent: aws-cli/1.18.147 Python/2.7.18 Linux/5.4.58-37.125.amzn2int.x86_64 botocore/1.18.6

{
    "alias": "myScraper",
    "destination":  {
        "ampConfiguration": {
            "workspaceArn": "arn:aws:aps:us-west-2:account-id:workspace/ws-workspace-id"
        }
    },
    "source": {
        "eksConfiguration": {
            "clusterArn": "arn:aws:eks:us-west-2:account-id:cluster/cluster-name",
            "securityGroupIds": ["sg-security-group-id"],
            "subnetIds": ["subnet-subnet-id-1", "subnet-subnet-id-2"]
        }
    },
    "scrapeConfiguration": {
        "configurationBlob": <base64-encoded-blob>
    }
}
```

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

**使用 AWS CLI建立湊集器**

使用 `create-scraper`命令來建立具有 的抓取器 AWS CLI。以下範例會在 `us-west-2` 地區中建立湊集器。您需要使用自己的 ID 取代 AWS 帳戶、工作區、安全性和 Amazon EKS 叢集資訊，並提供用於湊集器的組態。

**注意**  
安全群組和子網路應設定為您要連線之叢集的安全群組和子網路。  
您必須包含至少兩個子網路，至少位於兩個可用區域。

`scrape-configuration` 是一個 base64 編碼的 Prometheus 組態 YAML 檔案。您可以使用 `get-default-scraper-configuration`命令下載一般用途組態。如需 的格式詳細資訊`scrape-configuration`，請參閱 [湊集器組態](#AMP-collector-configuration)。

```
aws amp create-scraper \
  --source eksConfiguration="{clusterArn='arn:aws:eks:us-west-2:account-id:cluster/cluster-name', securityGroupIds=['sg-security-group-id'],subnetIds=['subnet-subnet-id-1', 'subnet-subnet-id-2']}" \
  --scrape-configuration configurationBlob=<base64-encoded-blob> \
  --destination ampConfiguration="{workspaceArn='arn:aws:aps:us-west-2:account-id:workspace/ws-workspace-id'}"
```

------

下列是您可以與 AWS API 一併使用的完整湊集器作業清單：
+ 使用 [CreateScraper](https://docs.aws.amazon.com/prometheus/latest/APIReference/API_CreateScraper.html) API 操作建立抓取器。
+ 使用 [ListScrapers](https://docs.aws.amazon.com/prometheus/latest/APIReference/API_ListScrapers.html) API 操作列出現有的抓取器。
+ 使用 [UpdateScraper](https://docs.aws.amazon.com/prometheus/latest/APIReference/API_UpdateScraper.html) API 操作更新抓取器的別名、組態或目的地。
+ 使用 [DeleteScraper](https://docs.aws.amazon.com/prometheus/latest/APIReference/API_DeleteScraper.html) API 操作刪除抓取器。
+ 使用 [DescribeScraper](https://docs.aws.amazon.com/prometheus/latest/APIReference/API_DescribeScraper.html) API 操作取得有關抓取器的詳細資訊。
+ 使用 [GetDefaultScraperConfiguration](https://docs.aws.amazon.com/prometheus/latest/APIReference/API_GetDefaultScraperConfiguration.html) API 操作取得抓取器的一般用途組態。

**注意**  
必須設定您要湊集的 Amazon EKS 叢集以讓 Amazon Managed Service for Prometheus 存取指標。下一個主題說明如何設定叢集。

### 跨帳戶設定
<a name="cross-account-remote-write"></a>

若要在 Amazon EKS 叢集和 Amazon Managed Service for Prometheus 工作區位於不同帳戶時建立跨帳戶抓取器，請使用下列程序。例如，您有`account_id_source`包含 Amazon EKS 叢集的來源帳戶，以及`account_id_target`包含 Amazon Managed Service for Prometheus 工作區的目標帳戶。

**在跨帳戶設定中建立抓取器**

1. 在來源帳戶中，建立角色`arn:aws:iam::account_id_source:role/Source`並新增下列信任政策。

   ```
   {
       "Effect": "Allow",
       "Principal": {
       "Service": [
           "scraper.aps.amazonaws.com"
        ]
       },
       "Action": "sts:AssumeRole",
       "Condition": {
           "ArnEquals": {
               "aws:SourceArn": "scraper_ARN"
           },
           "StringEquals": {
               "AWS:SourceAccount": "account_id"
           }
       }
   }
   ```

1. 在來源 (Amazon EKS 叢集） 和目標 (Amazon Managed Service for Prometheus 工作區） 的每個組合上，您需要建立角色，`arn:aws:iam::account_id_target:role/Target`並新增具有 [AmazonPrometheusRemoteWriteAccess](https://docs.aws.amazon.com/prometheus/latest/userguide/security-iam-awsmanpol.html) 許可的下列信任政策。

   ```
   {
     "Effect": "Allow",
     "Principal": {
        "AWS": "arn:aws:iam::account_id_source:role/Source"
     },
     "Action": "sts:AssumeRole",
     "Condition": {
        "StringEquals": {
           "sts:ExternalId": "scraper_ARN"
         }
     }
   }
   ```

1. 使用 `--role-configuration`選項建立抓取器。

   ```
   aws amp create-scraper \
     --source eksConfiguration="{clusterArn='arn:aws:eks:us-west-2:account-id_source:cluster/xarw,subnetIds=[subnet-subnet-id]}" \
     --scrape-configuration configurationBlob=<base64-encoded-blob> \
     --destination ampConfiguration="{workspaceArn='arn:aws:aps:us-west-2:account-id_target:workspace/ws-workspace-id'}"\
     --role-configuration '{"sourceRoleArn":"arn:aws:iam::account-id_source:role/Source", "targetRoleArn":"arn:aws:iam::account-id_target:role/Target"}'
   ```

1. 驗證抓取器建立。

   ```
   aws amp list-scrapers
   {
       "scrapers": [
           {
               "scraperId": "scraper-id",
               "arn": "arn:aws:aps:us-west-2:account_id_source:scraper/scraper-id",
               "roleArn": "arn:aws:iam::account_id_source:role/aws-service-role/scraper.aps.amazonaws.com/AWSServiceRoleForAmazonPrometheusScraperInternal_cc319052-41a3-4",
               "status": {
                   "statusCode": "ACTIVE"
               },
               "createdAt": "2024-10-29T16:37:58.789000+00:00",
               "lastModifiedAt": "2024-10-29T16:55:17.085000+00:00",
               "tags": {},
               "source": {
                   "eksConfiguration": {
                       "clusterArn": "arn:aws:eks:us-west-2:account_id_source:cluster/xarw",
                       "securityGroupIds": [
                           "sg-security-group-id",
                           "sg-security-group-id"
                       ],
                       "subnetIds": [
                           "subnet-subnet_id"
                       ]
                   }
               },
               "destination": {
                   "ampConfiguration": {
                       "workspaceArn": "arn:aws:aps:us-west-2:account_id_target:workspace/ws-workspace-id"
                   }
               }
           }
       ]
   }
   ```

### 在 RoleConfiguration 和服務連結角色之間變更
<a name="changing-roles"></a>

當您想要切換回服務連結角色，而不是 `RoleConfiguration` 以寫入 Amazon Managed Service for Prometheus 工作區時，您必須更新 ，`UpdateScraper`並在與抓取器相同的帳戶中提供工作區，而不使用 `RoleConfiguration`。`RoleConfiguration` 將從抓取器中移除，並使用服務連結角色。

當您變更與抓取器相同帳戶中的工作區，並且想要繼續使用 時`RoleConfiguration`，您必須再次`RoleConfiguration`在 上提供 `UpdateScraper`。

### 為使用客戶受管金鑰啟用的工作區建立抓取器
<a name="setup-customer-managed-keys"></a>

若要建立抓取器，以使用[客戶受管金鑰](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#customer-cmk)將指標擷取至 Amazon Managed Service for Prometheus 工作區，請使用同時將來源和目標設定為相同帳戶`--role-configuration`的 。

```
aws amp create-scraper \
  --source eksConfiguration="{clusterArn='arn:aws:eks:us-west-2:account-id:cluster/xarw,subnetIds=[subnet-subnet_id]}" \
  --scrape-configuration configurationBlob=<base64-encoded-blob> \
  --destination ampConfiguration="{workspaceArn='arn:aws:aps:us-west-2:account-id:workspace/ws-workspace-id'}"\
  --role-configuration '{"sourceRoleArn":"arn:aws:iam::account_id:role/Source", "targetRoleArn":"arn:aws:iam::account_id:role/Target"}'
```

### 建立抓取器時的常見錯誤
<a name="AMP-collector-create-errors"></a>

以下是嘗試建立新的抓取器時最常見的問題。
+ 所需的 AWS 資源不存在。指定的*安全群組*、*子網路*和 *Amazon EKS 叢集*必須存在。
+ IP 地址空間不足。您必須在每個傳入 `CreateScraper` API 的子網路中至少有一個可用的 IP 地址。

## 設定 Amazon EKS 叢集
<a name="AMP-collector-eks-setup"></a>

必須設定您的 Amazon EKS 叢集以讓湊集器存取指標。此組態有兩個選項：
+ 使用 Amazon EKS *存取項目*自動提供叢集的 Amazon Managed Service for Prometheus 收集器存取權。
+ 手動設定 Amazon EKS 叢集以進行受管指標抓取。

下列主題會更詳細地說明這些主題。

### 使用存取項目為抓取器存取設定 Amazon EKS
<a name="AMP-collector-eks-access-entry-setup"></a>

使用 Amazon EKS 的存取項目是讓 Amazon Managed Service for Prometheus 存取叢集的湊集指標最簡單的方式。

您抓取的 Amazon EKS 叢集必須設定為允許 API 身分驗證。叢集身分驗證模式必須設定為 `API`或 `API_AND_CONFIG_MAP`。這可在叢集詳細資訊的**存取組態**索引標籤上的 Amazon EKS 主控台中檢視。如需詳細資訊，請參閱《[Amazon EKS 使用者指南》中的允許 IAM 角色或使用者存取 Amazon EKS 叢集上的 Kubernetes 物件](https://docs.aws.amazon.com/eks/latest/userguide/access-entries.html)。 **

您可以在建立叢集時或在建立叢集之後建立抓取器：
+ **建立叢集時** – 當您[透過 Amazon EKS 主控台建立 Amazon EKS 叢集](https://docs.aws.amazon.com/eks/latest/userguide/create-cluster.html)時，您可以設定此存取 （遵循指示來建立叢集中的抓取器），系統會自動建立存取項目政策，讓 Amazon Managed Service for Prometheus 存取叢集指標。
+ **建立叢集後新增 **- 如果您的 Amazon EKS 叢集已存在，請將身分驗證模式設定為 `API`或 `API_AND_CONFIG_MAP`，而您[透過 Amazon Managed Service for Prometheus API 或 CLI](#AMP-collector-create) 或透過 Amazon EKS 主控台建立的任何抓取器，將自動擁有為您建立的正確存取項目政策，抓取器將可存取您的叢集。

**已建立存取項目政策**

當您建立抓取器並讓 Amazon Managed Service for Prometheus 為您產生存取項目政策時，會產生下列政策。如需存取項目的詳細資訊，請參閱[《Amazon EKS 使用者指南》中的允許 IAM 角色或使用者存取 Kubernetes](https://docs.aws.amazon.com/eks/latest/userguide/access-entries.html)。 **

```
{
    "rules": [
        {
            "effect": "allow",
            "apiGroups": [
                ""
            ],
            "resources": [
                "nodes",
                "nodes/proxy",
                "nodes/metrics",
                "services",
                "endpoints",
                "pods",
                "ingresses",
                "configmaps"
            ],
            "verbs": [
                "get",
                "list",
                "watch"
            ]
        },
        {
            "effect": "allow",
            "apiGroups": [
                "extensions",
                "networking.k8s.io"
            ],
            "resources": [
                "ingresses/status",
                "ingresses"
            ],
            "verbs": [
                "get",
                "list",
                "watch"
            ]
        },
        {
            "effect": "allow",
            "apiGroups": [
                "metrics.eks.amazonaws.com"
            ],
            "resources": [
                "kcm/metrics",
                "ksh/metrics"
            ],
            "verbs": [
                "get"
            ]
        },
        {
            "effect": "allow",
            "nonResourceURLs": [
                "/metrics"
            ],
            "verbs": [
                "get"
            ]
        }
    ]
}
```

### 手動設定 Amazon EKS 以進行抓取器存取
<a name="AMP-collector-eks-manual-setup"></a>

如果您偏好使用 `aws-auth ConfigMap`來控制對 kubernetes 叢集的存取，您仍然可以讓 Amazon Managed Service for Prometheus 湊集器存取您的指標。下列步驟將授予 Amazon Managed Service for Prometheus 從您的 Amazon EKS 叢集抓取指標的存取權。

**注意**  
如需 `ConfigMap`和 存取項目的詳細資訊，請參閱《*Amazon EKS 使用者指南*》中的[允許 IAM 角色或使用者存取 Kubernetes](https://docs.aws.amazon.com/eks/latest/userguide/access-entries.html)。

此程序使用 `kubectl`和 AWS CLI。如需有關安裝 `kubectl` 的資訊，請參閱《Amazon EKS 使用者指南》**中的[安裝 kubectl](https://docs.aws.amazon.com/eks/latest/userguide/install-kubectl.html)。

**手動設定 Amazon EKS 叢集以進行受管指標抓取**

1. 使用下列內文建立名為 `clusterrole-binding.yml` 的檔案：

   ```
   apiVersion: rbac.authorization.k8s.io/v1
   kind: ClusterRole
   metadata:
     name: aps-collector-role
   rules:
     - apiGroups: [""]
       resources: ["nodes", "nodes/proxy", "nodes/metrics", "services", "endpoints", "pods", "ingresses", "configmaps"]
       verbs: ["describe", "get", "list", "watch"]
     - apiGroups: ["extensions", "networking.k8s.io"]
       resources: ["ingresses/status", "ingresses"]
       verbs: ["describe", "get", "list", "watch"]
     - nonResourceURLs: ["/metrics"]
       verbs: ["get"]
     - apiGroups: ["metrics.eks.amazonaws.com"]
       resources: ["kcm/metrics", "ksh/metrics"]
       verbs: ["get"]
   ---
   apiVersion: rbac.authorization.k8s.io/v1
   kind: ClusterRoleBinding
   metadata:
     name: aps-collector-user-role-binding
   subjects:
   - kind: User
     name: aps-collector-user
     apiGroup: rbac.authorization.k8s.io
   roleRef:
     kind: ClusterRole
     name: aps-collector-role
     apiGroup: rbac.authorization.k8s.io
   ```

1. 在叢集中執行下列命令：

   ```
   kubectl apply -f clusterrole-binding.yml
   ```

   這將建立叢集角色連結和規則。此範例使用 `aps-collector-role` 作為角色名稱和 `aps-collector-user` 作為使用者名稱。

1. 以下命令為您提供有關含有 ID 為 *scraper-id* 的資訊。這是您使用上一節命令建立的湊集器。

   ```
   aws amp describe-scraper --scraper-id scraper-id
   ```

1. 在 `describe-scraper` 的結果中尋找 `roleArn`，其格式如下：

   ```
   arn:aws:iam::account-id:role/aws-service-role/scraper.aps.amazonaws.com/AWSServiceRoleForAmazonPrometheusScraper_unique-id
   ```

   Amazon EKS 需要此 ARN 的不同格式。您必須調整傳回 ARN 的格式，以便在下一步中使用。編輯以比對此格式：

   ```
   arn:aws:iam::account-id:role/AWSServiceRoleForAmazonPrometheusScraper_unique-id
   ```

   例如，此 ARN：

   ```
   arn:aws:iam::111122223333:role/aws-service-role/scraper.aps.amazonaws.com/AWSServiceRoleForAmazonPrometheusScraper_1234abcd-56ef-7
   ```

   必須改寫為：

   ```
   arn:aws:iam::111122223333:role/AWSServiceRoleForAmazonPrometheusScraper_1234abcd-56ef-7
   ```

1. 使用上一個步驟中修改後的 `roleArn` 以及您的叢集名稱和區域，在叢集中執行下列命令：

   ```
   eksctl create iamidentitymapping --cluster cluster-name --region region-id --arn roleArn --username aps-collector-user
   ```

   這允許湊集器使用 `clusterrole-binding.yml` 檔案中建立的角色和使用者存取叢集。

## 尋找並刪除湊集器
<a name="AMP-collector-list-delete"></a>

您可以使用 AWS API 或 AWS CLI 列出您帳戶中的抓取器或刪除它們。

**注意**  
請確定您使用的是最新版本的 AWS CLI 或 SDK。最新版本為您提供最新的功能和功能，以及安全性更新。或者，使用 [AWS CloudShell](https://docs.aws.amazon.com/cloudshell/latest/userguide/welcome.html)，它會自動提供up-to-date命令列體驗。

若要列出您帳戶中的所有抓取器，請使用 [ListScrapers](https://docs.aws.amazon.com/prometheus/latest/APIReference/API_ListScrapers.html) API 操作。

或者，使用 AWS CLI呼叫：

```
aws amp list-scrapers --region aws-region
```

`ListScrapers` 會傳回您帳戶中的所有湊集器，例如：

```
{
    "scrapers": [
        {
            "scraperId": "s-1234abcd-56ef-7890-abcd-1234ef567890",
            "arn": "arn:aws:aps:us-west-2:123456789012:scraper/s-1234abcd-56ef-7890-abcd-1234ef567890",
            "roleArn": "arn:aws:iam::123456789012:role/aws-service-role/AWSServiceRoleForAmazonPrometheusScraper_1234abcd-2931",
            "status": {
                "statusCode": "DELETING"
            },
            "createdAt": "2023-10-12T15:22:19.014000-07:00",
            "lastModifiedAt": "2023-10-12T15:55:43.487000-07:00",
            "tags": {},
            "source": {
                "eksConfiguration": {
                    "clusterArn": "arn:aws:eks:us-west-2:123456789012:cluster/my-cluster",
                    "securityGroupIds": [
                        "sg-1234abcd5678ef90"
                    ],
                    "subnetIds": [
                        "subnet-abcd1234ef567890", 
                        "subnet-1234abcd5678ab90"
                    ]
                }
            },
            "destination": {
                "ampConfiguration": {
                    "workspaceArn": "arn:aws:aps:us-west-2:123456789012:workspace/ws-1234abcd-5678-ef90-ab12-cdef3456a78"
                }
            }
        }
    ]
}
```

若要刪除抓取器，請使用 `ListScrapers`操作尋找您要刪除之抓取器`scraperId`的 ，然後使用 [DeleteScraper](https://docs.aws.amazon.com/prometheus/latest/APIReference/API_DeleteScraper.html) 操作將其刪除。

或者，使用 AWS CLI呼叫：

```
aws amp delete-scraper --scraper-id scraperId
```

## 湊集器組態
<a name="AMP-collector-configuration"></a>

您可以使用與 Prometheus 相容的湊集器組態控制湊集器如何探索和收集指標。例如，您可以變更將指標傳送至工作區的間隔。您也可以使用重新標籤來動態重新寫入指標的標籤。湊集器組態是一個 YAML 檔案，屬於湊集器定義的一部份。

建立新的湊集器時，您可以透過在 API 呼叫中提供 base64 編碼的 YAML 檔案來指定組態。您可以透過 Amazon Managed Service for Prometheus API 中的 `GetDefaultScraperConfiguration` 作業下載一般用途組態檔案。

若要修改抓取器的組態，您可以使用 `UpdateScraper`操作。如果您需要更新指標的來源 （例如，其他 Amazon EKS 叢集），您必須刪除抓取器，並使用新來源重新建立該抓取器。

**支援的組態**

如需抓取器組態格式的相關資訊，包括可能值的詳細明細，請參閱 Prometheus 文件中的[組態](https://prometheus.io/docs/prometheus/latest/configuration/configuration/)。全域組態選項和 `<scrape_config>` 選項說明最常需要的選項。

由於 Amazon EKS 是唯一支援的服務，因此支援的唯一服務探索組態 (`<*_sd_config>`) 是 `<kubernetes_sd_config>`。

允許組態區段的完整清單：
+ `<global>`
+ `<scrape_config>`
+ `<static_config>`
+ `<relabel_config>`
+ `<metric_relabel_configs>`
+ `<kubernetes_sd_config>`

這些區段中的限制會列在範例組態檔案之後。

**範例組態檔案**

以下是具有 30 秒湊集間隔的範例 YAML 組態檔。此範例包含對 kube API 伺服器指標，以及 kube-controller-manager 和 kube-scheduler 指標的支援。如需詳細資訊，請參閱《*Amazon EKS 使用者指南*》中的[擷取 Prometheus 格式的控制平面原始指標](https://docs.aws.amazon.com/eks/latest/userguide/view-raw-metrics.html#scheduler-controller-metrics)。

```
global:
   scrape_interval: 30s
   external_labels:
     clusterArn: apiserver-test-2
scrape_configs:
  - job_name: pod_exporter
    kubernetes_sd_configs:
      - role: pod
  - job_name: cadvisor
    scheme: https
    authorization:
      type: Bearer
      credentials_file: /var/run/secrets/kubernetes.io/serviceaccount/token
    kubernetes_sd_configs:
      - role: node
    relabel_configs:
      - action: labelmap
        regex: __meta_kubernetes_node_label_(.+)
      - replacement: kubernetes.default.svc:443
        target_label: __address__
      - source_labels: [__meta_kubernetes_node_name]
        regex: (.+)
        target_label: __metrics_path__
        replacement: /api/v1/nodes/$1/proxy/metrics/cadvisor
  # apiserver metrics
  - scheme: https
    authorization:
      type: Bearer
      credentials_file: /var/run/secrets/kubernetes.io/serviceaccount/token
    job_name: kubernetes-apiservers
    kubernetes_sd_configs:
    - role: endpoints
    relabel_configs:
    - action: keep
      regex: default;kubernetes;https
      source_labels:
      - __meta_kubernetes_namespace
      - __meta_kubernetes_service_name
      - __meta_kubernetes_endpoint_port_name
  # kube proxy metrics
  - job_name: kube-proxy
    honor_labels: true
    kubernetes_sd_configs:
    - role: pod
    relabel_configs:
    - action: keep
      source_labels:
      - __meta_kubernetes_namespace
      - __meta_kubernetes_pod_name
      separator: '/'
      regex: 'kube-system/kube-proxy.+'
    - source_labels:
      - __address__
      action: replace
      target_label: __address__
      regex: (.+?)(\\:\\d+)?
      replacement: $1:10249
  # Scheduler metrics
  - job_name: 'ksh-metrics'
    kubernetes_sd_configs:
    - role: endpoints
    metrics_path: /apis/metrics.eks.amazonaws.com/v1/ksh/container/metrics
    scheme: https
    bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
    relabel_configs:
    - source_labels:
      - __meta_kubernetes_namespace
      - __meta_kubernetes_service_name
      - __meta_kubernetes_endpoint_port_name
      action: keep
      regex: default;kubernetes;https
  # Controller Manager metrics
  - job_name: 'kcm-metrics'
    kubernetes_sd_configs:
    - role: endpoints
    metrics_path: /apis/metrics.eks.amazonaws.com/v1/kcm/container/metrics
    scheme: https
    bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
    relabel_configs:
    - source_labels:
      - __meta_kubernetes_namespace
      - __meta_kubernetes_service_name
      - __meta_kubernetes_endpoint_port_name
      action: keep
      regex: default;kubernetes;https
```

以下是 AWS 受管收集器特有的限制：
+ **湊集間隔**：湊集器組態無法指定少於 30 秒的湊集間隔。
+ **目標**：`static_config` 中的目標必須指定為 IP 地址。
+ **DNS 解析** – 與目標名稱相關，此組態中辨識的唯一伺服器名稱是 Kubernetes api 伺服器 `kubernetes.default.svc`。所有其他機器名稱都必須由 IP 地址指定。
+ **授權** – 如果不需要授權，請省略 。如果需要，授權必須是 `Bearer`，而且必須指向檔案 `/var/run/secrets/kubernetes.io/serviceaccount/token`。換句話說，如果使用 ，授權區段必須如下所示：

  ```
      authorization:
        type: Bearer
        credentials_file: /var/run/secrets/kubernetes.io/serviceaccount/token
  ```
**注意**  
`type: Bearer` 是預設值，因此可以省略。

## 對湊集器組態進行移難排解
<a name="AMP-collector-troubleshoot"></a>

Amazon Managed Service for Prometheus 收集器會自動探索和湊集指標。但是，若未在 Amazon Managed Service for Prometheus 工作區中看到您希望看到的指標，該如何進行疑難排解？

**重要**  
確認已啟用 Amazon EKS 叢集的私有存取。如需詳細資訊，請參閱《*Amazon EKS 使用者指南*》中的[叢集私有端點](https://docs.aws.amazon.com/eks/latest/userguide/cluster-endpoint.html#cluster-endpoint-private)。

`up` 指標是一個有助益的工具。針對 Amazon Managed Service for Prometheus 收集器探索的每個端點，皆會自動分配此指標。此指標有三種狀態，可協助您疑難排解收集器內所發生的事情。
+ `up` 不存在 - 若端點沒有 `up` 指標，則這表示收集器找不到端點。

  如果您確定端點存在，收集器可能無法找到它有幾個原因。
  + 您可能需要調整湊集組態。探索`relabel_config`可能需要調整。
  + `role` 用於探索的 可能有問題。
  + Amazon EKS 叢集使用的 Amazon VPC 可能尚未[啟用 DNS](https://docs.aws.amazon.com/vpc/latest/userguide/AmazonDNS-concepts.html)，這會使收集器無法尋找端點。
+ `up` 已存在，但始終為 0 — 如果 `up` 已存在但為 0，則收集器能夠探索端點，但找不到任何與 Prometheus 相容的指標。

  在這種情況下，您可以嘗試直接對端點使用 `curl` 命令。您可以驗證詳細資訊是否正確，例如，您正在使用的通訊協定 (`http` 或 `https`)、端點或連接埠。您也可以檢查端點是否以有效的`200`回應回應，並遵循 Prometheus 格式。最後，回應的內文不能大於允許的大小上限。（如需 AWS 受管收集器的限制，請參閱下一節。)
+ `up` 已存在且大於 0 — 若 `up` 已存在且大於 0，則指標會傳送至 Amazon Managed Service for Prometheus。

  驗證您正在尋找 Amazon Managed Service for Prometheus (或您的替代儀表板，例如 Amazon Managed Grafana) 中的正確指標。您可以再次使用 curl 來檢查`/metrics`端點中的預期數據。同時檢查您是否未超過其他限制，例如每個湊集器的端點數量。您可以使用 檢查指標計數，以檢查要抓取的`up`指標端點數量`count(up)`。

## 湊集器限制
<a name="AMP-collector-limits"></a>

Amazon Managed Service for Prometheus 所提供全受管湊集器的限制較少。
+ **區域**：您的 EKS 叢集、受管理湊集器和 Amazon Managed Service for Prometheus workspace 必須位於相同 AWS 區域。
+ **收集器**：各帳戶最多可以為每個區域提供 10 個 Amazon Managed Service for Prometheus 湊集器。
**注意**  
您可以透過[請求增加配額](https://console.aws.amazon.com/support/home#/case/create?issueType=service-limit-increase)來請求增加此限制。
+ **指標回應**：來自任何一個 `/metrics` 端點請求的回應主體不能超過 50 MB。
+ **每個湊集器的端點**：湊集器最多可以湊集 30,000 `/metrics` 個端點。
+ **湊集間隔**：湊集器組態無法指定少於 30 秒的湊集間隔。

# 設定 Amazon MSK 的受管 Prometheus 收集器
<a name="prom-msk-integration"></a>

若要使用 Amazon Managed Service for Prometheus 收集器，您可以建立抓取器，在 Amazon Managed Streaming for Apache Kafka 叢集中探索和提取指標。您也可以建立與 Amazon Elastic Kubernetes Service 整合的抓取器。如需詳細資訊，請參閱[整合 Amazon EKS](https://docs.aws.amazon.com/prometheus/latest/userguide/AMP-collector-how-to.html)。

## 建立湊集器
<a name="prom-msk-create-scraper"></a>

Amazon Managed Service for Prometheus 收集器包含一個抓取器，可從 Amazon MSK 叢集探索和收集指標。Amazon Managed Service for Prometheus 可為您管理湊集器，提供所需的可擴展性、安全性和可靠性，而無需自行管理任何執行個體、代理程式或湊集器。

您可以使用 AWS API 或 建立抓取器 AWS CLI ，如下列程序所述。

您需先滿足幾項先決條件，才能建立自己的湊集器：
+ 您必須建立 Amazon MSK 叢集。
+ 設定 Amazon MSK 叢集的安全群組，以允許 Amazon VPC 內連接埠 **11001 (JMX Exporter)** 和 **11002 (Node Exporter)** 的傳入流量，因為抓取器需要存取這些 DNS 記錄才能收集 Prometheus 指標。
+ Amazon MSK 叢集所在的 Amazon VPC 必須[啟用 DNS](https://docs.aws.amazon.com/vpc/latest/userguide/AmazonDNS-concepts.html)。

**注意**  
叢集會依其 Amazon 資源名稱 (ARN) 與抓取器建立關聯。如果您刪除叢集，然後使用相同名稱建立新的叢集，則 ARN 將重複使用於新叢集。因此，抓取器會嘗試收集新叢集的指標。[您可以將抓取器](#prom-msk-delete-scraper)與刪除叢集分開刪除。

------
#### [ To create a scraper using the AWS API ]

使用 `CreateScraper` API 操作來建立具有 AWS API 的抓取器。下列範例在美國東部 （維吉尼亞北部） 區域建立抓取器。將*範例*內容取代為您的 Amazon MSK 叢集資訊，並提供您的抓取器組態。

**注意**  
設定安全群組和子網路以符合您的目標叢集。在兩個可用區域中包含至少兩個子網路。

```
                POST /scrapers HTTP/1.1
Content-Length: 415 
Authorization: AUTHPARAMS
X-Amz-Date: 20201201T193725Z
User-Agent: aws-cli/1.18.147 Python/2.7.18 Linux/5.4.58-37.125.amzn2int.x86_64 botocore/1.18.6

{
    "alias": "myScraper",
    "destination":  {
        "ampConfiguration": {
            "workspaceArn": "arn:aws:aps:us-east-1:123456789012:workspace/ws-workspace-id"
        }
    },
    "source": {
        "vpcConfiguration": {
            "securityGroupIds": ["sg-security-group-id"],
            "subnetIds": ["subnet-subnet-id-1", "subnet-subnet-id-2"]
        }
    },
    "scrapeConfiguration": {
        "configurationBlob": base64-encoded-blob
    }
}
```

在此範例中， `scrapeConfiguration` 參數需要 base64 編碼的 Prometheus 組態 YAML 檔案，指定 MSK 叢集的 DNS 記錄。

每個 DNS 記錄代表特定可用區域中的中介裝置端點，可讓用戶端連線到分散在所選AZs中介裝置，以獲得高可用性。

MSK 叢集屬性中的 DNS 記錄數目對應至叢集組態中的代理程式節點和可用區域數目：
+ **預設組態** – 3 個 AZs的 3 個代理節點 = 3 個 DNS 記錄
+ **自訂組態** – 2 個 AZs的 2 個代理節點 = 2 個 DNS 記錄

若要取得 MSK 叢集的 DNS 記錄，請開啟 MSK 主控台，網址為 https：//[https://console.aws.amazon.com/msk/home?region=us-east-1\$1/home/](https://console.aws.amazon.com/msk/home?region=us-east-1#/home/)。前往 MSK 叢集。選擇**屬性**、**中介裝置**和**端點**。

您有兩個設定 Prometheus 以從 MSK 叢集抓取指標的選項：

1. **叢集層級 DNS 解析 （建議）** – 使用叢集的基本 DNS 名稱自動探索所有代理程式。如果您的中介裝置端點是 `b-1.clusterName.xxx.xxx.xxx`，請使用 `clusterName.xxx.xxx.xxx`做為 DNS 記錄。這可讓 Prometheus 自動抓取叢集中的所有代理程式。

   **個別代理程式端點** – 個別指定每個代理程式端點以進行精細控制。在組態中使用完整的代理程式識別符 (b-1、b-2)。例如：

   ```
   dns_sd_configs:
     - names:
       - b-1.clusterName.xxx.xxx.xxx
       - b-2.clusterName.xxx.xxx.xxx  
       - b-3.clusterName.xxx.xxx.xxx
   ```

**注意**  
從 AWS 主控台將 取代`clusterName.xxx.xxx.xxx`為您實際的 MSK 叢集端點。

如需詳細資訊，請參閱 *Prometheus* 文件中的 [<dns\$1sd\$1config>](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#dns_sd_config)。

以下是抓取器組態檔案的範例：

```
global:
  scrape_interval: 30s
  external_labels:
    clusterArn: msk-test-1

scrape_configs:
  - job_name: msk-jmx
    scheme: http
    metrics_path: /metrics
    scrape_timeout: 10s
    dns_sd_configs:
      - names:
          - dns-record-1
          - dns-record-2
          - dns-record-3
        type: A
        port: 11001
    relabel_configs:
      - source_labels: [__meta_dns_name]
        target_label: broker_dns
      - source_labels: [__address__]
        target_label: instance
        regex: '(.*)'
        replacement: '${1}'

  - job_name: msk-node
    scheme: http
    metrics_path: /metrics
    scrape_timeout: 10s
    dns_sd_configs:
      - names:
          - dns-record-1
          - dns-record-2
          - dns-record-3
        type: A
        port: 11002
    relabel_configs:
      - source_labels: [__meta_dns_name]
        target_label: broker_dns
      - source_labels: [__address__]
        target_label: instance
        regex: '(.*)'
        replacement: '${1}'
```

執行下列其中一個命令，將 YAML 檔案轉換為 base64。您也可以使用任何線上 base64 轉換器來轉換檔案。

**Example Linux/macOS**  

```
echo -n scraper config updated with dns records | base64 
```

**Example Windows PowerShell**  

```
[Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(scraper config updated with dns records))
```

------
#### [ To create a scraper using the AWS CLI ]

使用 `create-scraper`命令來使用 建立抓取器 AWS Command Line Interface。下列範例在美國東部 （維吉尼亞北部） 區域建立抓取器。將*範例*內容取代為您的 Amazon MSK 叢集資訊，並提供您的抓取器組態。

**注意**  
設定安全群組和子網路以符合您的目標叢集。在兩個可用區域中包含至少兩個子網路。

```
aws amp create-scraper \
 --source vpcConfiguration="{securityGroupIds=['sg-security-group-id'],subnetIds=['subnet-subnet-id-1', 'subnet-subnet-id-2']}" \ 
--scrape-configuration configurationBlob=base64-encoded-blob \
 --destination ampConfiguration="{workspaceArn='arn:aws:aps:us-west-2:123456789012:workspace/ws-workspace-id'}"
```

------
+ 以下是您可以搭配 AWS API 使用的湊集器操作完整清單：

  使用 [CreateScraper](https://docs.aws.amazon.com/prometheus/latest/APIReference/API_CreateScraper.html) API 操作建立抓取器。
+ 使用 [ListScrapers](https://docs.aws.amazon.com/prometheus/latest/APIReference/API_ListScrapers.html) API 操作列出現有的抓取器。
+ 使用 [UpdateScraper](https://docs.aws.amazon.com/prometheus/latest/APIReference/API_UpdateScraper.html) API 操作更新抓取器的別名、組態或目的地。
+ 使用 [DeleteScraper](https://docs.aws.amazon.com/prometheus/latest/APIReference/API_DeleteScraper.html) API 操作刪除抓取器。
+ 使用 [DescribeScraper](https://docs.aws.amazon.com/prometheus/latest/APIReference/API_DescribeScraper.html) API 操作取得有關抓取器的詳細資訊。

## 跨帳戶設定
<a name="prom-msk-cross-account"></a>

若要在您要收集指標的 Amazon MSK 叢集位於與 Amazon Managed Service for Prometheus 收集器不同的帳戶中時，在跨帳戶設定中建立抓取器，請使用下列程序。

例如，當您有兩個帳戶時，Amazon MSK `account_id_source` 所在的第一個來源帳戶，以及 `account_id_target` Amazon Managed Service for Prometheus 工作區所在的第二個目標帳戶。

**在跨帳戶設定中建立抓取器**

1. 在來源帳戶中，建立角色`arn:aws:iam::111122223333:role/Source`並新增下列信任政策。

   ```
   {
       "Effect": "Allow",
       "Principal": {
       "Service": [
           "scraper.aps.amazonaws.com"
        ]
       },
       "Action": "sts:AssumeRole",
       "Condition": {
           "ArnEquals": {
               "aws:SourceArn": "arn:aws:aps:aws-region:111122223333:scraper/scraper-id"
           },
           "StringEquals": {
               "AWS:SourceAccount": "111122223333"
           }
       }
   }
   ```

1. 在來源 (Amazon MSK 叢集） 和目標 (Amazon Managed Service for Prometheus 工作區） 的每個組合上，您需要建立角色，`arn:aws:iam::444455556666:role/Target`並新增具有 [AmazonPrometheusRemoteWriteAccess](https://docs.aws.amazon.com/prometheus/latest/userguide/security-iam-awsmanpol.html) 許可的下列信任政策。

   ```
   {
     "Effect": "Allow",
     "Principal": {
        "AWS": "arn:aws:iam::111122223333:role/Source"
     },
     "Action": "sts:AssumeRole",
     "Condition": {
        "StringEquals": {
           "sts:ExternalId": "arn:aws:aps:aws-region:111122223333:scraper/scraper-id"
         }
     }
   }
   ```

1. 使用 `--role-configuration`選項建立抓取器。

   ```
   aws amp create-scraper \ --source vpcConfiguration="{subnetIds=[subnet-subnet-id], "securityGroupIds": ["sg-security-group-id"]}" \ --scrape-configuration configurationBlob=<base64-encoded-blob> \ --destination ampConfiguration="{workspaceArn='arn:aws:aps:aws-region:444455556666:workspace/ws-workspace-id'}"\ --role-configuration '{"sourceRoleArn":"arn:aws:iam::111122223333:role/Source", "targetRoleArn":"arn:aws:iam::444455556666:role/Target"}'
   ```

1. 驗證抓取器建立。

   ```
   aws amp list-scrapers
   {
       "scrapers": [
           {
               "scraperId": "s-example123456789abcdef0",
               "arn": "arn:aws:aps:aws-region:111122223333:scraper/s-example123456789abcdef0": "arn:aws:iam::111122223333:role/Source",
               "status": "ACTIVE",
               "creationTime": "2025-10-27T18:45:00.000Z",
               "lastModificationTime": "2025-10-27T18:50:00.000Z",
               "tags": {},
               "statusReason": "Scraper is running successfully",
               "source": {
                   "vpcConfiguration": {
                       "subnetIds": ["subnet-subnet-id"],
                       "securityGroupIds": ["sg-security-group-id"]
                   }
               },
               "destination": {
                   "ampConfiguration": {
                       "workspaceArn": "arn:aws:aps:aws-region:444455556666:workspace/ws-workspace-id'"
                   }
               },
               "scrapeConfiguration": {
                   "configurationBlob": "<base64-encoded-blob>"
               }
           }
       ]
   }
   ```

## 在 RoleConfiguration 和服務連結角色之間變更
<a name="prom-msk-changing-roles"></a>

當您想要切換回服務連結角色，而不是 `RoleConfiguration`寫入 Amazon Managed Service for Prometheus 工作區時，您必須更新 ，`UpdateScraper`並在與抓取器相同的帳戶中提供工作區，而不需要 `RoleConfiguration`。`RoleConfiguration` 會從抓取器中移除 ，並使用服務連結角色。

當您變更與抓取器相同帳戶中的工作區，並且想要繼續使用 時`RoleConfiguration`，您必須再次`RoleConfiguration`在 上提供 `UpdateScraper`。

## 尋找並刪除湊集器
<a name="prom-msk-delete-scraper"></a>

您可以使用 AWS API 或 AWS CLI 列出您帳戶中的抓取器或刪除它們。

**注意**  
請確定您使用的是最新版本的 AWS CLI 或 SDK。最新版本提供最新的功能和功能，以及安全性更新。或者，使用 [AWS CloudShell](https://docs.aws.amazon.com/cloudshell/latest/userguide/welcome.html)，它會自動提供up-to-date命令列體驗。

若要列出您帳戶中的所有抓取器，請使用 [ListScrapers](https://docs.aws.amazon.com/prometheus/latest/APIReference/API_ListScrapers.html) API 操作。

或者，使用 AWS CLI呼叫：

```
aws amp list-scrapers
```

`ListScrapers` 會傳回您帳戶中的所有湊集器，例如：

```
{
    "scrapers": [
        {
            "scraperId": "s-1234abcd-56ef-7890-abcd-1234ef567890",
            "arn": "arn:aws:aps:aws-region:123456789012:scraper/s-1234abcd-56ef-7890-abcd-1234ef567890",
            "roleArn": "arn:aws:iam::123456789012:role/aws-service-role/AWSServiceRoleForAmazonPrometheusScraper_1234abcd-2931",
            "status": {
                "statusCode": "DELETING"
            },
            "createdAt": "2023-10-12T15:22:19.014000-07:00",
            "lastModifiedAt": "2023-10-12T15:55:43.487000-07:00",
            "tags": {},
            "source": {
                "vpcConfiguration": {
                   "securityGroupIds": [
                        "sg-1234abcd5678ef90"
                    ],
                    "subnetIds": [
                        "subnet-abcd1234ef567890", 
                        "subnet-1234abcd5678ab90"
                    ]
                }
            },
            "destination": {
                "ampConfiguration": {
                    "workspaceArn": "arn:aws:aps:aws-region:123456789012:workspace/ws-1234abcd-5678-ef90-ab12-cdef3456a78"
                }
            }
        }
    ]
}
```

若要刪除抓取器，請使用 `ListScrapers`操作尋找您要刪除之抓取器`scraperId`的 ，然後使用 [DeleteScraper](https://docs.aws.amazon.com/prometheus/latest/APIReference/API_DeleteScraper.html) 操作將其刪除。

或者，使用 AWS CLI呼叫：

```
aws amp delete-scraper --scraper-id scraperId
```

## 從 Amazon MSK 收集的指標
<a name="prom-msk-metrics"></a>

當您與 Amazon MSK 整合時，Amazon Managed Service for Prometheus 收集器會自動抓取下列指標：

### 指標：jmx\$1exporter 和 pod\$1exporter 任務
<a name="broker-metrics"></a>


| 指標 | 描述/目的 | 
| --- | --- | 
|  jmx\$1config\$1reload\$1failure\$1total  |  JMX 匯出工具無法重新載入其組態檔案的總次數。  | 
|  jmx\$1scrape\$1duration\$1seconds  |  目前收集週期的抓取 JMX 指標所花費的時間，以秒為單位。  | 
|  jmx\$1scrape\$1error  |  指出在 JMX 指標抓取期間是否發生錯誤 (1 = 錯誤，0 = 成功）。  | 
|  java\$1lang\$1Memory\$1HeapMemoryUsage\$1used  |  JVM 目前使用的堆積記憶體數量 （以位元組為單位）。  | 
|  java\$1lang\$1Memory\$1HeapMemoryUsage\$1max  |  可用於記憶體管理的堆積記憶體數量上限 （以位元組為單位）。  | 
|  java\$1lang\$1Memory\$1NonHeapMemoryUsage\$1used  |  JVM 目前使用的非堆積記憶體數量 （以位元組為單位）。  | 
|  kafka\$1cluster\$1Partition\$1Value  |  與 Kafka 叢集分割區相關的目前狀態或值，依分割區 ID 和主題細分。  | 
|  kafka\$1consumer\$1consumer\$1coordinator\$1metrics\$1assigned\$1partitions  |  目前指派給此取用者的分割區數量。  | 
|  kafka\$1consumer\$1consumer\$1coordinator\$1metrics\$1commit\$1latency\$1avg  |  遞交偏移所需的平均時間，以毫秒為單位。  | 
|  kafka\$1consumer\$1consumer\$1coordinator\$1metrics\$1commit\$1rate  |  每秒偏移遞交的數量。  | 
|  kafka\$1consumer\$1consumer\$1coordinator\$1metrics\$1failed\$1rebalance\$1total  |  失敗的取用者群組重新平衡總數。  | 
|  kafka\$1consumer\$1consumer\$1coordinator\$1metrics\$1last\$1heartbeat\$1seconds\$1ago  |  自上次將活動訊號傳送至協調器以來的秒數。  | 
|  kafka\$1consumer\$1consumer\$1coordinator\$1metrics\$1rebalance\$1latency\$1avg  |  取用者群組重新平衡的平均時間，以毫秒為單位。  | 
|  kafka\$1consumer\$1consumer\$1coordinator\$1metrics\$1rebalance\$1total  |  取用者群組重新平衡的總數。  | 
|  kafka\$1consumer\$1consumer\$1fetch\$1manager\$1metrics\$1bytes\$1consumed\$1rate  |  消費者每秒耗用的平均位元組數。  | 
|  kafka\$1consumer\$1consumer\$1fetch\$1manager\$1metrics\$1fetch\$1latency\$1avg  |  擷取請求所需的平均時間，以毫秒為單位。  | 
|  kafka\$1consumer\$1consumer\$1fetch\$1manager\$1metrics\$1fetch\$1rate  |  每秒擷取請求的數量。  | 
|  kafka\$1consumer\$1consumer\$1fetch\$1manager\$1metrics\$1records\$1consumed\$1rate  |  每秒耗用的平均記錄數。  | 
|  kafka\$1consumer\$1consumer\$1fetch\$1manager\$1metrics\$1records\$1lag\$1max  |  此取用者中任何分割區的記錄數量的最大延遲。  | 
|  kafka\$1consumer\$1consumer\$1metrics\$1connection\$1count  |  目前作用中連線的數量。  | 
|  kafka\$1consumer\$1consumer\$1metrics\$1incoming\$1byte\$1rate  |  每秒從所有伺服器接收的平均位元組數。  | 
|  kafka\$1consumer\$1consumer\$1metrics\$1last\$1poll\$1seconds\$1ago  |  自上次消費者輪詢 () 呼叫以來的秒數。  | 
|  kafka\$1consumer\$1consumer\$1metrics\$1request\$1rate  |  每秒傳送的請求數。  | 
|  kafka\$1consumer\$1consumer\$1metrics\$1response\$1rate  |  每秒收到的回應數。  | 
|  kafka\$1consumer\$1group\$1ConsumerLagMetrics\$1Value  |  取用者群組的目前取用者延遲值，指出取用者落後多遠。  | 
|  kafka\$1controller\$1KafkaController\$1Value  |  Kafka 控制器的目前狀態或值 (1 = 作用中控制器，0 = 非作用中）。  | 
|  kafka\$1controller\$1ControllerEventManager\$1Count  |  處理的控制器事件總數。  | 
|  kafka\$1controller\$1ControllerEventManager\$1Mean  |  處理控制器事件所需的平均 （平均） 時間。  | 
|  kafka\$1controller\$1ControllerStats\$1MeanRate  |  每秒控制器統計資料操作的平均速率。  | 
|  kafka\$1coordinator\$1group\$1GroupMetadataManager\$1Value  |  取用者群組之群組中繼資料管理員的目前狀態或值。  | 
|  kafka\$1log\$1LogFlushStats\$1Count  |  日誌排清操作的總數。  | 
|  kafka\$1log\$1LogFlushStats\$1Mean  |  日誌排清操作的平均 （平均） 時間。  | 
|  kafka\$1log\$1LogFlushStats\$1MeanRate  |  每秒日誌排清操作的平均速率。  | 
|  kafka\$1network\$1RequestMetrics\$1Count  |  已處理的網路請求總數。  | 
|  kafka\$1network\$1RequestMetrics\$1Mean  |  處理網路請求所需的平均 （平均） 時間。  | 
|  kafka\$1network\$1RequestMetrics\$1MeanRate  |  每秒網路請求的平均速率。  | 
|  kafka\$1network\$1Acceptor\$1MeanRate  |  每秒可接受連線的平均速率。  | 
|  kafka\$1server\$1Fetch\$1queue\$1size  |  擷取請求佇列的目前大小。  | 
|  kafka\$1server\$1Produce\$1queue\$1size  |  生產請求佇列的目前大小。  | 
|  kafka\$1server\$1Request\$1queue\$1size  |  一般請求佇列的目前大小。  | 
|  kafka\$1server\$1BrokerTopicMetrics\$1Count  |  中介裝置主題操作的總數 （訊息輸入/輸出、位元組輸入/輸出）。  | 
|  kafka\$1server\$1BrokerTopicMetrics\$1MeanRate  |  每秒中介裝置主題操作的平均速率。  | 
|  kafka\$1server\$1BrokerTopicMetrics\$1OneMinuteRate  |  中介裝置主題操作的一分鐘移動平均速率。  | 
|  kafka\$1server\$1DelayedOperationPurgatory\$1Value  |  查詢中的目前延遲操作數量 （等待完成）。  | 
|  kafka\$1server\$1DelayedFetchMetrics\$1MeanRate  |  每秒延遲擷取操作的平均速率。  | 
|  kafka\$1server\$1FetcherLagMetrics\$1Value  |  複本擷取器執行緒的目前延遲值 （落後領導者多遠）。  | 
|  kafka\$1server\$1FetcherStats\$1MeanRate  |  每秒擷取器操作的平均速率。  | 
|  kafka\$1server\$1ReplicaManager\$1Value  |  複本管理員的目前狀態或值。  | 
|  kafka\$1server\$1ReplicaManager\$1MeanRate  |  每秒複本管理員操作的平均速率。  | 
|  kafka\$1server\$1LeaderReplication\$1byte\$1rate  |  對於此代理程式為領導者的分割區，每秒複寫的位元組速率。  | 
|  kafka\$1server\$1group\$1coordinator\$1metrics\$1group\$1completed\$1rebalance\$1count  |  已完成取用者群組重新平衡的總數。  | 
|  kafka\$1server\$1group\$1coordinator\$1metrics\$1offset\$1commit\$1count  |  偏移遞交操作的總數。  | 
|  kafka\$1server\$1group\$1coordinator\$1metrics\$1offset\$1commit\$1rate  |  每秒偏移遞交操作的速率。  | 
|  kafka\$1server\$1socket\$1server\$1metrics\$1connection\$1count  |  目前作用中連線的數量。  | 
|  kafka\$1server\$1socket\$1server\$1metrics\$1connection\$1creation\$1rate  |  每秒建立新連線的速率。  | 
|  kafka\$1server\$1socket\$1server\$1metrics\$1connection\$1close\$1rate  |  每秒連線關閉速率。  | 
|  kafka\$1server\$1socket\$1server\$1metrics\$1failed\$1authentication\$1total  |  失敗的身分驗證嘗試總數。  | 
|  kafka\$1server\$1socket\$1server\$1metrics\$1incoming\$1byte\$1rate  |  每秒傳入位元組的速率。  | 
|  kafka\$1server\$1socket\$1server\$1metrics\$1outgoing\$1byte\$1rate  |  每秒傳出位元組的速率。  | 
|  kafka\$1server\$1socket\$1server\$1metrics\$1request\$1rate  |  每秒請求率。  | 
|  kafka\$1server\$1socket\$1server\$1metrics\$1response\$1rate  |  每秒回應速率。  | 
|  kafka\$1server\$1socket\$1server\$1metrics\$1network\$1io\$1rate  |  每秒的網路 I/O 操作速率。  | 
|  kafka\$1server\$1socket\$1server\$1metrics\$1io\$1ratio  |  在 I/O 操作中花費的時間部分。  | 
|  kafka\$1server\$1controller\$1channel\$1metrics\$1connection\$1count  |  控制器通道的目前作用中連線數。  | 
|  kafka\$1server\$1controller\$1channel\$1metrics\$1incoming\$1byte\$1rate  |  控制器通道每秒傳入位元組的速率。  | 
|  kafka\$1server\$1controller\$1channel\$1metrics\$1outgoing\$1byte\$1rate  |  控制器通道每秒傳出位元組的速率。  | 
|  kafka\$1server\$1controller\$1channel\$1metrics\$1request\$1rate  |  控制器通道每秒請求的速率。  | 
|  kafka\$1server\$1replica\$1fetcher\$1metrics\$1connection\$1count  |  複本擷取器的目前作用中連線數。  | 
|  kafka\$1server\$1replica\$1fetcher\$1metrics\$1incoming\$1byte\$1rate  |  複本擷取器每秒傳入位元組的速率。  | 
|  kafka\$1server\$1replica\$1fetcher\$1metrics\$1request\$1rate  |  複本擷取器的每秒請求率。  | 
|  kafka\$1server\$1replica\$1fetcher\$1metrics\$1failed\$1authentication\$1total  |  複本擷取器的失敗身分驗證嘗試總數。  | 
|  kafka\$1server\$1ZooKeeperClientMetrics\$1Count  |  ZooKeeper 用戶端操作的總計數。  | 
|  kafka\$1server\$1ZooKeeperClientMetrics\$1Mean  |  ZooKeeper 用戶端操作的平均延遲。  | 
|  kafka\$1server\$1KafkaServer\$1Value  |  Kafka 伺服器的目前狀態或值 （通常表示伺服器正在執行）。  | 
|  node\$1cpu\$1seconds\$1total  |  CPUs花費的總秒數 （使用者、系統、閒置等），依 CPU 和模式細分。  | 
|  node\$1disk\$1read\$1bytes\$1total  |  從磁碟成功讀取的位元組總數，依裝置細分。  | 
|  node\$1disk\$1reads\$1completed\$1total  |  磁碟成功完成的讀取總數，依裝置細分。  | 
|  node\$1disk\$1writes\$1completed\$1total  |  磁碟成功完成的寫入總數，依裝置細分。  | 
|  node\$1disk\$1written\$1bytes\$1total  |  成功寫入磁碟的位元組總數，依裝置細分。  | 
|  node\$1filesystem\$1avail\$1bytes  |  非根使用者可用的檔案系統空間，以位元組為單位，依裝置和掛載點細分。  | 
|  node\$1filesystem\$1size\$1bytes  |  檔案系統的總大小，以位元組為單位，依裝置和掛載點細分。  | 
|  node\$1filesystem\$1free\$1bytes  |  以位元組為單位的可用檔案系統空間，依裝置和掛載點細分。  | 
|  node\$1filesystem\$1files  |  檔案系統上的檔案節點 (inodes) 總數，依裝置和掛載點細分。  | 
|  node\$1filesystem\$1files\$1free  |  檔案系統上可用檔案節點 (inodes) 的數量，依裝置和掛載點細分。  | 
|  node\$1filesystem\$1readonly  |  指出檔案系統是否已掛載唯讀 (1 = 唯讀，0 = 讀寫）。  | 
|  node\$1filesystem\$1device\$1error  |  指出取得檔案系統統計資料時發生錯誤 (1 = 錯誤，0 = 成功）。  | 

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

目前與 Amazon Managed Service for Prometheus 整合的 Amazon MSK 有下列限制：
+ 僅支援 Amazon MSK 佈建叢集 （不適用於 Amazon MSK Serverless)
+ 不支援搭配 KRaft 中繼資料模式啟用公有存取的 Amazon MSK 叢集
+ Amazon MSK Express 代理程式不支援
+ 目前支援 Amazon MSK 叢集與 Amazon Managed Service for Prometheus 收集器/工作空間之間的 1：1 映射

# 什麼是與 Prometheus 相容的指標？
<a name="prom-compatible-metrics"></a>

若要從您的應用程式和基礎設施中湊集 Prometheus 指標以用於 Amazon Managed Service for Prometheus，他們必須從與 Prometheus 相容的 `/metrics` 個端點中檢測並公開*與 Prometheus 相容的指標*。您可以建置自己的指標，但不必這樣做。Kubernetes (包括 Amazon EKS) 和許多其他程式庫和服務會直接建置這些指標。

將 Amazon EKS 中的指標匯出到與 Prometheus 相容的端點時，您可以讓 Amazon Managed Service for Prometheus 收集器自動湊集這些指標。

如需詳細資訊，請參閱下列主題：
+ 如需有關將指標匯出為 Prometheus 指標的現有程式庫和服務詳細資訊，請參閱 Prometheus 說明文件中的[匯出程式和整合](https://prometheus.io/docs/instrumenting/exporters/)。
+ 如需有關從您自己的程式碼匯出與 Prometheus 相容指標的詳細資訊，請參閱 Prometheus 文件中的[撰寫匯出程式](https://prometheus.io/docs/instrumenting/writing_exporters/)。
+ 如需有關如何設定 Amazon Managed Service for Prometheus 收集器以自動從 Amazon EKS 叢集湊集指標的詳細資訊，請參閱 [設定 Amazon EKS 的受管收集器](AMP-collector-how-to.md)。

# 使用付費日誌監控收集器
<a name="AMP-collector-vended-logs"></a>

Amazon Managed Service for Prometheus 收集器提供付費日誌，協助您監控指標收集程序並進行疑難排解。這些日誌會自動傳送至 Amazon CloudWatch Logs，並提供服務探索、指標收集和資料匯出操作的可見性。收集器會為指標收集管道的三個主要元件提供日誌：

**Topics**
+ [服務探索日誌](#amp-collector-service-discovery-vended-logs)
+ [收集器日誌](#amp-collector-vended-logs)
+ [匯出工具日誌](#amp-exporter-vended-logs)
+ [了解和使用收集器提供的日誌](#amp-collector-log-details)

## 服務探索日誌
<a name="amp-collector-service-discovery-vended-logs"></a>

服務探索日誌提供有關目標探索程序的資訊，包括：
+ 存取 Kubernetes API 資源時的身分驗證或許可問題。
+ 服務探索設定中的組態錯誤。

下列範例示範您在服務探索期間可能遇到的常見身分驗證和許可錯誤：

**不存在的 Amazon EKS 叢集**  
當指定的 Amazon EKS 叢集不存在時，您會收到下列錯誤：  

```
{
  "component": "SERVICE_DISCOVERY",
  "timestamp": "2025-04-30T17:25:41.946Z",
  "message": {
    "log": "Failed to watch Service - Verify your scraper source exists."
  },
  "scrapeConfigId": "s-a1b2c3d4-5678-90ab-cdef-EXAMPLE11111"
}
```

**無效的 服務許可**  
當收集器缺少適當的角色型存取控制 (RBAC) 許可來監看 服務時，您會收到此錯誤：  

```
{
  "component": "SERVICE_DISCOVERY",
  "timestamp": "2025-04-30T17:25:41.946Z",
  "message": {
    "log": "Failed to watch Service - Verify your scraper source permissions are valid."
  },
  "scrapeConfigId": "s-a1b2c3d4-5678-90ab-cdef-EXAMPLE11111"
}
```

**端點的無效許可**  
當收集器缺少適當的角色型存取控制 (RBAC) 許可來監看端點時，您會收到此錯誤：  

```
{
  "component": "SERVICE_DISCOVERY",
  "timestamp": "2025-04-30T17:25:41.946Z",
  "message": {
    "log": "Failed to watch Endpoints - Verify your scraper source permissions are valid."
  },
  "scrapeConfigId": "s-a1b2c3d4-5678-90ab-cdef-EXAMPLE11111"
}
```

## 收集器日誌
<a name="amp-collector-vended-logs"></a>

收集器日誌提供有關指標抓取程序的資訊，包括：
+ 由於端點無法使用而導致的擴展失敗。
+ 嘗試抓取目標時發生連線問題。
+ 湊集操作期間的逾時。
+ 湊集目標傳回的 HTTP 狀態錯誤。

下列範例示範您在指標抓取過程中可能遇到的常見收集器錯誤：

**缺少指標端點**  
當`/metrics`端點無法在目標執行個體上使用時，您會收到此錯誤：  

```
{
    "component": "COLLECTOR",
    "message": {
        "log": "Failed to scrape Prometheus endpoint - verify /metrics endpoint is available",
        "job": "pod_exporter",
        "targetLabels": "{__name__=\"up\", instance=\10.24.34.0\", job=\"pod_exporter\"}"
    },
    "timestamp": "1752787969551",
    "scraperId": "s-a1b2c3d4-5678-90ab-cdef-EXAMPLE11111"
}
```

**連線遭拒**  
當收集器無法建立與目標端點的連線時，您會收到此錯誤：  

```
{
  "scrapeConfigId": "s-a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
  "timestamp": "2025-04-30T17:25:41.946Z",
  "message": {
    "message": "Scrape failed",
    "scrape_pool": "pod_exporter",
    "target": "http://10.24.34.0:80/metrics",
    "error": "Get \"http://10.24.34.0:80/metrics\": dial tcp 10.24.34.0:80: connect: connection refused"
  },
  "component": "COLLECTOR"
}
```

## 匯出工具日誌
<a name="amp-exporter-vended-logs"></a>

匯出者日誌提供有關將收集指標傳送至 Amazon Managed Service for Prometheus 工作區的程序資訊，包括：
+ 處理的指標和資料點數量。
+ 匯出因工作區問題而失敗。
+ 嘗試寫入指標時發生許可錯誤。
+ 匯出管道中的相依性失敗。

下列範例示範在指標匯出程序期間可能遇到的常見匯出工具錯誤：

**找不到工作區**  
當找不到指標匯出的目標工作區時，您會收到此錯誤：  

```
{
    "component": "EXPORTER",
    "message": {
        "log": "Failed to export to the target workspace - Verify your scraper destination.",
        "samplesDropped": 5
    },
    "timestamp": "1752787969664",
    "scraperId": "s-a1b2c3d4-5678-90ab-cdef-EXAMPLE11111"
}
```

## 了解和使用收集器提供的日誌
<a name="amp-collector-log-details"></a>

### 日誌結構
<a name="amp-log-structure"></a>

所有收集器提供的日誌都遵循與下列欄位一致的結構：

**scrapeConfigId**  
產生日誌之湊集組態的唯一識別符。

**timestamp**  
產生日誌項目的時間。

**message**  
日誌訊息內容，其中可能包含其他結構化欄位。

**元件**  
產生日誌的元件 (SERVICE\$1DISCOVERY、COLLECTOR 或 EXPORTER)

### 使用付費日誌進行故障診斷
<a name="amp-troubleshooting"></a>

收集器提供的日誌可協助您疑難排解指標收集的常見問題：

1. 服務探索問題
   + 檢查 **SERVICE\$1DISCOVERY** 日誌是否有身分驗證或許可錯誤。
   + 確認收集器具有存取 Kubernetes 資源的必要許可。

1. 指標抓取問題
   + 檢查 **COLLECTOR** 日誌是否有抓取失敗。
   + 驗證目標端點是否可存取並傳回指標。
   + 確保防火牆規則允許收集器連線到目標端點。

1. 指標匯出問題
   + 檢查 **EXPORTER** 日誌是否有匯出失敗。
   + 確認工作區存在且已正確設定。
   + 確保收集器具有寫入工作區的必要許可。

### 存取收集器提供的日誌
<a name="amp-accessing-logs"></a>

收集器提供的日誌會自動傳送至 Amazon CloudWatch Logs。若要存取這些日誌：

1. 透過 [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/) 開啟 CloudWatch 主控台。

1. 在導覽窗格中，選擇 **Log groups** (日誌群組)。

1. 尋找並選取收集器的日誌群組：`/aws/prometheus/workspace_id/collector/collector_id`。

1. 瀏覽或搜尋日誌事件以尋找相關資訊。

您也可以使用 CloudWatch Logs Insights 來查詢和分析收集器日誌。例如，若要尋找所有服務探索錯誤：

```
fields @timestamp, message.message
| filter component = "SERVICE_DISCOVERY" and message.message like /Failed/
| sort @timestamp desc
```

### 監控收集器的最佳實務
<a name="amp-monitoring-best-practices"></a>

若要有效監控 Amazon Managed Service for Prometheus 收集器：

1. 針對關鍵收集器問題設定 CloudWatch 警示，例如持久性抓取失敗或匯出錯誤。如需詳細資訊，請參閱《*Amazon CloudWatch 使用者指南*》中的[警示](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html)。

1. 建立 CloudWatch 儀表板，以視覺化方式呈現收集器效能指標以及已佈建的日誌資料。如需詳細資訊，請參閱《*Amazon CloudWatch 使用者指南*》中的[儀表板](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Dashboards.html)。

1. 定期檢閱服務探索日誌，以確保正確探索目標。

1. 監控捨棄的目標數量，以識別潛在的組態問題。

1. 追蹤匯出失敗，以確保指標成功傳送到您的工作區。

# 客戶受管收集器
<a name="self-managed-collectors"></a>

本節包含透過設定您自己的收集器來擷取資料的相關資訊，這些收集器會使用 Prometheus 遠端寫入將指標傳送至 Amazon Managed Service for Prometheus。

當您使用自己的收集器將指標傳送到 Amazon Managed Service for Prometheus 時，您負責保護指標的安全，並確保擷取程序符合您的可用性需求。

大多數客戶管理的收集器都使用下列其中一種工具：
+ **AWS Distro for OpenTelemetry (ADOT)** – ADOT 是 OpenTelemetry 的完全支援、安全、生產就緒的開放原始碼分佈，可讓代理程式收集指標。您可以使用 ADOT 收集指標，並將其傳送至您的 Amazon Managed Service for Prometheus 工作區。如需有關 ADOT 匯集器的詳細資訊，請參閱 [AWS 適用於 OpenTelemetry 的 AWS Distro](https://aws.amazon.com/otel/)。
+ **Prometheus 代理程式** - 您可以設定自己的開放原始碼 Prometheus 伺服器執行個體 (以客服人員身分執行)，以收集指標並將其轉寄至 Amazon Managed Service for Prometheus。

下列主題說明如何使用這兩個工具，並包括設定自己收集器的一般資訊。

**Topics**
+ [保護您的指標擷取作業](AMP-secure-metric-ingestion.md)
+ [使用 AWS Distro for OpenTelemetry 做為收集器](AMP-ingest-with-adot.md)
+ [使用 Prometheus 執行個體作為收集器](AMP-ingest-with-prometheus.md)
+ [設定 Amazon Managed Service for Prometheus 以取得高可用性資料](AMP-ingest-high-availability.md)

# 保護您的指標擷取作業
<a name="AMP-secure-metric-ingestion"></a>

Amazon Managed Service for Prometheus 提供協助您保護指標擷取的方法。

## AWS PrivateLink 搭配 Amazon Managed Service for Prometheus 使用
<a name="AMP-secure-VPC"></a>

將指標擷取至 Amazon Managed Service for Prometheus 的網路流量可以透過公有網際網路端點或透過 VPC 端點完成 AWS PrivateLink。使用 AWS PrivateLink 可確保來自 VPC 的網路流量在 AWS 網路中受到保護，而無需透過公用網際網路。若要為 Amazon Managed Service for Prometheus 建立 AWS PrivateLink VPC 端點，請參閱 [使用 Amazon Managed Service for Prometheus 和介面 VPC 端點](AMP-and-interface-VPC.md)。

## 身分驗證和授權
<a name="AMP-secure-auth"></a>

AWS Identity and Access Management (IAM) 是一種 Web 服務，可協助您安全地控制對 AWS 資源的存取。您可以使用 IAM 來控制能通過身分驗證 (登入) 和授權使用資源的 (具有許可) 的人員。Amazon Managed Service for Prometheus 與 IAM 整合，協助您確保資料安全。當您設定 Amazon Managed Service for Prometheus 時，您需要建立一些 IAM 角色，使其能夠從 Prometheus 伺服器擷取指標，並讓 Grafana 伺服器查詢存放在 Amazon Managed Service for Prometheus 工作區中的指標。如需有關 IAM 的相關資訊，請參閱[什麼是 IAM？](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html)。

另一個可協助您設定 Amazon Managed Service for Prometheus AWS 的安全功能是 AWS Signature 第 4 版簽署程序 (AWS SigV4)。Signature 第 4 版是將身分驗證資訊新增至 HTTP 傳送之 AWS 請求的程序。為了安全起見，對 的大多數請求 AWS 都必須使用存取金鑰簽署，該金鑰包含存取金鑰 ID 和私密存取金鑰。這兩種金鑰通常稱為您的安全憑證。如需有關 SigV4 的詳細資訊，請參閱[簽章第 4 版簽署程序](https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html)。

# 使用 AWS Distro for OpenTelemetry 做為收集器
<a name="AMP-ingest-with-adot"></a>

本節說明如何設定 AWS Distro for OpenTelemetry (ADOT) 收集器從 Prometheus 檢測的應用程式進行湊集，並將指標傳送至 Amazon Managed Service for Prometheus。如需有關 ADOT 匯集器的詳細資訊，請參閱 [AWS 適用於 OpenTelemetry 的 AWS Distro](https://aws.amazon.com/otel/)。

下列主題說明三種將 ADOT 設定為指標收集器的不同方式，取決於您的指標是來自 Amazon EKS、Amazon ECS 還是 Amazon EC2 執行個體。

**Topics**
+ [在 Amazon Elastic Kubernetes Service 叢集上使用 AWS Distro for OpenTelemetry 設定指標擷取](AMP-onboard-ingest-metrics-OpenTelemetry.md)
+ [使用 AWS Distro for Open Telemetry 從 Amazon ECS 設定指標擷取](AMP-onboard-ingest-metrics-OpenTelemetry-ECS.md)
+ [使用遠端寫入設定 Amazon EC2 執行個體擷取的指標](AMP-onboard-ingest-metrics-remote-write-EC2.md)

# 在 Amazon Elastic Kubernetes Service 叢集上使用 AWS Distro for OpenTelemetry 設定指標擷取
<a name="AMP-onboard-ingest-metrics-OpenTelemetry"></a>

您可以使用 AWS Distro for OpenTelemetry (ADOT) 收集器從 Prometheus 檢測應用程式抓取指標，並將指標傳送至 Amazon Managed Service for Prometheus。

**注意**  
如需 ADOT 收集器的詳細資訊，請參閱 [AWS Distro for OpenTelemetry](https://aws.amazon.com/otel/)。  
如需 Prometheus 檢測應用程式的詳細資訊，請參閱 [什麼是與 Prometheus 相容的指標？](prom-compatible-metrics.md)。

使用 ADOT 收集 Prometheus 指標涉及三個 OpenTelemetry 元件：Prometheus 接收器、Prometheus 遠端寫入匯出程式和 SigV4 驗證延伸。

您可以使用現有的 Prometheus 組態，來組態 Prometheus 接收器來執行服務探索和指標抓取。Prometheus 接收器會以 Prometheus 展開格式抓取指標。您要抓取的任何應用程式或端點都應使用 Prometheus 用戶端程式庫進行組態。Prometheus 接收器在 Prometheus 說明文件中，支援[組態](https://prometheus.io/docs/prometheus/latest/configuration/configuration/)中說明的全套 Prometheus 抓取和重新標籤組態。您可以將這些組態直接貼到您的 ADOT 收集器組態中。

Prometheus 遠端寫入匯出程式會使用 `remote_write` 端點將抓取的指標傳送至您的管理入口網站工作區。將使用 AWS SigV4 身分驗證延伸的安全身分驗證 AWS 通訊協定 Sigv4 簽署匯出資料的 HTTP 請求。如需詳細資訊，請參閱[簽章版本 4 簽署程序](https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html)。

收集器會自動探索 Amazon EKS 上的 Prometheus 指標端點，並使用[<kubernetes\$1sd\$1config>](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#kubernetes_sd_config)中找到的組態。

 以下示範是在執行 Amazon Elastic Kubernetes Service 或自我管理 Kubernetes 的叢集上進行此組態的範例。若要執行這些步驟，您必須擁有 AWS 來自預設登入資料鏈中任何潛在選項的 AWS 登入資料。如需詳細資訊，請參閱[設定 AWS SDK for Go](https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html)。此示範使用範例應用程式，用於程序的整合測試。範例應用程式會在 `/metrics` 端點公開指標，例如 Prometheus 用戶端程式庫。

## 先決條件
<a name="AMP-onboard-ingest-metrics-OpenTelemetry-pre"></a>

在開始下列擷取設定步驟之前，您必須為服務帳戶和信任政策設定 IAM 角色。

**設定服務帳戶和信任政策的 IAM 角色**

1. 遵循 [自 Amazon EKS 叢集設定指標擷取作業的服務角色](set-up-irsa.md#set-up-irsa-ingest) 中的步驟，為服務帳戶建立 IAM 角色。

   當 ADOT 收集器抓取並匯出指標時，將使用此角色。

1. 接下來，編輯信任政策。前往 [https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/home) 開啟 IAM 主控台。

1. 在左側導覽窗格中選擇**角色**，然後尋找您在步驟 1 中建立的 **amp-iamproxy-ingest-role**。

1. 選擇**信任關係**索引標籤，然後選擇**編輯信任關係**。

1. 在信任關係政策 JSON 中，取代 `aws-amp` 為 `adot-col`，然後選擇**更新信任原則**。結果信任政策應如下所示：

------
#### [ JSON ]

****  

   ```
   {
     "Version":"2012-10-17",		 	 	 
     "Statement": [
       {
         "Effect": "Allow",
         "Principal": {
           "Federated": "arn:aws:iam::111122223333:oidc-provider/oidc.eks.us-east-1.amazonaws.com/id/EXAMPLED539D4633E53DE1B71EXAMPLE"
         },
         "Action": "sts:AssumeRoleWithWebIdentity",
         "Condition": {
           "StringEquals": {
             "oidc.eks.us-east-1.amazonaws.com/id/EXAMPLED539D4633E53DE1B71EXAMPLE:sub": "system:serviceaccount:adot-col:amp-iamproxy-ingest-service-account",
             "oidc.eks.us-east-1.amazonaws.com/id/EXAMPLED539D4633E53DE1B71EXAMPLE:aud": "sts.amazonaws.com"
           }
         }
       }
     ]
   }
   ```

------

1. 選擇**權限**索引標籤，並確定已將下列權限政策附加至該角色。

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Effect": "Allow",
               "Action": [
                   "aps:RemoteWrite",
                   "aps:GetSeries",
                   "aps:GetLabels",
                   "aps:GetMetricMetadata"
               ],
               "Resource": "*"
           }
       ]
   }
   ```

------

## 啟用 Prometheus 指標收集
<a name="AMP-onboard-ingest-metrics-OpenTelemetry-steps"></a>

**注意**  
當您在 Amazon EKS 中建立命名空間時，依預設 `alertmanager` 會停用節點匯出程式。

**在 Amazon EKS 或 Kubernetes 叢集上啟用 Prometheus 集合**

1. 分隔並複製從 [aws-otel-community](https://github.com/aws-observability/aws-otel-community) 儲存庫中的樣本應用程式。

   然後，執行以下命令。

   ```
   cd ./sample-apps/prometheus-sample-app
   docker build . -t prometheus-sample-app:latest
   ```

1. 將此圖片推送到註冊表，例如 Amazon ECR 或 DockerHub。

1. 複製此 Kubernetes 組態並套用，在叢集中部署範例應用程式。通過在 `prometheus-sample-app.yaml` 檔案中替換 `{{PUBLIC_SAMPLE_APP_IMAGE}}` 將圖像變更為剛才推送的圖像。

   ```
   curl https://raw.githubusercontent.com/aws-observability/aws-otel-collector/main/examples/eks/aws-prometheus/prometheus-sample-app.yaml -o prometheus-sample-app.yaml
   kubectl apply -f prometheus-sample-app.yaml
   ```

1. 輸入下列命令，確認已啟動範例應用程式。在命令的輸出中，您將在 `NAME` 欄中看到 `prometheus-sample-app`。

   ```
   kubectl get all -n aoc-prometheus-pipeline-demo
   ```

1. 啟動 ADOT 收集器的預設執行個體。若要執行此作業，請先輸入下列命令來拉取 ADOT 收集器的 Kubernetes 組態。

   ```
   curl https://raw.githubusercontent.com/aws-observability/aws-otel-collector/main/examples/eks/aws-prometheus/prometheus-daemonset.yaml -o prometheus-daemonset.yaml
   ```

   然後編輯範本檔案，將您 `YOUR_ENDPOINT` 的 Amazon Managed Service for Prometheus 工作區以及您 `YOUR_REGION` 的區域替換 **remote\$1write** 端點。查看工作區詳細資料時，請使用在 Amazon Managed Service for Prometheus 主控台中顯示的 **remote\$1write** 端點。

   您也需要在 Kubernetes 組態的服務帳戶區段`YOUR_ACCOUNT_ID`中將 變更為 AWS 您的帳戶 ID。

   在此範例中，ADOT 收集器組態使用註解 (`scrape=true`) 來判斷要抓取哪些目標端點。這可讓 ADOT 收集器區分範例應用程式端點與叢集中的 kube 系統端點。若您要抓取不同範例應用程式，則可以將其從重新標籤組態中移除。

1. 輸入下列命令以部署 ADOT 收集器。

   ```
   kubectl apply -f prometheus-daemonset.yaml
   ```

1. 輸入下列命令，確認已啟動 ADOT 收集器。在 `NAMESPACE` 欄中尋找 `adot-col`。

   ```
   kubectl get pods -n adot-col
   ```

1. 使用記錄匯出程式確認管線是否正常運作。我們的範例範本已與記錄匯出程式整合。輸入下列命令：

   ```
   kubectl get pods -A
   kubectl logs -n adot-col name_of_your_adot_collector_pod
   ```

   範例應用程式中的某些抓取指標將依照以下範例所示：

   ```
   Resource labels:
        -> service.name: STRING(kubernetes-service-endpoints)
        -> host.name: STRING(192.168.16.238)
        -> port: STRING(8080)
        -> scheme: STRING(http)
   InstrumentationLibraryMetrics #0
   Metric #0
   Descriptor:
        -> Name: test_gauge0
        -> Description: This is my gauge
        -> Unit: 
        -> DataType: DoubleGauge
   DoubleDataPoints #0
   StartTime: 0
   Timestamp: 1606511460471000000
   Value: 0.000000
   ```

1. 若要測試 Amazon Managed Service for Prometheus 是否收到指標，請使用 `awscurl`。此工具可讓您透過命令列使用 AWS Sigv4 身分驗證來傳送 HTTP 請求，因此您必須在本機設定 AWS 登入資料，並具有從 Amazon Managed Service for Prometheus 查詢的正確許可。如需安裝 的指示`awscurl`，請參閱 [awscurl](https://github.com/okigan/awscurl)。

   在下列命令中，將 `AMP_REGION` 和 `AMP_ENDPOINT` 替換為 Amazon Managed Service for Prometheus 工作區的資訊。

   ```
   awscurl --service="aps" --region="AMP_REGION" "https://AMP_ENDPOINT/api/v1/query?query=adot_test_gauge0"
   {"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"adot_test_gauge0"},"value":[1606512592.493,"16.87214000011479"]}]}}
   ```

   如果您收到指標作為回應，則表示管道設定已成功，且指標已成功從範例應用程式傳播到 Amazon Managed Service for Prometheus。

**清除**

若要清理此示範，請輸入下列命令。

```
kubectl delete namespace aoc-prometheus-pipeline-demo
kubectl delete namespace adot-col
```

## 進階組態
<a name="AMP-otel-advanced"></a>

Prometheus 接收器在 Prometheus 說明文件中，支援[組態](https://prometheus.io/docs/prometheus/latest/configuration/configuration/)中說明的全套 Prometheus 抓取和重新標籤組態。您可以將這些組態直接貼到您的 ADOT 收集器組態中。

Prometheus 接收器的組態包括您的服務探索、抓取組態和重新標籤組態。接收器組態如下所示。

```
receivers:
  prometheus:
    config:
      [[Your Prometheus configuration]]
```

以下是範例組態。

```
receivers:
  prometheus:
    config:
      global:
        scrape_interval: 1m
        scrape_timeout: 10s
        
      scrape_configs:
      - job_name: kubernetes-service-endpoints
        sample_limit: 10000
        kubernetes_sd_configs:
        - role: endpoints
        tls_config:
          ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
          insecure_skip_verify: true
        bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
```

如果您有現有的 Prometheus 組態，則必須將 `$` 個字元替換為 `$$` 以避免將值替換為環境變數。\$1 這對於 relabel\$1configurations 的替換值特別重要。例如，若您開始使用下列 relabel\$1configuration：

```
relabel_configs:
- source_labels: [__meta_kubernetes_ingress_scheme,__address__,__meta_kubernetes_ingress_path]
  regex: (.+);(.+);(.+)
  replacement: ${1}://${2}${3}
  target_label: __param_target
```

它將成為以下幾點：

```
relabel_configs:
- source_labels: [__meta_kubernetes_ingress_scheme,__address__,__meta_kubernetes_ingress_path]
  regex: (.+);(.+);(.+)
  replacement: $${1}://${2}${3}
  target_label: __param_target
```

**Prometheus 遠端寫入匯出程式和 Sigv4 身分驗證延伸**

Prometheus 遠端寫入匯出程式和 Sigv4 身分驗證延伸的設定較 Prometheus 接收器容易。在此管道階段已擷取指標，我們已準備好將這些資料匯出到 Amazon Managed Service for Prometheus。下列範例顯示成功組態與 Amazon Managed Service for Prometheus 通訊的最低需求。

```
extensions:
  sigv4auth:
    service: "aps"
    region: "user-region"
exporters:
  prometheusremotewrite:
    endpoint: "https://aws-managed-prometheus-endpoint/api/v1/remote_write"
    auth:
      authenticator: "sigv4auth"
```

此組態會使用來自預設 AWS 登入資料鏈的 AWS 登入資料，傳送由 AWS SigV4 簽署的 HTTPS 請求。如需詳細資訊，請參閱[設定 適用於 Go 的 AWS SDK](https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html)。您必須將服務指定為 `aps`。

無論部署方法為何，ADOT 收集器都必須能夠存取預設 AWS 登入資料鏈中列出的其中一個選項。Sigv4 身分驗證延伸取決於 適用於 Go 的 AWS SDK ，並使用它來擷取登入資料並進行身分驗證。您必須確保這些憑證有 Amazon Managed Service for Prometheus 的遠端寫入權限。

# 使用 AWS Distro for Open Telemetry 從 Amazon ECS 設定指標擷取
<a name="AMP-onboard-ingest-metrics-OpenTelemetry-ECS"></a>

本節說明如何從 Amazon Elastic Container Service (Amazon ECS) 收集指標，並使用 AWS Distro for Open Telemetry (ADOT) 將其擷取至 Amazon Managed Service for Prometheus。這同時說明如何在 Amazon Managed Grafana 中將指標視覺化。

## 先決條件
<a name="AMP-onboard-ingest-metrics-OpenTelemetry-ECS-prereq"></a>

**重要**  
在開始之前，您必須在具有預設設定的 AWS Fargate 叢集上擁有 Amazon ECS 環境、Amazon Managed Service for Prometheus 工作區，以及 Amazon Managed Grafana 工作區。我們假設您熟悉容器工作負載、Amazon Managed Service for Prometheus，以及 Amazon Managed Grafana。

如需詳細資訊，請參閱下列連結：
+ 如需如何使用預設設定在 Fargate 叢集上建立 Amazon ECS 環境的詳細資訊，請參閱《*Amazon ECS 開發人員指南*》中的[建立叢集](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/create_cluster.html)。
+ 如需如何建立 Amazon Managed Service for Prometheus 的詳細資訊，請參閱《*Amazon Managed Service for Prometheus 使用者指南*》中的[建立工作區](https://docs.aws.amazon.com/prometheus/latest/userguide/AMP-onboard-create-workspace.html)。
+ 如需如何建立 Amazon Managed Grafana 工作區的詳細資訊，請參閱《*Amazon Managed Grafana 使用者指南*》中的[建立工作區](https://docs.aws.amazon.com/grafana/latest/userguide/AMG-create-workspace.html)。

## 步驟 1：定義自訂 ADOT 收集器容器映像
<a name="AMP-onboard-ingest-metrics-OpenTelemetry-ECS-create"></a>

使用下列組態檔作為範本，定義您自己的 ADOT 收集器容器映像檔。將 *my-remote-URL* 和 *my-region* 替換為您的 `endpoint` 和 `region` 值。將組態儲存在一個名為 *adot-config.yaml* 檔案中的組態。

**注意**  
此組態使用 `sigv4auth` 延伸來驗證 Amazon Managed Service for Prometheus 的呼叫。如需有關組態 `sigv4auth` 的詳細資訊，請參閱 GitHub 上的[驗證器 - Sigv4](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/extension/sigv4authextension)。

```
receivers:
  prometheus:
    config:
      global:
        scrape_interval: 15s
        scrape_timeout: 10s
      scrape_configs:
      - job_name: "prometheus"
        static_configs:
        - targets: [ 0.0.0.0:9090 ]
  awsecscontainermetrics:
    collection_interval: 10s
processors:
  filter:
    metrics:
      include:
        match_type: strict
        metric_names:
          - ecs.task.memory.utilized
          - ecs.task.memory.reserved
          - ecs.task.cpu.utilized
          - ecs.task.cpu.reserved
          - ecs.task.network.rate.rx
          - ecs.task.network.rate.tx
          - ecs.task.storage.read_bytes
          - ecs.task.storage.write_bytes
exporters:
  prometheusremotewrite:
    endpoint: my-remote-URL
    auth:
      authenticator: sigv4auth
  logging:
    loglevel: info
extensions:
  health_check:
  pprof:
    endpoint: :1888
  zpages:
    endpoint: :55679
  sigv4auth:
    region: my-region
    service: aps
service:
  extensions: [pprof, zpages, health_check, sigv4auth]
  pipelines:
    metrics:
      receivers: [prometheus]
      exporters: [logging, prometheusremotewrite]
    metrics/ecs:
      receivers: [awsecscontainermetrics]
      processors: [filter]
      exporters: [logging, prometheusremotewrite]
```

## 步驟 2：將您的 ADOT 收集器容器映像推送至 Amazon ECR 儲存庫
<a name="AMP-onboard-ingest-metrics-OpenTelemetry-ECS-push"></a>

使用 Dockerfile 建立容器映像，然後將其推送至 Amazon Elastic Container Registry (ECR) 儲存庫。

1. 建立 Dockerfile 以複製和新增您的容器映像檔並將其新增至 OTEL Docker 映像檔中。

   ```
   FROM public.ecr.aws/aws-observability/aws-otel-collector:latest
   COPY adot-config.yaml /etc/ecs/otel-config.yaml
   CMD ["--config=/etc/ecs/otel-config.yaml"]
   ```

1. 建立 Amazon ECR 儲存庫。

   ```
   # create repo:
   COLLECTOR_REPOSITORY=$(aws ecr create-repository --repository aws-otel-collector \ 
                                  --query repository.repositoryUri --output text)
   ```

1. 建立容器映像。

   ```
   # build ADOT collector image:
   docker build -t $COLLECTOR_REPOSITORY:ecs .
   ```
**注意**  
這假設您正在執行容器的相同環境中建構容器。若否，您可能需要在建立映像時使用 `--platform` 參數。

1. 登入 Amazon ECR 儲存庫。將 *my-region* 替換為您的 `region` 值。

   ```
   # sign in to repo:
   aws ecr get-login-password --region my-region | \
           docker login --username AWS --password-stdin $COLLECTOR_REPOSITORY
   ```

1. 推送您的容器映像。

   ```
   # push ADOT collector image:
   docker push $COLLECTOR_REPOSITORY:ecs
   ```

## 步驟 3：建立 Amazon ECS 任務定義來抓取 Amazon Managed Service for Prometheus
<a name="AMP-onboard-ingest-metrics-OpenTelemetry-ECS-task"></a>

建立 Amazon ECS 任務定義來抓取 Amazon Managed Service for Prometheus。您的工作定義應包含名為 `adot-collector` 的容器和名為 `prometheus` 的容器。`prometheus` 產生指標，和 `adot-collector` 抓取 `prometheus`。

**注意**  
Amazon Managed Service for Prometheus 以服務的形式執行，並從容器收集指標。在這種情況下，容器會以代理程式模式在本端執行 Prometheus，並將本端指標傳送至 Amazon Managed Service for Prometheus。

**範例：任務定義**

以下為任務定義外觀的範例。您可以使用此範例作為建立您任務定義的範本。將 `adot-collector` 的 `image` 值替換為儲存庫 URL 和映像標籤 (`$COLLECTOR_REPOSITORY:ecs`)。將 `adot-collector` 和 `prometheus` 的 `region` 個值替換為 `region` 個值。

```
{
  "family": "adot-prom",
  "networkMode": "awsvpc",
  "containerDefinitions": [
    {
      "name": "adot-collector",
      "image": "account_id.dkr.ecr.region.amazonaws.com/image-tag",
      "essential": true,
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": {
          "awslogs-group": "/ecs/ecs-adot-collector",
          "awslogs-region": "my-region",
          "awslogs-stream-prefix": "ecs",
          "awslogs-create-group": "True"
        }
      }
    },
    {
      "name": "prometheus",
      "image": "prom/prometheus:main",
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": {
          "awslogs-group": "/ecs/ecs-prom",
          "awslogs-region": "my-region",
          "awslogs-stream-prefix": "ecs",
          "awslogs-create-group": "True"
        }
      }
    }
  ],
  "requiresCompatibilities": [
    "FARGATE"
  ],
  "cpu": "1024"
}
```

## 步驟 4：授予您的任務存取 Amazon Managed Service for Prometheus 的許可
<a name="AMP-onboard-ingest-metrics-OpenTelemetry-ECS-attach"></a>

若要將抓取指標傳送至 Amazon Managed Service for Prometheus，您的 Amazon ECS 任務必須具有正確的許可，才能為您呼叫 AWS API 操作。您必須為任務建立 IAM 角色，並將 `AmazonPrometheusRemoteWriteAccess` 政策附加至 IAM 角色。如需有關建立此角色並附加政策的詳細資訊，請參閱[為任務建立 IAM 角色和政策](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html#create_task_iam_policy_and_role)。

在您將 `AmazonPrometheusRemoteWriteAccess` 附加至 IAM 角色並將該角色用於您的任務之後，Amazon ECS 可以將您抓取的指標傳送到 Amazon Managed Service for Prometheus。

## 步驟 5：在 Amazon Managed Grafana 中視覺化您的指標
<a name="AMP-onboard-ingest-metrics-OpenTelemetry-ECS-vis"></a>

**重要**  
在開始之前，您必須在 Amazon ECS 任務定義中執行 Fargate 任務。否則，Amazon Managed Service for Prometheus 將無法使用您的指標。

1. 在 Amazon Managed Grafana 工作區的導覽窗格中，選擇 AWS 圖示下的**資料來源**。

1. 在**資料來源**索引標籤上，針對**服務**選取 **Amazon Managed Service for Prometheus**，然後選擇您的**預設區域**。

1. 選擇 **[新增資料來源]**。

1. 使用 `ecs` 和 `prometheus` 個前綴查詢和檢視您的指標。

# 使用遠端寫入設定 Amazon EC2 執行個體擷取的指標
<a name="AMP-onboard-ingest-metrics-remote-write-EC2"></a>

本節說明如何在 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體中使用遠端寫入執行 Prometheus 伺服器。這會說明如何從使用 Go 編寫的示範應用程式收集指標，並將其傳送到 Amazon Managed Service for Prometheus 工作區。

## 先決條件
<a name="AMP-onboard-ingest-metrics-remote-write-EC2-prereq"></a>

**重要**  
在開始前，您必須安裝 Prometheus v2.26 或更高版本。我們假設您熟悉 Prometheus、Amazon EC2 和 Amazon Managed Service for Prometheus。有關如何安裝 Prometheus 的訊息，請參閱 Prometheus 網站上的[入門](https://prometheus.io/docs/prometheus/latest/getting_started/)。

如果您不熟悉 Amazon EC2 或 Amazon Managed Service for Prometheus，建議您先從閱讀以下各節開始：
+ [Amazon Elastic Compute Cloud 是什麼？](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html)
+ [Amazon Managed Service for Prometheus 是什麼？](https://docs.aws.amazon.com/prometheus/latest/userguide/what-is-Amazon-Managed-Service-Prometheus.html)

## 建立 Amazon EC2 的 IAM 角色
<a name="AMP-onboard-ingest-metrics-remote-write-EC2-IAM"></a>

若要串流指標，您必須先使用 AWS 受管政策 **AmazonPrometheusRemoteWriteAccess** 建立 IAM 角色。然後，您可以啟動具有角色的執行個體，並將指標串流到 Amazon Managed Service for Prometheus 工作區。

1. 前往 [https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/) 開啟 IAM 主控台。

1. 從導覽窗格，選擇 **Roles (角色)**，然後選擇 **Create role (建立角色)**。

1. 對於信任的實體類型，選擇 **AWS service** (AWS 服務)。針對使用案例，選擇 **EC2**。選擇**下一步：許可**。

1. 在搜尋列中，輸入 **Amazon PrometheusRemoteWriteAccess**。針對**政策名稱**，選取 **AmazonPrometheusRemoteWriteAccess** 然後選擇 **附加政策**。選擇 **Next: Add Tags (下一步：新增標籤)**。

1. (選用) 為您的 IAM 角色建立 IAM 標籤。選擇 **[下一步：檢閱]**。

1. 輸入您的角色名稱。選擇**建立政策**。

## 啟動 Amazon EC2 執行個體
<a name="AMP-onboard-ingest-metrics-remote-write-EC2-instance"></a>

若要啟動 Amazon EC2 執行個體，請遵循《*適用於 Linux 執行個體的 Amazon Elastic Compute Cloud 使用者指南*》中[啟動執行個體](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html#launch-instance-with-role)的指示。

## 執行示範應用程式
<a name="AMP-onboard-ingest-metrics-remote-write-EC2-demo"></a>

建立 IAM 角色並使用 角色啟動 EC2 執行個體後，您可以執行示範應用程式來查看它是否有效。

**執行示範應用程式和測試指標**

1. 使用下列範本建立名為 `main.go` 的 Go 檔案。

   ```
   package main
   
   import (
       "github.com/prometheus/client_golang/prometheus/promhttp"
       "net/http"
   )
   
   func main() {
       http.Handle("/metrics", promhttp.Handler())
   
       http.ListenAndServe(":8000", nil)
   }
   ```

1. 執行以下命令以安裝正確相依項目。

   ```
   sudo yum update -y
   sudo yum install -y golang
   go get github.com/prometheus/client_golang/prometheus/promhttp
   ```

1. 執行示範應用程式。

   ```
   go run main.go
   ```

   展示應用程式應該在連接埠 8000 上運行，並顯示所有暴露的 Prometheus 指標。以下是這些指標的範例。

   ```
   curl -s http://localhost:8000/metrics 
   ...
   process_max_fds 4096# HELP process_open_fds Number of open file descriptors.# TYPE process_open_fds gauge
   process_open_fds 10# HELP process_resident_memory_bytes Resident memory size in bytes.# TYPE process_resident_memory_bytes gauge
   process_resident_memory_bytes 1.0657792e+07# HELP process_start_time_seconds Start time of the process since unix epoch in seconds.# TYPE process_start_time_seconds gauge
   process_start_time_seconds 1.61131955899e+09# HELP process_virtual_memory_bytes Virtual memory size in bytes.# TYPE process_virtual_memory_bytes gauge
   process_virtual_memory_bytes 7.77281536e+08# HELP process_virtual_memory_max_bytes Maximum amount of virtual memory available in bytes.# TYPE process_virtual_memory_max_bytes gauge
   process_virtual_memory_max_bytes -1# HELP promhttp_metric_handler_requests_in_flight Current number of scrapes being served.# TYPE promhttp_metric_handler_requests_in_flight gauge
   promhttp_metric_handler_requests_in_flight 1# HELP promhttp_metric_handler_requests_total Total number of scrapes by HTTP status code.# TYPE promhttp_metric_handler_requests_total counter
   promhttp_metric_handler_requests_total{code="200"} 1
   promhttp_metric_handler_requests_total{code="500"} 0
   promhttp_metric_handler_requests_total{code="503"} 0
   ```

## 建立 Amazon Managed Service for Prometheus 工作區
<a name="AMP-onboard-ingest-metrics-remote-write-EC2-workspace"></a>

若要建立 Amazon Managed Service for Prometheus 工作區，請按照[建立工作區](AMP-create-workspace.md)中的指示操作。

## 執行 Prometheus 伺服器
<a name="AMP-onboard-ingest-metrics-remote-write-EC2-server"></a>

1. 使用下列範例 YAML 檔案作為範本，以建立名為 `prometheus.yaml` 的新檔案。針對 `url`，將 *my-region* 值替換您的地區值，以及將 *my-workspace-id* 替換為針對您產生 Amazon Managed Service for Prometheus 的工作區 ID。針對 `region`，將 *my-region* 替換為您的地區值。

   **範例：YAML 檔案**

   ```
   global:
     scrape_interval: 15s
     external_labels:
       monitor: 'prometheus'
   
   scrape_configs:
     - job_name: 'prometheus'
       static_configs:
         - targets: ['localhost:8000']
   
   remote_write:
     -
       url: https://aps-workspaces.my-region.amazonaws.com/workspaces/my-workspace-id/api/v1/remote_write
       queue_config:
           max_samples_per_send: 1000
           max_shards: 200
           capacity: 2500
       sigv4:
            region: my-region
   ```

1. 執行 Prometheus 伺服器，將示範應用程式的指標傳送至您的 Amazon Managed Service for Prometheus 工作區。

   ```
   prometheus --config.file=prometheus.yaml
   ```

Prometheus 伺服器現在應該會將示範應用程式的指標傳送到您的 Amazon Managed Service for Prometheus 工作區。

# 使用 Prometheus 執行個體作為收集器
<a name="AMP-ingest-with-prometheus"></a>

您可以使用在*代理*程式模式 （稱為 Prometheus *代理程式） 中執行的 Prometheus* 執行個體來抓取指標，並將其傳送至您的 Amazon Managed Service for Prometheus 工作區。

下列主題說明如何針對在代理程式模式下執行的 Prometheus 執行個體，設定作為指標的收集器的不同方法。

**警告**  
當您建立 Prometheus 代理程式時，您必須負責其組態和維護。[啟用安全功能](https://prometheus.io/docs/prometheus/latest/configuration/https/)，避免 Prometheus 湊集端點暴露於公有網際網路。

如果您設定多個 Prometheus 執行個體來監控同一組指標，並將其傳送到單一 Amazon Managed Service for Prometheus 以獲得高可用性，則需要設定重複資料刪除功能。若未按照步驟設定重複資料刪除功能，則會向您收取傳送至 Amazon Managed Service for Prometheus 的所有資料樣本費用，包括重複樣本。如需有關設定重複資料刪除的指示，請參閱 [將傳送至 Amazon Managed Service for Prometheus 的高可用性指標刪除重複資料](AMP-ingest-dedupe.md)。

**Topics**
+ [設定使用 Helm 從新的 Prometheus 伺服器擷取](AMP-onboard-ingest-metrics-new-Prometheus.md)
+ [在 EC2 上 Kubernetes 中設定現有 Prometheus 伺服器的擷取作業](AMP-onboard-ingest-metrics-existing-Prometheus.md)
+ [在 Fargate 的 Kubernetes 從現有的 Prometheus 伺服器設定擷取作業](AMP-onboard-ingest-metrics-existing-Prometheus-fargate.md)

# 設定使用 Helm 從新的 Prometheus 伺服器擷取
<a name="AMP-onboard-ingest-metrics-new-Prometheus"></a>

本節中的指示可協助您快速啟動，並使用 Amazon Managed Service for Prometheus 執行。您在 Amazon EKS 叢集中設定新的 Prometheus 伺服器，新伺服器會使用預設組態將指標傳送至 Amazon Managed Service for Prometheus。此主題有以下先決條件：
+ 您必須擁有新的 Prometheus 伺服器將從中收集指標的 Amazon EKS 叢集。
+ 您的 Amazon EKS 叢集必須安裝 [Amazon EBS CSI 驅動程式](https://docs.aws.amazon.com/eks/latest/userguide/ebs-csi.html) (Helm 要求）。
+ 您必須使用 Helm CLI 3.0 或更新版本。
+ 您必須使用 Linux 或 macOS 電腦來執行以下各節中的步驟。

## 步驟 1：新增 Helm Chart 儲存庫
<a name="AMP-onboard-new-Prometheus-HelmRepo"></a>

若要新增 Helm Chart 儲存庫，請輸入下列命令。如需有關這些命令的詳細資訊，請參閱 [Helm 儲存庫](https://helm.sh/docs/helm/helm_repo/)。

```
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo add kube-state-metrics https://kubernetes.github.io/kube-state-metrics
helm repo update
```

## 步驟 2：建立 Prometheus 命名空間
<a name="AMP-onboard-new-Prometheus-namespace"></a>

輸入下列命令，為 Prometheus 伺服器和其他監控元件建立 Prometheus 命名空間。將 *prometheus-namespace* 替換為您希望此命名空間的名稱。

```
kubectl create namespace prometheus-namespace
```

## 步驟 3：為服務帳戶設定 IAM 角色
<a name="AMP-onboard-new-Prometheus-IRSA"></a>

若為我們正在記錄的入職方法，您需要在執行 Prometheus 伺服器的 Amazon EKS 叢集中使用服務帳戶的 IAM 角色。

透過服務帳戶的 IAM 角色，您可以產生 IAM 角色與 Kubernetes 服務帳戶的關聯。然後，此服務帳戶可以為使用該服務帳戶之任何 Pod 中的容器提供 AWS 許可。如需詳細資訊，請參閱[服務帳戶的 IAM 角色](https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html)。

如果您尚未設定這些角色，請按照中的 [自 Amazon EKS 叢集設定指標擷取作業的服務角色](set-up-irsa.md#set-up-irsa-ingest) 指示設定角色。本節中的說明需要使用 `eksctl`。如需詳細資訊，請參閱 [Amazon Elastic Kubernetes Service 入門 - `eksctl`](https://docs.aws.amazon.com/eks/latest/userguide/getting-started-eksctl.html)。

**注意**  
當您不在 EKS 或 ， AWS 並且只使用存取金鑰和私密金鑰來存取 Amazon Managed Service for Prometheus 時，則無法使用 `EKS-IAM-ROLE` 型 SigV4。

## 步驟 4：設定新伺服器並開始擷取指標
<a name="AMP-onboard-ingest-metrics-new-Prometheus-Helm"></a>

若要安裝新的 Prometheus 伺服器，該伺服器會將指標傳送至您的 Amazon Managed Service for Prometheus 工作區，請按照下列步驟操作。

**安裝新的 Prometheus 伺服器，以將指標傳送至您的 Amazon Managed Service for Prometheus 工作區**

1. 使用文字編輯器建立名為 `my_prometheus_values_yaml` 的檔案，包含下列內容。
   + 將 *IAM\$1PROXY\$1PROMETHEUS\$1ROLE\$1ARN* 替換為您在 [自 Amazon EKS 叢集設定指標擷取作業的服務角色](set-up-irsa.md#set-up-irsa-ingest) 中建立 **amp-iamproxy-ingest-role** 的 ARN。
   + 將 *WORKSPACE\$1ID* 替換為您 Amazon Managed Service for Prometheus 工作區的 ID。
   + 將 *REGION* 替換為您 Amazon Managed Service for Prometheus 工作區的區域。

   ```
   ## The following is a set of default values for prometheus server helm chart which enable remoteWrite to AMP
   ## For the rest of prometheus helm chart values see: https://github.com/prometheus-community/helm-charts/blob/main/charts/prometheus/values.yaml
   ##
   serviceAccounts:
     server:
       name: amp-iamproxy-ingest-service-account
       annotations: 
         eks.amazonaws.com/role-arn: ${IAM_PROXY_PROMETHEUS_ROLE_ARN}
   server:
     remoteWrite:
       - url: https://aps-workspaces.${REGION}.amazonaws.com/workspaces/${WORKSPACE_ID}/api/v1/remote_write
         sigv4:
           region: ${REGION}
         queue_config:
           max_samples_per_send: 1000
           max_shards: 200
           capacity: 2500
   ```

1. 輸入下列命令以建立 Prometheus 伺服器。
   + 將 *prometheus-chart-name* 替換為您的 Prometheus 版本名稱。
   + 將 *prometheus-namespace* 替換為您的 Prometheus 命名空間的名稱。

   ```
   helm install prometheus-chart-name prometheus-community/prometheus -n prometheus-namespace \
   -f my_prometheus_values_yaml
   ```
**注意**  
您可以使用多種方式自訂 `helm install` 命令。如需詳細資訊，請參閱 *Helm 文件*中的 [Helm 安裝](https://helm.sh/docs/helm/helm_install/)。

# 在 EC2 上 Kubernetes 中設定現有 Prometheus 伺服器的擷取作業
<a name="AMP-onboard-ingest-metrics-existing-Prometheus"></a>

Amazon Managed Service for Prometheus 支援擷取 Prometheus 伺服器的指標，位於執行 Amazon EKS 的叢集以及在 Amazon EC2 中執行的自我管理 Kubernetes。本節中的詳細說明適用於 Amazon EKS 叢集中的 Prometheus 伺服器。除了您將需要在 Kubernetes 叢集中自行設定服務帳戶的 OIDC 提供者和 IAM 角色以外，Amazon EC2 上的自我管理 Kubernetes 叢集步驟皆相同。

本節中的指示使用 Helm 做為 Kubernetes 套件管理員。

**Topics**
+ [步驟 1：為服務帳戶設定 IAM 角色](#AMP-onboard-existing-Prometheus-IRSA)
+ [步驟 2：使用 Helm 升級您現有的 Prometheus 伺服器](#AMP-onboard-ingest-metrics-existing-remotewrite)

## 步驟 1：為服務帳戶設定 IAM 角色
<a name="AMP-onboard-existing-Prometheus-IRSA"></a>

若為我們正在記錄的入職方法，您需要在執行 Prometheus 伺服器的 Amazon EKS 叢集中使用服務帳戶的 IAM 角色。這些角色也稱為*服務角色*。

透過服務角色，您可以產生 IAM 角色與 Kubernetes 服務帳戶的關聯。然後，此服務帳戶可以為使用該服務帳戶的任何 Pod 中的容器提供 AWS 許可。如需詳細資訊，請參閱[服務帳戶的 IAM 角色](https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html)。

如果您尚未設定這些角色，請按照中的 [自 Amazon EKS 叢集設定指標擷取作業的服務角色](set-up-irsa.md#set-up-irsa-ingest) 指示設定角色。

## 步驟 2：使用 Helm 升級您現有的 Prometheus 伺服器
<a name="AMP-onboard-ingest-metrics-existing-remotewrite"></a>

本節中的說明包括設定遠端寫入和 sigv4 以進行驗證，並授權 Prometheus 伺服器遠端寫入 Amazon Managed Service for Prometheus 工作區。

### 使用 Prometheus 版本 2.26.0 或更新版本
<a name="AMP-onboard-ingest-metrics-Helm13"></a>

如果您稍後使用 Helm Chart 與版本 2.26.0 或更新版本的 Prometheus 伺服器，請按照下列步驟操作。

**使用 Helm Chart 從 Prometheus 伺服器設定遠端寫入**

1. 在 Helm 組態檔案中建立一個新的遠端寫入區段：
   + 將 `${IAM_PROXY_PROMETHEUS_ROLE_ARN}` 替換為您在 [步驟 1：為服務帳戶設定 IAM 角色](#AMP-onboard-existing-Prometheus-IRSA) 中建立 **amp-iamproxy-ingest-role** 的 ARN。角色 ARN 的格式應為 `arn:aws:iam::your account ID:role/amp-iamproxy-ingest-role`。
   + 將 `${WORKSPACE_ID}` 替換為 Amazon Managed Service for Prometheus 工作區 ID。
   + 將 `${REGION}` 替換為 Amazon Managed Service for Prometheus 工作區的地區 (例如 `us-west-2`)。

   ```
   ## The following is a set of default values for prometheus server helm chart which enable remoteWrite to AMP
       ## For the rest of prometheus helm chart values see: https://github.com/prometheus-community/helm-charts/blob/main/charts/prometheus/values.yaml
       ##
       serviceAccounts:
         server:
           name: amp-iamproxy-ingest-service-account
           annotations: 
             eks.amazonaws.com/role-arn: ${IAM_PROXY_PROMETHEUS_ROLE_ARN}
       server:
         remoteWrite:
           - url: https://aps-workspaces.${REGION}.amazonaws.com/workspaces/${WORKSPACE_ID}/api/v1/remote_write
             sigv4:
               region: ${REGION}
             queue_config:
               max_samples_per_send: 1000
               max_shards: 200
               capacity: 2500
   ```

1. 使用 Helm 更新您現有的 Prometheus 伺服器組態：
   + 將 `prometheus-chart-name` 替換為您的 Prometheus 版本名稱。
   + 將 `prometheus-namespace` 替換為安裝 Prometheus 伺服器的 Kubernetes 命名空間。
   + 將 `my_prometheus_values_yaml` 替換為 Helm 組態檔路徑。
   + 將 `current_helm_chart_version` 替換為您 Prometheus 伺服器 Helm Chart 的目前版本。您可以通過使用 [Helm list](https://helm.sh/docs/helm/helm_list/) 命令找到目前圖表版本。

   ```
   helm upgrade prometheus-chart-name prometheus-community/prometheus \
          -n prometheus-namespace \
          -f my_prometheus_values_yaml \
          --version current_helm_chart_version
   ```

### 使用早期 Prometheus 的版本
<a name="AMP-onboard-ingest-metrics-Helm8"></a>

若您正在使用 2.26.0 之前的 Prometheus 版本，請按照以下步驟操作。這些步驟使用附屬方法，因為舊版 Prometheus 本身不支援 AWS Signature 第 4 版簽署程序 (AWS SigV4)。

以下說明假設您使用 Helm 部署 Prometheus。

**從 Prometheus 伺服器設定遠端寫入**

1. 在您的 Prometheus 伺服器上，建立新的遠端寫入組態。首先，建立新的更新檔案。我們將呼叫該檔案 `amp_ingest_override_values.yaml`。

   將下列值新增至 YAML 檔案。

   ```
   serviceAccounts:
           server:
               name: "amp-iamproxy-ingest-service-account"
               annotations:
                   eks.amazonaws.com/role-arn: "${SERVICE_ACCOUNT_IAM_INGEST_ROLE_ARN}"
       server:
           sidecarContainers:
               - name: aws-sigv4-proxy-sidecar
                 image: public.ecr.aws/aws-observability/aws-sigv4-proxy:1.0
                 args:
                 - --name
                 - aps
                 - --region
                 - ${REGION}
                 - --host
                 - aps-workspaces.${REGION}.amazonaws.com
                 - --port
                 - :8005
                 ports:
                 - name: aws-sigv4-proxy
                   containerPort: 8005
           statefulSet:
               enabled: "true"
           remoteWrite:
               - url: http://localhost:8005/workspaces/${WORKSPACE_ID}/api/v1/remote_write
   ```

   將 `${REGION}` 替換為 Amazon Managed Service for Prometheus 工作區的地區。

   將 `${SERVICE_ACCOUNT_IAM_INGEST_ROLE_ARN}` 替換為您在 [步驟 1：為服務帳戶設定 IAM 角色](#AMP-onboard-existing-Prometheus-IRSA) 中建立 **amp-iamproxy-ingest-role** 的 ARN。角色 ARN 的格式應為 `arn:aws:iam::your account ID:role/amp-iamproxy-ingest-role`。

   將 `${WORKSPACE_ID}` 替換為工作區 ID。

1. 升級 Prometheus Helm Chart。首先，輸入下列命令尋找 Helm Chart 名稱。在此命令的輸出中，尋找名稱包含 `prometheus` 的圖表。

   ```
   helm ls --all-namespaces
   ```

   然後輸入下列命令。

   ```
   helm upgrade --install prometheus-helm-chart-name prometheus-community/prometheus -n prometheus-namespace -f ./amp_ingest_override_values.yaml
   ```

   將 *prometheus-helm-chart-name* 替換為上一個命令中傳回 Prometheus Helm Chart 的名稱。將 *prometheus-namespace* 替換為命名空間名稱。

#### 下載 Helm Chart
<a name="AMP-onboard-ingest-downloadHelm"></a>

如果您尚未在本機下載 Helm Chart，您可以使用以下命令進行下載。

```
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
    helm pull prometheus-community/prometheus --untar
```

# 在 Fargate 的 Kubernetes 從現有的 Prometheus 伺服器設定擷取作業
<a name="AMP-onboard-ingest-metrics-existing-Prometheus-fargate"></a>

Amazon Managed Service for Prometheus 支援在 Fargate 上執行的自我管理 Kubernetes 叢集中，從 Prometheus 伺服器擷取指標。若要從 Fargate 上執行 Amazon EKS 叢集中的 Prometheus 伺服器擷取指標，請覆寫名為 amp\$1ingest\$1override\$1values.yaml 組態檔中的預設組態，如下所示：

```
prometheus-node-exporter:
        enabled: false
    
    alertmanager:
        enabled: false
    
    serviceAccounts:
      server:
        name: amp-iamproxy-ingest-service-account
        annotations: 
          eks.amazonaws.com/role-arn: ${IAM_PROXY_PROMETHEUS_ROLE_ARN}
    
    server:
      persistentVolume:
        enabled: false
      remoteWrite:
        - url: https://aps-workspaces.${REGION}.amazonaws.com/workspaces/${WORKSPACE_ID}/api/v1/remote_write
          sigv4:
            region: ${REGION}
          queue_config:
            max_samples_per_send: 1000
            max_shards: 200
            capacity: 2500
```

使用透過以下命令覆寫安裝 Prometheus：

```
helm install prometheus-for-amp prometheus-community/prometheus \
                   -n prometheus \
                   -f amp_ingest_override_values.yaml
```

請注意，在 Helm Chart 組態中，我們停用節點匯出程式和警示管理員以及執行 Prometheus 伺服器部署。

您可以使用以下範例測試查詢驗證安裝程序。

```
$ awscurl --region region --service aps "https://aps-workspaces.region_id.amazonaws.com/workspaces/workspace_id/api/v1/query?query=prometheus_api_remote_read_queries"
    {"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"prometheus_api_remote_read_queries","instance":"localhost:9090","job":"prometheus"},"value":[1648461236.419,"0"]}]}}21
```

# 設定 Amazon Managed Service for Prometheus 以取得高可用性資料
<a name="AMP-ingest-high-availability"></a>

若您將資料傳送到 Amazon Managed Service for Prometheus 時，將會跨地區中的 AWS 個可用區域複製，並從提供可擴展性、可用性和安全性的主機叢集提供給您。根據特定設定，您可能需要額外的高可用性失效安全。有兩種常見的方法可以為您的設定提供額外的高可用性安全性：
+ 如果您有多個容器或執行個體具有相同資料，則可以將該資料傳送到 Amazon Managed Service for Prometheus，並自動刪除重複資料。這有助於確保將您的資料傳送到 Amazon Managed Service for Prometheus 工作區。

  如需有關刪除重複高可用性資料的詳細資訊，請參閱 [將傳送至 Amazon Managed Service for Prometheus 的高可用性指標刪除重複資料](AMP-ingest-dedupe.md)。
+ 若您想要確保資料有存取權，即使沒有 AWS 地區，您可將指標傳送給其他地區的第二個工作區。

  如需將指標資料傳送至多個工作區的詳細資訊，請參閱 [使用跨區域工作區在 Amazon Managed Service for Prometheus 中新增高可用性](AMP-send-to-multiple-workspaces.md)。

**Topics**
+ [將傳送至 Amazon Managed Service for Prometheus 的高可用性指標刪除重複資料](AMP-ingest-dedupe.md)
+ [使用 Prometheus 將高可用性資料傳送至 Amazon Managed Service for Prometheus](Send-high-availability-data.md)
+ [使用 Prometheus Operator Helm Chart 將高可用性資料設定為 Amazon Managed Service for Prometheus](Send-high-availability-data-operator.md)
+ [使用 AWS Distro for OpenTelemetry 將高可用性資料傳送至 Amazon Managed Service for Prometheus](Send-high-availability-data-ADOT.md)
+ [使用 Prometheus 社群 Helm Chart，將高可用性資料傳送至 Amazon Managed Service for Prometheus](Send-high-availability-prom-community.md)
+ [Amazon Managed Service for Prometheus 中高可用性組態的常見問題解答](HA_FAQ.md)
+ [使用跨區域工作區在 Amazon Managed Service for Prometheus 中新增高可用性](AMP-send-to-multiple-workspaces.md)

# 將傳送至 Amazon Managed Service for Prometheus 的高可用性指標刪除重複資料
<a name="AMP-ingest-dedupe"></a>

您可以將多位 Prometheus *客戶人員* (在客服人員模式中執行的 Prometheus 執行個體) 資料，傳送至 Amazon Managed Service for Prometheus 工作區。若其中一個執行個體正在記錄並傳送相同指標，您的資料的可用性將較高 (即使其中一位客服人員停止傳送資料，Amazon Managed Service for Prometheus 工作區仍將會收到其他執行個體的資料)。然而，您希望 Amazon Managed Service for Prometheus 工作區自動刪除重複指標，以便不想多次看到指標，進而防止多次收取資料擷取和儲存費用。

若要讓 Amazon Managed Service for Prometheus 自動從多個 Prometheus 代理程式刪除重複資料，您可以為傳送重複資料的一組代理程式提供單一*叢集名稱*，而每個執行個體都具有*複本名稱*。叢集名稱會將執行個體識別為具有共用資料，而複本名稱可讓 Amazon Managed Service for Prometheus 識別每個指標的來源。最終儲存的指標包含叢集標籤，但不包含複本，因此指標似乎來自單一來源。

**注意**  
某些版本的 Kubernetes (1.28 和 1.29) 可能會發出自己的指標和`cluster`標籤。這可能會導致 Amazon Managed Service for Prometheus 重複資料刪除的問題。如需詳細資訊，請參閱[高可用性常見問答集](HA_FAQ.md#HA_FAQ_cluster-label)。

下列主題說明如何傳送資料並包含 `cluster`和 `__replica__`標籤，以便 Amazon Managed Service for Prometheus 自動刪除重複資料。

**重要**  
若您未設定重複資料刪除功能，則所有傳送至 Amazon Managed Service for Prometheus 的資料範例都需收費。這些資料範例包括重複的範例。

# 使用 Prometheus 將高可用性資料傳送至 Amazon Managed Service for Prometheus
<a name="Send-high-availability-data"></a>

若要使用 Prometheus 設定高可用性組態，您必須在高可用性群組的所有執行個體上套用外部標籤，以供 Amazon Managed Service for Prometheus 進行識別。使用 `cluster` 標籤識別 Prometheus 執行個體代理程式作為高可用性群組的一部份。使用 `__replica__` 標籤分別識別群組中的每個複本。您需要同時套用 `__replica__` 和 `cluster` 標籤，以便重複資料刪除工作。

**注意**  
`__replica__` 標籤會在文字 `replica` 前後使用兩個底線符號進行格式化。

**範例：程式碼片段**

在下列程式碼片段中，`cluster` 標籤會識別 Prometheus 執行個體代理程式 `prom-team1`，而 `_replica_` 標籤會識別複本 `replica1` 和 `replica2`。

```
cluster: prom-team1
__replica__: replica1
```

```
cluster: prom-team1
__replica__: replica2
```

由於 Amazon Managed Service for Prometheus 會使用這些標籤儲存來自高可用性複本的資料範例，因此在接受範例時會剝離 `replica` 標籤。這表示只有當前序列的 1：1 序列對應，而不是每個複本的系列。`cluster` 標籤會保留下來。

**注意**  
某些版本的 Kubernetes (1.28 和 1.29) 可能會發出自己的指標和`cluster`標籤。這可能會導致 Amazon Managed Service for Prometheus 重複資料刪除的問題。如需詳細資訊，請參閱[高可用性常見問答集](HA_FAQ.md#HA_FAQ_cluster-label)。

# 使用 Prometheus Operator Helm Chart 將高可用性資料設定為 Amazon Managed Service for Prometheus
<a name="Send-high-availability-data-operator"></a>

若要使用 Helm 中的 Prometheus Operator 設定高可用性組態，您必須在高可用性群組的所有執行個體上套用外部標籤，以便 Amazon Managed Service for Prometheus 可以識別它們。您也必須在 Prometheus 操作員 Helm Chart 上設定屬性 `replicaExternalLabelName` 和 `externalLabels`。

**範例：YAML 標題**

在下列 YAML 標題中，`cluster` 會新增至 `externalLabel`，以將 Prometheus 執行個體代理程式識別為高可用性群組的一部份，而 `replicaExternalLabels` 會識別群組中的每個複本。

```
replicaExternalLabelName: __replica__
externalLabels:
cluster: prom-dev
```

**注意**  
某些版本的 Kubernetes (1.28 和 1.29) 可能會發出自己的指標和`cluster`標籤。這可能會導致 Amazon Managed Service for Prometheus 重複資料刪除的問題。如需詳細資訊，請參閱[高可用性常見問答集](HA_FAQ.md#HA_FAQ_cluster-label)。

# 使用 AWS Distro for OpenTelemetry 將高可用性資料傳送至 Amazon Managed Service for Prometheus
<a name="Send-high-availability-data-ADOT"></a>

AWS Distro for OpenTelemetry (ADOT) 是 OpenTelemetry 專案的安全且可供生產使用的分佈。ADOT 為您提供來源 API、程式庫和代理程式，因此您可以收集分散式追蹤和指標以進行應用程式監控。如需 ADOT 的相關資訊，請參閱[關於 AWS Distro for Open Telemetry](https://aws-otel.github.io/about)。

若要使用高可用性組態設定 ADOT，您必須設定 ADOT 收集器容器映像，並將外部標籤`cluster`和 `__replica__` 套用至 AWS Prometheus 遠端寫入匯出程式。此匯出程式會透過 `remote_write` 端點將您抓取的指標傳送到 Amazon Managed Service for Prometheus 工作區。當您在遠端寫入匯出程式上設定這些標籤時，可避免在執行備援複本時保留重複的指標。如需 AWS Prometheus 遠端寫入匯出器的詳細資訊，請參閱[適用於 Amazon Managed Service for Prometheus 的 Prometheus 遠端寫入匯出器入門](https://aws-otel.github.io/docs/getting-started/prometheus-remote-write-exporter)。

**注意**  
某些版本的 Kubernetes (1.28 和 1.29) 可能會發出自己的指標和`cluster`標籤。這可能會導致 Amazon Managed Service for Prometheus 重複資料刪除的問題。如需詳細資訊，請參閱[高可用性常見問答集](HA_FAQ.md#HA_FAQ_cluster-label)。

# 使用 Prometheus 社群 Helm Chart，將高可用性資料傳送至 Amazon Managed Service for Prometheus
<a name="Send-high-availability-prom-community"></a>

若要使用 Prometheus 社群 Helm chart 設定高可用性組態，您必須在高可用性群組的所有執行個體上套用外部標籤，以便 Amazon Managed Service for Prometheus 進行識別。下面是如何從 Prometheus 社區 Helm Chart 將 `external_labels` 新增至 Prometheus 單一執行個體的範例。

```
server:
global:
  external_labels:
      cluster: monitoring-cluster
      __replica__: replica-1
```

**注意**  
如果您想要多個複本，您必須使用不同的複本值多次部署圖表，因為 Prometheus 社群 Helm Chart 不允許直接從控制器群組增加複本數量時動態設定複本值。如果您想要自動設定 `replica` 標籤，請使用 Prometheus 操作員 Helm Chart。

**注意**  
某些版本的 Kubernetes (1.28 和 1.29) 可能會發出自己的指標和`cluster`標籤。這可能會導致 Amazon Managed Service for Prometheus 重複資料刪除的問題。如需詳細資訊，請參閱[高可用性常見問答集](HA_FAQ.md#HA_FAQ_cluster-label)。

# Amazon Managed Service for Prometheus 中高可用性組態的常見問題解答
<a name="HA_FAQ"></a>

## 我是否應將 *\$1\$1replica\$1\$1* 值納入另一個標籤，以追蹤範例點？
<a name="HA_FAQ_replica-label"></a>

 在高可用性設定中，Amazon Managed Service for Prometheus 可透過選擇 Prometheus 執行個體叢集中的領導者，以確保資料範例不會重複。若領導者複本停止傳送資料範例 30 秒，Amazon Managed Service for Prometheus 會自動將另一個 Prometheus 執行個體設為領導者複本，並從新領導者擷取資料，包括任何遺漏的資料。因此，答案為否，不建議執行。 這樣做可能會導致以下問題：
+  在選舉新領導者的期間，在 **PromQL** 中查詢 `count` 可能會傳回高於預期的值。
+  在選舉新領導者期間增加的 `active series` 數量，這會到達 `active series limits`。如需詳細資訊，請參閱 [AMP 配額](https://docs.aws.amazon.com/prometheus/latest/userguide/AMP_quotas.html)中的配額。

## Kubernetes 似乎具有自己的*叢集*標籤，而且不會刪除重複的指標。我要如何修正這個情形？
<a name="HA_FAQ_cluster-label"></a>

Kubernetes 1.28 `apiserver_storage_size_bytes`引進了具有`cluster`標籤的新指標。這可能會導致 Amazon Managed Service for Prometheus 中重複資料刪除的問題，這取決於`cluster`標籤。在 Kubernetes 1.3 中，標籤會重新命名為 `storage-cluster_id`（在 1.28 和 1.29 的更新修補程式中也會重新命名）。如果您的叢集使用 `cluster`標籤發出此指標，Amazon Managed Service for Prometheus 就無法刪除相關聯的時間序列。建議您將 Kubernetes 叢集升級至最新的修補版本，以避免此問題。或者，您可以在將指標擷取至 Amazon Managed Service for Prometheus 之前重新標記`apiserver_storage_size_bytes`指標上的`cluster`標籤。

**注意**  
如需 Kubernetes 變更的詳細資訊，請參閱 *Kubernetes GitHub 專案*中的將[標籤叢集重新命名為 apiserver\$1storage\$1size\$1bytes 指標的 storage\$1cluster\$1id](https://github.com/kubernetes/kubernetes/pull/124283)。

# 使用跨區域工作區在 Amazon Managed Service for Prometheus 中新增高可用性
<a name="AMP-send-to-multiple-workspaces"></a>

若要將跨區域可用性新增至您的資料，您可以將指標傳送至跨 AWS 區域的多個工作區。Prometheus 支持多個編寫器和跨區域編寫。

下列範例顯示如何設定在代理程式模式下執行的 Prometheus 伺服器，以便將指標傳送至位於不同區域中的兩個工作區。

```
extensions:
      sigv4auth:
        service: "aps"
     
    receivers:
      prometheus:
        config:
          scrape_configs:
            - job_name: 'kubernetes-kubelet'
              scheme: https
              tls_config:
                ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
                insecure_skip_verify: true
              bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
              kubernetes_sd_configs:
              - role: node
              relabel_configs:
              - action: labelmap
                regex: __meta_kubernetes_node_label_(.+)
              - target_label: __address__
                replacement: kubernetes.default.svc.cluster.local:443
              - source_labels: [__meta_kubernetes_node_name]
                regex: (.+)
                target_label: __metrics_path__
                replacement: /api/v1/nodes/$${1}/proxy/metrics
     
    exporters:
      prometheusremotewrite/one:
        endpoint: "https://aps-workspaces.workspace_1_region.amazonaws.com/workspaces/ws-workspace_1_id/api/v1/remote_write"
        auth:
          authenticator: sigv4auth
      prometheusremotewrite/two:
        endpoint: "https://aps-workspaces.workspace_2_region.amazonaws.com/workspaces/ws-workspace_2_id/api/v1/remote_write"
        auth:
          authenticator: sigv4auth
     
    service:
      extensions: [sigv4auth]
      pipelines:
        metrics/one:
          receivers: [prometheus]
          exporters: [prometheusremotewrite/one]
        metrics/two:
          receivers: [prometheus]
          exporters: [prometheusremotewrite/two]
```