

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

# 使用 Amazon Managed Service for Prometheus 監控 Spark 指標
<a name="monitor-with-prometheus"></a>

透過 Amazon EMR 7.1.0 版及更高版本，您可以將 EMR Serverless 與 Amazon Managed Service for Prometheus 整合，以收集 EMR Serverless 任務和應用程式的 Apache Spark 指標。當您使用 AWS 主控台、EMR Serverless API 或 提交任務或建立應用程式時，即可使用此整合 AWS CLI。

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

在您將 Spark 指標交付至 Amazon Managed Service for Prometheus 之前，請先完成下列先決條件。
+ [建立 Amazon Managed Service for Prometheus 工作區。](https://docs.aws.amazon.com/prometheus/latest/userguide/AMP-onboard-create-workspace.html)此工作區用作擷取端點。記下針對**端點 - 遠端寫入 URL 顯示的 URL**。建立 EMR Serverless 應用程式時，您需要指定 URL。
+ 若要將任務的存取權授予 Amazon Managed Service for Prometheus 以進行監控，請將下列政策新增至您的任務執行角色。

  ```
  {
      "Sid": "AccessToPrometheus",
      "Effect": "Allow",
      "Action": ["aps:RemoteWrite"],
      "Resource": "arn:aws:aps:{{<AWS_REGION>}}:{{<AWS_ACCOUNT_ID>}}:workspace/{{<WORKSPACE_ID>}}"
  }
  ```

## 設定
<a name="monitoring-with-prometheus-setup"></a>

**使用 AWS 主控台建立與 Amazon Managed Service for Prometheus 整合的應用程式**

1. 請參閱 [Amazon EMR Serverless 入門](https://docs.aws.amazon.com/emr/latest/EMR-Serverless-UserGuide/getting-started.html                             )以建立應用程式。

1. 當您建立應用程式時，請選擇**使用自訂設定**，然後將資訊指定至您要設定的欄位，以設定您的應用程式。

1. 在**應用程式日誌和指標**下，選擇將**引擎指標交付至 Amazon Managed Service for Prometheus**，然後指定遠端寫入 URL。

1. 指定您想要的任何其他組態設定，然後選擇**建立和啟動應用程式**。

**使用 AWS CLI 或 EMR Serverless API**

當您執行 AWS CLI 或 `start-job-run`命令時，您也可以使用 `create-application`或 EMR Serverless API 將您的 EMR Serverless 應用程式與 Amazon Managed Service for Prometheus 整合。

------
#### [ create-application ]

```
aws emr-serverless create-application \
--release-label emr-7.1.0 \
--type "SPARK" \
--monitoring-configuration '{ 
    "prometheusMonitoringConfiguration": {
        "remoteWriteUrl": "https://aps-workspaces.{{<AWS_REGION>}}.amazonaws.com/workspaces/{{<WORKSPACE_ID>}}/api/v1/remote_write"
    }
}'
```

------
#### [ start-job-run ]

```
aws emr-serverless start-job-run \
--application-id {{<APPPLICATION_ID>}} \
--execution-role-arn {{<JOB_EXECUTION_ROLE>}} \
--job-driver '{
    "sparkSubmit": {
        "entryPoint": "local:///usr/lib/spark/examples/src/main/python/pi.py",
        "entryPointArguments": ["10000"],
        "sparkSubmitParameters": "--conf spark.dynamicAllocation.maxExecutors=10"
    }
}' \
--configuration-overrides '{
     "monitoringConfiguration": {
        "prometheusMonitoringConfiguration": {
            "remoteWriteUrl": "https://aps-workspaces.{{<AWS_REGION>}}.amazonaws.com/workspaces/{{<WORKSPACE_ID>}}/api/v1/remote_write"
        }
    }
}'
```

------

在您的命令`prometheusMonitoringConfiguration`中包含 表示 EMR Serverless 必須使用收集 Spark 指標的代理程式來執行 Spark 任務，並將其寫入 Amazon Managed Service for Prometheus 的`remoteWriteUrl`端點。然後，您可以使用 Amazon Managed Service for Prometheus 中的 Spark 指標進行視覺化、提醒和分析。

## 進階組態屬性
<a name="monitoring-with-prometheus-config-options"></a>

EMR Serverless 在 Spark 中使用名為 的元件`PrometheusServlet`來收集 Spark 指標，並將效能資料轉譯為與 Amazon Managed Service for Prometheus 相容的資料。根據預設，EMR Serverless 會在 Spark 中設定預設值，並在您使用 提交任務時剖析驅動程式和執行器指標`PrometheusMonitoringConfiguration`。

下表說明在提交將指標傳送至 Amazon Managed Service for Prometheus 的 Spark 任務時設定的所有屬性。


| Spark 屬性 | 預設值 | Description | 
| --- | --- | --- | 
| spark.metrics.conf.\*.sink.prometheusServlet.class | org.apache.spark.metrics.sink.PrometheusServlet | Spark 用來將指標傳送至 Amazon Managed Service for Prometheus 的類別。若要覆寫預設行為，請指定您自己的自訂類別。 | 
| spark.metrics.conf.\*.source.jvm.class | org.apache.spark.metrics.source.JvmSource | 類別 Spark 使用 從基礎 Java 虛擬機器收集和傳送關鍵指標。若要停止收集 JVM 指標，請將其設定為空字串來停用此屬性，例如 `""`。若要覆寫預設行為，請指定您自己的自訂類別。 | 
| spark.metrics.conf.driver.sink.prometheusServlet.path | /metrics/prometheus | Amazon Managed Service for Prometheus 用來從驅動程式收集指標的不同 URL。若要覆寫預設行為，請指定您自己的路徑。若要停止收集驅動程式指標，請將其設定為空字串來停用此屬性，例如 `""`。 | 
| spark.metrics.conf.executor.sink.prometheusServlet.path | /metrics/executor/prometheus | Amazon Managed Service for Prometheus 用來從執行器收集指標的不同 URL。若要覆寫預設行為，請指定您自己的路徑。若要停止收集執行器指標，請將其設定為空字串來停用此屬性，例如 `""`。 | 

如需 Spark 指標的詳細資訊，請參閱 [Apache Spark 指標](https://spark.apache.org/docs/latest/monitoring.html#metrics)。

## 考量和限制
<a name="monitoring-with-prometheus-limitations"></a>

使用 Amazon Managed Service for Prometheus 從 EMR Serverless 收集指標時，請考慮下列考量和限制。
+ Amazon Managed Service for Prometheus 與 EMR Serverless 搭配使用的支援僅適用於 [AWS 區域 Amazon Managed Service for Prometheus 已全面推出的地方。](https://docs.aws.amazon.com/general/latest/gr/prometheus-service.html)
+ 在 Amazon Managed Service for Prometheus 上執行代理程式以收集 Spark 指標需要更多工作者的資源。如果您選擇較小的工作者大小，例如一個 vCPU 工作者，您的任務執行時間可能會增加。
+ Amazon Managed Service for Prometheus 搭配 EMR Serverless 的支援僅適用於 Amazon EMR 7.1.0 版和更新版本。
+ Amazon Managed Service for Prometheus 必須部署在您執行 EMR Serverless 以收集指標的相同帳戶中。