Prometheus에서 알림 규칙 및 수신자 마이그레이션

이 페이지에서는 PromQL 기반 조건을 사용하여 Prometheus 알림 규칙을 Cloud Monitoring 알림 정책으로 마이그레이션하는 방법을 설명합니다. 마이그레이션 프로세스는 Google Cloud 외부 환경에서 PromQL을 사용하는 경우에 유용합니다. Prometheus 알림 규칙을 Cloud Monitoring으로 마이그레이션하면 모든 알림 정책을 하나의 환경에서 관리할 수 있습니다.

PromQL 기반 조건의 알림 정책에 대한 일반적인 정보는 PromQL을 사용한 알림 정책을 참조하세요.

Grafana 대시보드를 가져올 수도 있습니다. 자세한 내용은 Cloud Monitoring으로 Grafana 대시보드 가져오기를 참조하세요.

마이그레이션 작동 방식

gcloud CLI에서 기존 알림 규칙과 수신자를 마이그레이션하여 Cloud Monitoring 알림 정책과 알림 채널을 만들 수 있습니다. gcloud CLI는 다음 유형의 마이그레이션을 수행할 수 있습니다.

  • 알림 규칙에 Prometheus 구성 파일을 제공하면 gcloud CLI가 PromQL 기반 조건을 사용하여 알림 정책을 만듭니다.

    PromQL 기반 조건을 사용하여 여러 알림 정책을 동시에 만들어 여러 Prometheus 구성 파일을 제공할 수 있습니다.

  • 하나 이상의 수신자에 Prometheus Alertmanager 파일을 제공하면 gcloud CLI가 각 수신자를 Monitoring의 동등한 알림 채널로 변환합니다.

  • Prometheus Alertmanager 파일과 Prometheus 구성 파일을 동시에 제공하면 gcloud CLI가 알림 정책과 알림 채널을 만든 후 각 새 알림 채널을 새 알림 정책과 연결합니다.

    알림 규칙이 포함된 구성 파일을 여러 개 제공하면 gcloud CLI가 Alertmanager 파일의 모든 알림 채널을 각각의 새 알림 정책에 할당합니다.

Prometheus 알림 규칙이 커스텀 측정항목을 참조하는 경우 gcloud CLI에서 알림 정책을 만들려면 동등한 측정항목 설명이 필요합니다.

Prometheus 구성 파일과 Prometheus Alertmanager 파일은 YAML 또는 JSON에서 유효한 Prometheus 규칙 파일 형식이어야 합니다.

필드 매핑

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

마이그레이션된 Prometheus 알림 규칙의 Google Cloud 문서 변수

Prometheus는 알림 규칙 주석과 라벨의 변수를 위해 알림 필드 템플릿을 사용합니다. 알림 필드 템플릿이 있는 Prometheus 알림 규칙을 마이그레이션하면 Cloud Monitoring은 다음과 같이 템플릿을 Cloud Monitoring 문서 변수로 변환합니다.

Prometheus 알림 필드 템플릿 Cloud Monitoring 문서 변수
{{$value}}

${metric.label.value}

이 변수는 ${metric.label.KEY}의 구현으로, 여기서 value는 PromQL 쿼리의 값을 포함합니다.

{{humanize $value}}

${metric.label.value}

이 변수는 ${metric.label.KEY}의 구현으로, 여기서 value는 PromQL 쿼리의 값을 포함합니다.

{{$labels}} ${metric_or_resource.labels}
{{humanize $labels}} ${metric_or_resource.labels}
{{$labels.key}}

알림 정책 문서 구성에서 ${metric_or_resource.label.KEY}로 표시됩니다.

  • KEY가 유효한 라벨이면 이 변수는 알림에서 ${metric.label.KEY} 값으로 렌더링됩니다.
  • KEY가 유효한 리소스면 이 변수는 알림에서 ${resource.label.KEY} 값으로 렌더링됩니다.
  • KEY가 유효한 라벨이나 유효한 리소스가 아니면 이 변수는 알림에서 빈 문자열로 렌더링됩니다.
{{humanize $labels.<key>}}

알림 정책 문서 구성에서 ${metric_or_resource.label.KEY}로 표시됩니다.

  • KEY가 유효한 라벨이면 이 변수는 알림에서 ${metric.label.KEY} 값으로 렌더링됩니다.
  • KEY가 유효한 리소스면 이 변수는 알림에서 ${resource.label.KEY} 값으로 렌더링됩니다.
  • KEY가 유효한 라벨이나 유효한 리소스가 아니면 이 변수는 알림에서 빈 문자열로 렌더링됩니다.

Prometheus에서 알림 규칙 및 수신자 마이그레이션

PromQL 기반 조건을 사용해서 Prometheus 알림 규칙을 Cloud Monitoring 알림 정책으로 마이그레이션하려면 gcloud alpha monitoring policies migrate 명령어를 사용합니다. 이 명령어를 사용하여 Prometheus 수신자를 Cloud Monitoring 알림 채널로 마이그레이션할 수도 있습니다.

  • 알림 규칙을 마이그레이션하려면 알림 규칙이 포함된 YAML 또는 JSON Prometheus 규칙 파일 파일이 있어야 합니다.
  • 수신자를 마이그레이션하려면 수신자를 포함하는 YAML 또는 JSON Prometheus Alertmanager 파일을 사용해야 합니다.

Google Cloud는 Prometheus 레코딩 규칙 마이그레이션을 지원하지 않습니다. 기록 규칙을 사용하려면 자체 배포 규칙 평가자를 사용하는 것이 좋습니다.

Prometheus 구성 파일에서 알림 정책 만들기

Prometheus 구성 파일에서 알림 정책을 만들려면 다음 명령어를 실행하세요. 여러 파일 경로를 제공하여 알림 정책을 여러 개 만들 수 있습니다.

gcloud alpha monitoring policies migrate \
--policies-from-prometheus-alert-rules-yaml=PATH_TO_FILE_1,PATH_TO_FILE_2

Prometheus Alertmanager 구성 파일에서 알림 채널 만들기

Prometheus Alertmanager 구성 파일에서 알림 채널을 만들려면 다음 명령어를 실행합니다.

gcloud alpha monitoring policies migrate \
--channels-from-prometheus-alertmanager-yaml=PATH_TO_FILE

알림 정책과 알림 채널을 동시에 만들기

Prometheus 구성 파일에서 알림 정책을 만들고 동시에 Prometheus Alertmanager 구성 파일에서 알림 채널을 만들려면 다음 명령어를 실행합니다.

gcloud alpha 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

예시

CPU 사용률이 1분 동안 75% 를 초과하면 알림을 보내는 알림 규칙이 있는 Prometheus 구성 파일이 있습니다. 알림 규칙은 다음과 같이 구성됩니다.

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%"

또한 이메일, PagerDuty, 웹훅 수신자를 포함하는 Prometheus AlertManager 파일이 있습니다.

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 alpha 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 알림 정책으로 마이그레이션하려면 다음 안내를 따르세요.

  1. 커스텀 리소스의 groups 섹션을 새 Prometheus 규칙 파일에 복사합니다.

  2. 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

이전 Rules 커스텀 리소스에서 groups를 복사하여 생성된 Prometheus 규칙 파일은 다음과 같습니다.

  groups:
  - name: example
    interval: 30s
    rules:
    - alert: FewerThanTenInstancesUp
      expr: sum(up) < 10

여러 Rules 커스텀 리소스 마이그레이션

여러 Rules, CustomRules, GlobalRules를 마이그레이션하려면 리소스마다 별도의 규칙 파일을 구성하고 마이그레이션하는 것이 좋습니다. 자세한 내용은 Prometheus 문서를 참조하세요.

필터 및 집계 표현식 관리

Rules 및 ClusterRules는 특정 라벨을 기준으로 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이 의도한 대로 쿼리를 실행하도록 하는 것이 좋습니다. 예를 들어 Rules 커스텀 리소스에서 규칙 파일을 만들 때 sum(up) < 10을 다음 표현식으로 변경할 수 있습니다.

sum (up) by (project_id, cluster, namespace) < 10

이전 표현식은 Google Cloud 프로젝트, 클러스터, 네임스페이스마다 별도의 시계열을 생성한 후 각 시계열을 알림 기준 <10과 비교합니다.

다음 표에서는 Rules 및 ClusterRules의 알림 표현식 필터 평가 순서를 보여줍니다.

규칙 리소스 유형 필터 라벨
Rule
  1. project_id
  2. cluster
  3. namespace
ClusterRule
  1. project_id
  2. cluster

Prometheus 표현식의 필터링 동작에 대한 자세한 내용은 인스턴트 벡터 선택기를 참조하세요.