알림 템플릿 예시

알림 템플릿 예시 (Notification template examples)

이 문서는 다양한 알림과 그에 대응하는 Alertmanager 설정 파일(alertmanager.yml) 예시를 보여줘요. 각 예시는 Go 템플릿 시스템을 사용합니다.

템플릿 문법을 익히면 Slack 같은 수신자에 보내는 메시지의 내용과 형식을 자유롭게 바꿀 수 있어요. 여기서는 Slack을 기준으로 네 가지 패턴(URL 커스터마이징, annotation 접근, 전체 알림 순회, 재사용 템플릿 정의)을 다룹니다.

출처: 문서

본문

다음은 모두 다른 종류의 알림과 그에 대응하는 Alertmanager 설정 파일(alertmanager.yml) 예시입니다. 각각은 Go 템플릿 시스템을 사용합니다.

Slack 알림 커스터마이징

이 예시에서는 받은 특정 알림을 어떻게 처리해야 하는지에 대한 우리 조직의 위키로 가는 URL을 보내도록 Slack 알림을 커스터마이즈했습니다.

global:
  # 파일에 이 URL을 넣는 것도 가능.
  # 예: `slack_api_url_file: '/etc/alertmanager/slack_url'`
  slack_api_url: 'https://hooks.slack.com/services/...'

route:
  receiver: 'slack-notifications'
  group_by: [alertname, datacenter, app]

receivers:
- name: 'slack-notifications'
  slack_configs:
  - channel: '#alerts'
    text: 'https://internal.myorg.net/wiki/alerts/{{ .GroupLabels.app }}/{{ .GroupLabels.alertname }}'

CommonAnnotations에서 annotation 접근

이 예시에서는 Alertmanager가 보내는 데이터의 CommonAnnotations에 저장된 summarydescription에 접근해 Slack 수신자에게 보내는 텍스트를 다시 커스터마이즈합니다.

알림 (Alert)

groups:
- name: Instances
  rules:
  - alert: InstanceDown
    expr: up == 0
    for: 5m
    labels:
      severity: page
    # Prometheus 템플릿은 알림의 annotation과 label 필드에 여기서 적용된다.
    annotations:
      description: '{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes.'
      summary: 'Instance {{ $labels.instance }} down'

수신자 (Receiver)

- name: 'team-x'
  slack_configs:
  - channel: '#alerts'
    # Alertmanager 템플릿이 여기서 적용된다.
    text: " \nsummary: {{ .CommonAnnotations.summary }}\ndescription: {{ .CommonAnnotations.description }}"

수신된 모든 알림 순회하기

마지막으로, 이전 예시와 같은 알림을 가정해, 우리는 수신자가 Alertmanager에서 받은 모든 알림을 순회해 각각의 annotation 요약과 설명을 새 줄에 출력하도록 커스터마이즈합니다.

수신자 (Receiver)

- name: 'default-receiver'
  slack_configs:
  - channel: '#alerts'
    title: "{{ range .Alerts }}{{ .Annotations.summary }}\n{{ end }}"
    text: "{{ range .Alerts }}{{ .Annotations.description }}\n{{ end }}"

재사용 가능한 템플릿 정의

첫 번째 예시로 돌아가서, 우리는 여러 줄에 걸친 복잡한 템플릿을 피하기 위해 Alertmanager가 로드하는 이름 있는 템플릿이 들어 있는 파일을 제공할 수도 있어요. /alertmanager/template/myorg.tmpl 아래에 파일을 만들고 그 안에 "slack.myorg.text"라는 템플릿을 만듭니다:

{{ define "slack.myorg.text" }}https://internal.myorg.net/wiki/alerts/{{ .GroupLabels.app }}/{{ .GroupLabels.alertname }}{{ end}}

이제 설정은 "text" 필드에 주어진 이름의 템플릿을 로드하고, 우리는 사용자 정의 템플릿 파일에 대한 경로를 제공합니다:

global:
  slack_api_url: 'https://hooks.slack.com/services/...'

route:
  receiver: 'slack-notifications'
  group_by: [alertname, datacenter, app]

receivers:
- name: 'slack-notifications'
  slack_configs:
  - channel: '#alerts'
    text: '{{ template "slack.myorg.text" . }}'

templates:
- '/etc/alertmanager/templates/myorg.tmpl'

이 예시는 이 블로그 포스트에서 더 자세히 설명됩니다.

더 알아보기 (Learn more)