Skip to content

Instantly share code, notes, and snippets.

@K-Phoen
Created June 19, 2022 19:23
Show Gist options
  • Save K-Phoen/d586abba338059fe4039a862a042ae4f to your computer and use it in GitHub Desktop.
Save K-Phoen/d586abba338059fe4039a862a042ae4f to your computer and use it in GitHub Desktop.
Improved Slack template for Grafana alerts
{{ define "custom_alert.title" }}[{{ .Status | toUpper }}{{ if eq .Status "firing" }}: {{ .Alerts.Firing | len }}{{ if gt (.Alerts.Resolved | len) 0 }}, RESOLVED: {{ .Alerts.Resolved | len }}{{ end }}{{ end }}]{{ if gt (len .GroupLabels) 0 }} Grouped by: {{ range .CommonLabels.SortedPairs }}{{ .Name }}: {{ .Value }}{{ end }}{{ end }}{{ end }}
{{ define "__text_alert_name" }}{{ range .Labels.SortedPairs }}{{ if eq .Name "alertname" }}{{ .Value }}{{ end }}{{ end }}{{ end }}
{{ define "__text_alert_summary" }}{{ range .Annotations.SortedPairs }}{{ if eq .Name "summary" }}{{ .Value }}
{{ end }}{{ end }}{{ end }}
{{ define "__text_alert_description" }}{{ range .Annotations.SortedPairs }}{{ if eq .Name "description" }}{{ .Value }}{{ end }}{{ end }}{{ end }}
{{ define "__text_alert_runbook_url" }}{{ range .Annotations.SortedPairs }}{{ if eq .Name "runbook_url" }}
:bookmark_tabs: <{{ .Value }}|Playbook>{{ end }}{{ end }}{{ end }}
{{ define "__text_alert_firing_item" }}:bell: {{ template "__text_alert_name" . }}
{{ template "__text_alert_summary" . }}{{ template "__text_alert_description" . }}
Labels: {{ range .Labels.SortedPairs }}
{{- if ne .Name "alertname" }}
{{- if ne .Name "ref_id" }}
{{- if ne .Name "datasource_uid" }}
{{- if ne .Name "rule_uid" }}
- {{ .Name }} = {{ .Value }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
Actions:
{{ if gt (len .DashboardURL) 0 }}:grafana: <{{ .DashboardURL }}|Go to dashboard>{{ end }}
{{ if gt (len .PanelURL) 0 }}:chart_with_upwards_trend: <{{ .PanelURL }}|Go to panel>{{ end }}
{{ if gt (len .GeneratorURL) 0 }}:arrow_right: <{{ .GeneratorURL }}|Go to alert>{{ end }}
{{ if gt (len .SilenceURL) 0 }}:mute: <{{ .SilenceURL }}|Silence alert>{{ end }}{{ template "__text_alert_runbook_url" . }}{{ end }}
{{ define "__text_alert_resolved_item" }}:large_green_circle: {{ template "__text_alert_name" . }}
Labels: {{ range .Labels.SortedPairs }}
{{- if ne .Name "alertname" }}
{{- if ne .Name "ref_id" }}
{{- if ne .Name "datasource_uid" }}
{{- if ne .Name "rule_uid" }}
- {{ .Name }} = {{ .Value }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
Actions:
{{ if gt (len .DashboardURL) 0 }}:grafana: <{{ .DashboardURL }}|Go to dashboard>{{ end }}
{{ if gt (len .PanelURL) 0 }}:chart_with_upwards_trend: <{{ .PanelURL }}|Go to panel>{{ end }}
{{ if gt (len .GeneratorURL) 0 }}:arrow_right: <{{ .GeneratorURL }}|Go to alert>{{ end }}{{ end }}
{{ define "__text_alert_list_firing" }}{{ range . }}
{{ template "__text_alert_firing_item" . }}{{ end }}{{ end }}
{{ define "__text_alert_list_resolved" }}{{ range . }}
{{ template "__text_alert_resolved_item" . }}{{ end }}{{ end }}
{{ define "custom_alert.message" }}
{{ if gt (len .Alerts.Firing) 0 }}{{ .Alerts.Firing | len }} Firing{{ template "__text_alert_list_firing" .Alerts.Firing }}{{ end }}
{{ if gt (len .Alerts.Resolved) 0 }}{{ .Alerts.Resolved | len }} Resolved{{ template "__text_alert_list_resolved" .Alerts.Resolved }}{{ end }}{{ end }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment