Alerting: Fix email template for text/plain emails (#69951)

This commit fixes the email template for text/plain emails to
support custom messages. It also fixes the default template,
removing extra whitespace and showing Summary and Description
annotations separate from the other annotations.
This commit is contained in:
George Robinson 2023-06-14 23:09:06 +01:00 committed by GitHub
parent fc374f93a3
commit c1e3362e21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 117 additions and 70 deletions

View File

@ -1,38 +1,62 @@
[[HiddenSubject .Subject "[[.Title]]"]] [[- define "__default_message" -]]
You have [[ len .Alerts.Firing ]] firing alert(s), and [[ len .Alerts.Resolved ]] resolved alert(s) for [[ .GroupLabels ]]
[[.Title]] [[ if .Alerts.Firing -]]
---------------- ([[ len .Alerts.Firing ]]) FIRING
-----------
[[ template "__default_alerts_summarize" .Alerts.Firing ]]
[[- end ]]
[[ .Alerts | len ]] alert[[ if gt (len .Alerts) 1 ]]s[[ end ]] for [[ if .Alerts.Resolved -]]
[[ range .GroupLabels.SortedPairs ]] ([[ len .Alerts.Resolved ]]) RESOLVED
[[ .Name ]] = [[ .Value ]] -------------
[[ end ]] [[ template "__default_alerts_summarize" .Alerts.Resolved ]]
[[ if gt (len .Alerts.Firing) 0 ]]([[ .Alerts.Firing | len ]]) Firing[[ end ]] [[- end ]]
[[ range .Alerts.Firing ]] Go to the Alerts page: [[ .AlertPageUrl ]]
Labels: [[- end -]]
[[ range .Labels.SortedPairs ]]
[[ .Name ]] = [[ .Value ]]
[[ end ]]
[[ if gt (len .Annotations) 0 ]]
Annotations:
[[ end ]]
[[ range .Annotations.SortedPairs ]]
[[ .Name ]] = [[ .Value ]]
[[ end ]]
[[ end ]][[ if gt (len .Alerts.Resolved) 0 ]]([[ .Alerts.Resolved | len ]]) Resolved[[ end ]]
[[ range .Alerts.Resolved ]]
Labels:
[[ range .Labels.SortedPairs ]]
[[ .Name ]] = [[ .Value ]]
[[ end ]]
[[ if gt (len .Annotations) 0 ]]
Annotations:
[[ end ]]
[[ range .Annotations.SortedPairs ]]
[[ .Name ]] = [[ .Value ]]
[[ end ]]
[[ end ]]View your Alert rule:
[[.RuleUrl]]
Go to the Alerts page: [[- define "__default_alerts_summarize" -]]
[[.AlertPageUrl]] [[- range . ]]
[[ template "__default_alert_labels" . ]]
[[ template "__default_alert_annotations" . ]]
[[- end ]]
[[- end -]]
[[- define "__default_alert_labels" -]]
[[- if gt (len .Labels) 3 -]]
Labels: [[ template "__default_sorted_pairs" .Labels ]]
[[- else -]]
Labels: [[ .Labels.SortedPairs ]]
[[- end -]]
[[- end -]]
[[- define "__default_alert_annotations" -]]
[[- $annotations := .Annotations -]]
[[- if index $annotations "summary" -]]
Summary: [[ index .Annotations "summary" ]]
[[- $annotations = $annotations.Remove (toStrings (list "summary")) ]]
[[ end -]]
[[- if index $annotations "description" -]]
Description: [[ index $annotations "description" ]]
[[- $annotations = $annotations.Remove (toStrings (list "description")) ]]
[[ end -]]
[[- if index $annotations "runbook_url" -]]
Runbook: [[ index $annotations "runbook_url" ]]
[[- $annotations = $annotations.Remove (toStrings (list "runbook_url")) ]]
[[ end -]]
[[- if $annotations -]]
Annotations: [[ template "__default_sorted_pairs" $annotations ]]
[[ end -]]
[[- end -]]
[[- define "__default_sorted_pairs" -]]
[[ range .SortedPairs ]]
[[ .Name ]] = [[ .Value ]]
[[- end ]]
[[- end -]]
[[- if .Message -]]
[[ .Message ]]
[[ else -]]
[[ template "__default_message" . ]]
[[- end ]]

View File

@ -1,41 +1,64 @@
{{HiddenSubject .Subject "{{.Title}}"}} {{- define "__default_message" -}}
You have {{ len .Alerts.Firing }} firing alert(s), and {{ len .Alerts.Resolved }} resolved alert(s) for {{ .GroupLabels }}
{{.Title}} {{ if .Alerts.Firing -}}
---------------- ({{ len .Alerts.Firing }}) FIRING
-----------
{{ template "__default_alerts_summarize" .Alerts.Firing }}
{{- end }}
{{ .Alerts | len }} alert{{ if gt (len .Alerts) 1 }}s{{ end }} for {{ if .Alerts.Resolved -}}
{{ range .GroupLabels.SortedPairs }} ({{ len .Alerts.Resolved }}) RESOLVED
{{ .Name }} = {{ .Value }} -------------
{{ end }} {{ template "__default_alerts_summarize" .Alerts.Resolved }}
{{ if gt (len .Alerts.Firing) 0 }}({{ .Alerts.Firing | len }}) Firing{{ end }} {{- end }}
{{ range .Alerts.Firing }} Go to the Alerts page: {{ .AlertPageUrl }}
Labels: {{- end -}}
{{ range .Labels.SortedPairs }}
{{ .Name }} = {{ .Value }}
{{ end }}
{{ if gt (len .Annotations) 0 }}
Annotations:
{{ end }}
{{ range .Annotations.SortedPairs }}
{{ .Name }} = {{ .Value }}
{{ end }}
{{ end }}{{ if gt (len .Alerts.Resolved) 0 }}({{ .Alerts.Resolved | len }}) Resolved{{ end }}
{{ range .Alerts.Resolved }}
Labels:
{{ range .Labels.SortedPairs }}
{{ .Name }} = {{ .Value }}
{{ end }}
{{ if gt (len .Annotations) 0 }}
Annotations:
{{ end }}
{{ range .Annotations.SortedPairs }}
{{ .Name }} = {{ .Value }}
{{ end }}
{{ end }}View your Alert rule:
{{.RuleUrl}}
Go to the Alerts page: {{- define "__default_alerts_summarize" -}}
{{.AlertPageUrl}} {{- range . }}
{{ template "__default_alert_labels" . }}
{{ template "__default_alert_annotations" . }}
{{- end }}
{{- end -}}
{{- define "__default_alert_labels" -}}
{{- if gt (len .Labels) 3 -}}
Labels: {{ template "__default_sorted_pairs" .Labels }}
{{- else -}}
Labels: {{ .Labels.SortedPairs }}
{{- end -}}
{{- end -}}
{{- define "__default_alert_annotations" -}}
{{- $annotations := .Annotations -}}
{{- if index $annotations "summary" -}}
Summary: {{ index .Annotations "summary" }}
{{- $annotations = $annotations.Remove (toStrings (list "summary")) }}
{{ end -}}
{{- if index $annotations "description" -}}
Description: {{ index $annotations "description" }}
{{- $annotations = $annotations.Remove (toStrings (list "description")) }}
{{ end -}}
{{- if index $annotations "runbook_url" -}}
Runbook: {{ index $annotations "runbook_url" }}
{{- $annotations = $annotations.Remove (toStrings (list "runbook_url")) }}
{{ end -}}
{{- if $annotations -}}
Annotations: {{ template "__default_sorted_pairs" $annotations }}
{{ end -}}
{{- end -}}
{{- define "__default_sorted_pairs" -}}
{{ range .SortedPairs }}
{{ .Name }} = {{ .Value }}
{{- end }}
{{- end -}}
{{- if .Message -}}
{{ .Message }}
{{ else -}}
{{ template "__default_message" . }}
{{- end }}
Sent by Grafana v{{.BuildVersion}} (c) {{now | date "2006"}} Grafana Labs Sent by Grafana v{{.BuildVersion}} (c) {{now | date "2006"}} Grafana Labs