Skip to content

Instantly share code, notes, and snippets.

@mecaneer23
Last active March 18, 2025 10:28
Show Gist options
  • Select an option

  • Save mecaneer23/40d1f99cec7228f4afd4fda06f15fae1 to your computer and use it in GitHub Desktop.

Select an option

Save mecaneer23/40d1f99cec7228f4afd4fda06f15fae1 to your computer and use it in GitHub Desktop.
Format Grafana notification templates in a more readable way

Grafana Notification Template

Format notifications in a more readable way

Implementation

  1. Add the following template to Alerting/Contact points/Notification Templates with the name payload_message
{{ define "payload_message" -}}
{{ range .Alerts -}}
[{{ .Status }} {{ if eq .Status "firing" }}🔥{{ else }}{{ if eq .Status "resolved" }}✅{{ end }}{{ end }}] {{ .Labels.alertname }}{{ if .Labels.instance }} ({{ .Labels.instance }}){{ end }}{{/*
Values:{{ if len .Values }}{{ $first := true }}{{ range $refID, $value := .Values -}}{{ if $first }}{{ $first = false }}{{ else }}{{ end }}
  - {{ $refID }}: {{ $value }}
{{- end }}
{{ else }} [no value]
{{ end -}} */}}{{/*
Value:{{ if .Annotations.value }}
  - {{ .Annotations.value }}{{ else }}
  - [no `value` field provided]{{ end }} */}}
Labels:{{ range .Labels.SortedPairs -}}
{{ if and (ne .Name "alertname") (ne .Name "instance") }}
  - {{ .Name }}: {{ .Value -}}
{{ end -}}
{{ end -}}
{{ if gt (len .Annotations) 0 }}
Details:{{ range .Annotations.SortedPairs -}}{{/*
{{ if (ne .Name "value") }}
  - {{ .Name }}: {{ .Value }}
{{ else }}{{ end -}} */}}
  - {{ .Name }}: {{ .Value }}
{{ end -}}
{{ else }}
{{ end }}
{{ end }}
{{ end }}
  1. For every Alert rule you have configured, add a custom annotation with the name value and a value similar to {{ humanize $values.A.Value }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment