

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

# 在 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\_sd\_config>](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\_write** 端點。查看工作區詳細資料時，請使用在 Amazon Managed Service for Prometheus 主控台中顯示的 **remote\_write** 端點。

   您也需要在 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 組態，則必須將 `$` 個字元替換為 `$$` 以避免將值替換為環境變數。\* 這對於 relabel\_configurations 的替換值特別重要。例如，若您開始使用下列 relabel\_configuration：

```
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 的遠端寫入權限。