Created
February 13, 2026 12:18
-
-
Save taminomara/1ec01de44d929e8753b760df0d4bcd4a to your computer and use it in GitHub Desktop.
Better Grafana/Alertmanager alert templates for slack and telegram
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {{ define "__rich.status_emoji" }} | |
| {{- if eq . "resolved" }}🌿{{ else }}🔥{{ end -}} | |
| {{ end }} | |
| {{ define "__rich.slack.text_values_list" }} | |
| {{- if eq .Annotations.alert_show_values "true" -}} | |
| {{- if len .Values -}} | |
| {{- $sep := " " -}} | |
| {{- range $refID, $value := .Values -}} | |
| {{- $sep -}} | |
| {{- $sep = ", " -}} | |
| {{- $refID -}}=`{{- $value -}}` | |
| {{- end -}} | |
| {{- else -}} | |
| [no value] | |
| {{- end -}} | |
| {{- end -}} | |
| {{ end }} | |
| {{ define "__rich.slack.text_alert_list" }} | |
| {{- $summary := .summary -}} | |
| {{- $alertname := .alertname -}} | |
| {{- range .Alerts -}} | |
| {{- template "__rich.status_emoji" .Status -}} | |
| {{- if eq ($alertname | len) 0 }}*{{ .Labels.alertname }}*{{ end -}} | |
| {{- template "__rich.slack.text_values_list" . -}} | |
| {{- template "__rich.slack.labels_inline" .Labels -}} | |
| {{- if ne .Status "resolved" -}} | |
| {{ "\n" }} | |
| {{- if and (gt (.Annotations.summary | len) 0) (eq ($summary | len) 0) -}} | |
| {{- .Annotations.summary -}} | |
| {{ "\n" }} | |
| {{- end -}} | |
| {{- template "__rich.slack.links" . -}} | |
| {{ "\n" }} | |
| {{- end -}} | |
| {{ "\n" }} | |
| {{- end -}} | |
| {{ end }} | |
| {{ define "__rich.slack.labels_inline" }} | |
| {{- $Labels := . -}} | |
| {{- $hiddenLabels := coll.Dict | |
| "alertname" "true" | |
| "grafana_folder" "true" | |
| "job" "true" | |
| "environment" "true" | |
| "instance" "true" | |
| "service_name" "true" | |
| "silence_on_deploy" "true" | |
| -}} | |
| {{- $simpleLabels := coll.Slice | |
| "environment" | |
| "instance" | |
| "service_name" | |
| -}} | |
| {{- $sep := " @ " -}} | |
| {{- range $simpleLabels -}} | |
| {{- if eq ((index $Labels . | len)) 0 -}} | |
| {{- continue -}} | |
| {{- end -}} | |
| {{- $sep -}} | |
| {{- index $Labels . -}} | |
| {{- $sep = ", " -}} | |
| {{- end -}} | |
| {{- range $Labels.SortedPairs -}} | |
| {{- if eq (index $hiddenLabels .Name) "true" -}} | |
| {{- continue -}} | |
| {{- end -}} | |
| {{- $sep -}} | |
| {{- .Name -}}=`{{- .Value -}}` | |
| {{- $sep = ", " -}} | |
| {{- end -}} | |
| {{ end }} | |
| {{ define "__rich.slack.links" }} | |
| {{- $links := coll.Slice | |
| (coll.Slice "Source" .GeneratorURL) | |
| (coll.Slice "Silence" .SilenceURL) | |
| (coll.Slice "Dashboard" .DashboardURL) | |
| (coll.Slice "Panel" .PanelURL) | |
| (coll.Slice "Runbook" .Annotations.runbook_url) | |
| -}} | |
| {{- $sep := "" -}} | |
| {{- range $pair := $links }} | |
| {{- $name := index $pair 0 -}} | |
| {{- $link := index $pair 1 -}} | |
| {{- if eq ($link | len) 0 -}} | |
| {{- continue -}} | |
| {{- end -}} | |
| {{- $sep -}} | |
| <{{ $link }}|{{ $name }}> | |
| {{- $sep = " | " -}} | |
| {{- end -}} | |
| {{ end }} | |
| {{ define "slack.rich.title" }} | |
| {{- .Status | title -}} | |
| {{- if gt (.CommonLabels.alertname | len) 0 -}} | |
| : {{ .CommonLabels.alertname }} | |
| {{- else -}} | |
| {{- if eq .Status "firing" -}} | |
| : {{ .Alerts.Firing | len -}} | |
| {{- if gt (.Alerts.Resolved | len) 0 -}} | |
| , Resolved: {{ .Alerts.Resolved | len -}} | |
| {{- end -}} | |
| {{- end -}} | |
| {{- end -}} | |
| {{ end }} | |
| {{ define "slack.rich.message" }} | |
| {{- if gt (.CommonLabels.alertname | len) 0 -}} | |
| {{- if ne .Status "resolved" -}} | |
| {{- if gt (.CommonAnnotations.summary | len) 0 -}} | |
| {{- "\n" -}} | |
| {{- .CommonAnnotations.summary -}} | |
| {{- "\n" -}} | |
| {{- end -}} | |
| {{- end -}} | |
| {{- "\n" -}} | |
| {{- template "__rich.slack.text_alert_list" (coll.Dict | |
| "Alerts" .Alerts | |
| "alertname" .CommonLabels.alertname | |
| "summary" .CommonAnnotations.summary | |
| ) -}} | |
| {{- else -}} | |
| {{- template "__rich.slack.text_alert_list" (coll.Dict | |
| "Alerts" .Alerts | |
| "summary" "" | |
| "alertname" "" | |
| ) -}} | |
| {{- end -}} | |
| {{ end }} | |
| {{ define "__rich.telegram.text_values_list" }} | |
| {{- if eq .Annotations.alert_show_values "true" -}} | |
| {{- if len .Values -}} | |
| {{- $sep := " " -}} | |
| {{- range $refID, $value := .Values -}} | |
| {{- $sep -}} | |
| {{- $sep = ", " -}} | |
| {{- $refID -}}=<code>{{- $value -}}</code> | |
| {{- end -}} | |
| {{- else -}} | |
| [no value] | |
| {{- end -}} | |
| {{- end -}} | |
| {{ end }} | |
| {{ define "__rich.telegram.text_alert_list" }} | |
| {{- $summary := .summary -}} | |
| {{- $alertname := .alertname -}} | |
| {{- range .Alerts -}} | |
| {{- template "__rich.status_emoji" .Status -}} | |
| {{- if eq ($alertname | len) 0 }}<b>{{ .Labels.alertname }}</b>{{ end -}} | |
| {{- template "__rich.telegram.text_values_list" . -}} | |
| {{- template "__rich.telegram.labels_inline" .Labels -}} | |
| {{- if ne .Status "resolved" -}} | |
| {{ "\n" }} | |
| {{- if and (gt (.Annotations.summary | len) 0) (eq ($summary | len) 0) -}} | |
| {{- .Annotations.summary -}} | |
| {{ "\n" }} | |
| {{- end -}} | |
| {{- template "__rich.telegram.links" . -}} | |
| {{ "\n" }} | |
| {{- end -}} | |
| {{ "\n" }} | |
| {{- end -}} | |
| {{ end }} | |
| {{ define "__rich.telegram.labels_inline" }} | |
| {{- $Labels := . -}} | |
| {{- $hiddenLabels := coll.Dict | |
| "alertname" "true" | |
| "grafana_folder" "true" | |
| "job" "true" | |
| "environment" "true" | |
| "instance" "true" | |
| "service_name" "true" | |
| "silence_on_deploy" "true" | |
| -}} | |
| {{- $simpleLabels := coll.Slice | |
| "environment" | |
| "instance" | |
| "service_name" | |
| -}} | |
| {{- $sep := " @ " -}} | |
| {{- range $simpleLabels -}} | |
| {{- if eq ((index $Labels . | len)) 0 -}} | |
| {{- continue -}} | |
| {{- end -}} | |
| {{- $sep -}} | |
| {{- index $Labels . -}} | |
| {{- $sep = ", " -}} | |
| {{- end -}} | |
| {{- range $Labels.SortedPairs -}} | |
| {{- if eq (index $hiddenLabels .Name) "true" -}} | |
| {{- continue -}} | |
| {{- end -}} | |
| {{- $sep -}} | |
| {{- .Name -}}=<code>{{- .Value -}}</code> | |
| {{- $sep = ", " -}} | |
| {{- end -}} | |
| {{ end }} | |
| {{ define "__rich.telegram.links" }} | |
| {{- $links := coll.Slice | |
| (coll.Slice "Source" .GeneratorURL) | |
| (coll.Slice "Silence" .SilenceURL) | |
| (coll.Slice "Dashboard" .DashboardURL) | |
| (coll.Slice "Panel" .PanelURL) | |
| (coll.Slice "Runbook" .Annotations.runbook_url) | |
| -}} | |
| {{- $sep := "" -}} | |
| {{- range $pair := $links }} | |
| {{- $name := index $pair 0 -}} | |
| {{- $link := index $pair 1 -}} | |
| {{- if eq ($link | len) 0 -}} | |
| {{- continue -}} | |
| {{- end -}} | |
| {{- $sep -}} | |
| <a href="{{ $link }}">{{ $name }}</a> | |
| {{- $sep = " | " -}} | |
| {{- end -}} | |
| {{ end }} | |
| {{ define "telegram.rich.title" }} | |
| {{- .Status | title -}} | |
| {{- if gt (.CommonLabels.alertname | len) 0 -}} | |
| : {{ .CommonLabels.alertname }} | |
| {{- else -}} | |
| {{- if eq .Status "firing" -}} | |
| : {{ .Alerts.Firing | len -}} | |
| {{- if gt (.Alerts.Resolved | len) 0 -}} | |
| , Resolved: {{ .Alerts.Resolved | len -}} | |
| {{- end -}} | |
| {{- end -}} | |
| {{- end -}} | |
| {{ end }} | |
| {{ define "telegram.rich.message" }} | |
| {{- if gt (.CommonLabels.alertname | len) 0 -}} | |
| <b>{{- template "telegram.rich.title" . -}}</b> | |
| {{- "\n" -}} | |
| {{- if ne .Status "resolved" -}} | |
| {{- if gt (.CommonAnnotations.summary | len) 0 -}} | |
| {{- "\n" -}} | |
| {{- .CommonAnnotations.summary -}} | |
| {{- "\n" -}} | |
| {{- end -}} | |
| {{- end -}} | |
| {{- "\n" -}} | |
| {{- template "__rich.telegram.text_alert_list" (coll.Dict | |
| "Alerts" .Alerts | |
| "alertname" .CommonLabels.alertname | |
| "summary" .CommonAnnotations.summary | |
| ) -}} | |
| {{- else -}} | |
| {{- template "__rich.telegram.text_alert_list" (coll.Dict | |
| "Alerts" .Alerts | |
| "summary" "" | |
| "alertname" "" | |
| ) -}} | |
| {{- end -}} | |
| {{ end }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment