Example of a custom template

Here’s an example of how to use a custom template. You can also use the default template included in the setup.

Step 1: Configure a template to render a single alert.

  1. {{ define "myalert" }}
  2. [{{.Status}}] {{ .Labels.alertname }}
  3. Labels:
  4. {{ range .Labels.SortedPairs }}
  5. {{ .Name }}: {{ .Value }}
  6. {{ end }}
  7. {{ if gt (len .Annotations) 0 }}
  8. Annotations:
  9. {{ range .Annotations.SortedPairs }}
  10. {{ .Name }}: {{ .Value }}
  11. {{ end }}
  12. {{ end }}
  13. {{ if gt (len .SilenceURL ) 0 }}
  14. Silence alert: {{ .SilenceURL }}
  15. {{ end }}
  16. {{ if gt (len .DashboardURL ) 0 }}
  17. Go to dashboard: {{ .DashboardURL }}
  18. {{ end }}
  19. {{ end }}

Step 2: Configure a template to render entire notification message.

  1. {{ define "mymessage" }}
  2. {{ if gt (len .Alerts.Firing) 0 }}
  3. {{ len .Alerts.Firing }} firing:
  4. {{ range .Alerts.Firing }} {{ template "myalert" .}} {{ end }}
  5. {{ end }}
  6. {{ if gt (len .Alerts.Resolved) 0 }}
  7. {{ len .Alerts.Resolved }} resolved:
  8. {{ range .Alerts.Resolved }} {{ template "myalert" .}} {{ end }}
  9. {{ end }}
  10. {{ end }}

Step 3: Add mymessage in the notification message field.

  1. Alert summary:
  2. {{ template "mymessage" . }}