本頁面說明如何將 Prometheus 快訊規則遷移至 Cloud Monitoring 中以 PromQL 為基礎的快訊政策。如果您在 Google Cloud以外的環境中使用 PromQL,遷移程序會很有幫助。將 Prometheus 警報規則遷移至 Cloud Monitoring 後,您就能在單一環境中管理所有警報政策。
如需以 PromQL 為基礎的快訊政策一般資訊,請參閱 PromQL 快訊總覽。
您也可以匯入 Grafana 資訊主頁。詳情請參閱將 Grafana 資訊主頁匯入 Cloud Monitoring。
遷移作業的運作方式
在 gcloud CLI 中,您可以遷移現有的快訊規則和接收者,建立 Cloud Monitoring 快訊政策和通知管道。gcloud CLI 可執行下列類型的遷移作業:
如果您提供含有警告規則的 Prometheus 設定檔,gcloud CLI 就會建立以 PromQL 為基礎的警告政策。
您可以同時提供多個 Prometheus 設定檔,建立多個以 PromQL 為基礎的警報政策。
如果您提供至少有一個接收者的 Prometheus Alertmanager 檔案,gcloud CLI 會將每個接收者轉換為 Monitoring 中的對等通知管道。
如果您同時提供 Prometheus Alertmanager 檔案和 Prometheus 設定檔,gcloud CLI 會建立快訊政策和通知管道,然後將每個新通知管道與新快訊政策建立關聯。
如果您提供多個含有快訊規則的設定檔,gcloud CLI 會將 Alertmanager 檔案中的每個通知管道指派給各項新快訊政策。
如果 Prometheus 警告規則參照自訂指標,則 gcloud CLI 需要對應的指標描述元,才能建立警告政策。
Prometheus 設定檔和 Prometheus Alertmanager 檔案必須格式化為有效的 Prometheus 規則檔案 (YAML 或 JSON 格式)。
欄位對應
gcloud CLI 會將 Prometheus 警報規則和 Cloud Monitoring 警報政策之間的欄位對應如下:
| Prometheus 快訊規則 | Cloud Monitoring 快訊政策 |
|---|---|
alert |
conditionPrometheusQueryLanguage.alertRule |
expr |
conditionPrometheusQueryLanguage.query |
for |
conditionPrometheusQueryLanguage.duration |
labels |
conditionPrometheusQueryLanguage.labels |
annotations: "summary" |
documentation.subject |
annotations: "description" |
documentation.content |
Google Cloud 已遷移 Prometheus 快訊規則中的說明文件變數
Prometheus 會使用快訊欄位範本,在快訊規則註解和標籤中設定變數。遷移含有快訊欄位範本的 Prometheus 快訊規則時,Cloud Monitoring 會將範本轉換為 Cloud Monitoring 文件變數,轉換方式如下:
| Prometheus 快訊欄位範本 | Cloud Monitoring 說明文件變數 |
|---|---|
{{$value}} |
這個變數是 |
{{humanize $value}} |
這個變數是 |
{{$labels}} |
${metric_or_resource.labels} |
{{humanize $labels}} |
${metric_or_resource.labels} |
{{$labels.key}} |
在快訊政策說明文件設定中顯示為
|
{{humanize $labels.<key>}} |
在快訊政策說明文件設定中顯示為
|
從 Prometheus 遷移快訊規則和接收器
如要將 Prometheus 警報規則遷移至 Cloud Monitoring 的 PromQL 警報政策,請使用 gcloud monitoring policies
migrate 指令。您也可以使用這個指令,將 Prometheus 接收器遷移至 Cloud Monitoring 通知管道。
- 如要遷移警報規則,您必須備妥含有警報規則的 YAML 或 JSON Prometheus 規則檔案。
如要遷移接收器,您必須使用 YAML 或 JSON 格式的 Prometheus Alertmanager 檔案,其中包含接收器。
Google Cloud 不支援遷移 Prometheus 記錄規則。如要使用記錄規則,建議您使用自行部署的規則評估工具。
透過 Prometheus 設定檔建立快訊政策
如要從 Prometheus 設定檔建立快訊政策,請執行下列指令。您可以提供多個檔案路徑,建立多項警告政策:
gcloud monitoring policies migrate \ --policies-from-prometheus-alert-rules-yaml=PATH_TO_FILE_1,PATH_TO_FILE_2
從 Prometheus Alertmanager 設定檔建立通知管道
如要從 Prometheus Alertmanager 設定檔建立通知管道,請執行下列指令:
gcloud monitoring policies migrate \ --channels-from-prometheus-alertmanager-yaml=PATH_TO_FILE
同時建立快訊政策和通知管道
如要從 Prometheus 設定檔建立快訊政策,並同時從 Prometheus Alertmanager 設定檔建立通知管道,請執行下列指令:
gcloud monitoring policies migrate \ --policies-from-prometheus-alert-rules-yaml=PATH_TO_FILE_1,PATH_TO_FILE_2 \ --channels-from-prometheus-alertmanager-yaml=PATH_TO_FILE
範例
您有一個 Prometheus 設定檔,其中包含一項快訊規則,如果 CPU 使用率超過 75% 達一分鐘,就會發出快訊。警報規則設定如下:
groups:
- name: Utilization_Alerts
interval: 30s
rules:
- alert: CPU_Utilization_Over_75
expr: compute_googleapis_com:instance_cpu_utilization > 75
for: 60s
labels:
severity: page
annotations:
subject: "Subject: ${metric.label.value}"
description: "CPU utilization is over 75%"
您也有 Prometheus AlertManager 檔案,其中包含電子郵件、PagerDuty 和 Webhook 接收器。
receivers: - name: 'team-emails' email_configs: - to: EMAIL_ADDRESS - name: 'team-pager' pagerduty_configs: - service_key: SERVICE_KEY - name: 'team-webhook' webhook_configs: - url: WEBHOOK_ADDRESS
如要建立快訊政策和通知管道,請執行下列指令:
gcloud monitoring policies migrate \ --policies-from-prometheus-alert-rules-yaml=PATH_TO_CONFIG_FILE \ --channels-from-prometheus-alertmanager-yaml=PATH_TO_ALERTMANAGER_FILE
接著,gcloud CLI 會建立類似下列範例的快訊政策:
{
"name": "projects/PROJECT_ID/alertPolicies/POLICY_ID",
"displayName": "Utilization_Alerts/CPU_Utilization_Over_75",
"documentation": {
"content": "CPU utilization is over 75%",
"mimeType": "text/markdown",
"subject": "Subject: ${metric.label.value}"
},
"userLabels": {},
"conditions": [
{
"name": "projects/PROJECT_ID/alertPolicies/POLICY_ID/conditions/CONDITION_ID",
"displayName": "CPU_Utilization_Over_75"
"conditionPrometheusQueryLanguage": {
"alertRule": "CPU_Utilization_Over_75",
"duration": "60s",
"evaluationInterval": "30s",
"labels": {
"severity": "page"
},
"query": "compute_googleapis_com:instance_cpu_utilization > 75",
"ruleGroup": "Utilization_Alerts"
}
}
],
"alertStrategy": {},
"combiner": "OR",
"enabled": true,
"notificationChannels": [
"projects/google.com:PROJECT_ID/notificationChannels/CHANNEL_ID_1",
"projects/google.com:PROJECT_ID/notificationChannels/CHANNEL_ID_2",
"projects/google.com:PROJECT_ID/notificationChannels/CHANNEL_ID_3"
]
}
從 Managed Service for Prometheus 遷移快訊規則
Google Cloud 使用 Rules、ClusterRules 和 GlobalRules 自訂資源,為 Managed Service for Prometheus 設定代管規則評估。
如要將這些資源遷移至以 PromQL 為基礎的快訊政策,請按照下列步驟操作: Google Cloud
將自訂資源的
groups區段複製到新的 Prometheus 規則檔案。使用 gcloud CLI 遷移規則檔案。
舉例來說,您有下列 Rules 自訂資源:
apiVersion: monitoring.googleapis.com/v1
kind: Rules
metadata:
namespace: NAMESPACE_NAME
name: example-rules
spec:
groups:
- name: example
interval: 30s
rules:
- alert: FewerThanTenInstancesUp
expr: sum(up) < 10
從先前的規則自訂資源複製 groups 所建立的 Prometheus 規則檔案,看起來會像這樣:
groups:
- name: example
interval: 30s
rules:
- alert: FewerThanTenInstancesUp
expr: sum(up) < 10
遷移多個 Rules 自訂資源
如要遷移多個規則、CustomRule 或 GlobalRule,建議您為每個資源建構及遷移個別規則檔案。詳情請參閱 Prometheus 說明文件。
管理篩選器和匯總運算式
規則和 ClusterRule 會根據特定標籤,自動篩選 Prometheus 警告運算式。如要在遷移的快訊政策查詢中保留這項行為,您必須先在 Prometheus 規則檔案中,明確將這些標籤納入篩選器或彙整運算式,再遷移檔案。
舉例來說,Managed Service for Prometheus 的代管規則評估工具會執行 sum(up) < 10 運算式,如下所示:
sum(up{project_id="test-project", cluster="test-cluster", namespace="NAMESPACE_NAME"}) < 10
不過,如果警告政策包含 PromQL 查詢 sum(up) < 10,Cloud Monitoring 只會執行 sum(up) < 10 查詢。這項查詢會套用至指標範圍內的每個 Google Cloud 專案、叢集和命名空間。為避免這種情況,建議您在查詢中依標籤匯總,確保 Cloud Monitoring 依預期執行查詢。舉例來說,從「規則」自訂資源建立規則檔案時,您可以將 sum(up) < 10 變更為下列運算式:
sum (up) by (project_id, cluster, namespace) < 10
上一個運算式會為每個Google Cloud 專案、叢集和命名空間產生個別的時間序列,然後將每個時間序列與 <10 的警報門檻進行比較。
下表顯示規則和 ClusterRule 的警報運算式篩選器評估順序:
| 規則資源類型 | 篩選標籤 |
|---|---|
| 規則 |
|
| ClusterRule |
|
如要進一步瞭解 Prometheus 運算式中的篩選行為,請參閱即時向量選取器。