Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v10.0.x] Alerting: Fix email template for text/plain emails #70111

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
94 changes: 59 additions & 35 deletions emails/templates/ng_alert_notification.txt
Original file line number Diff line number Diff line change
@@ -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
[[ range .GroupLabels.SortedPairs ]]
[[ .Name ]] = [[ .Value ]]
[[ end ]]
[[ if gt (len .Alerts.Firing) 0 ]]([[ .Alerts.Firing | len ]]) Firing[[ end ]]
[[ range .Alerts.Firing ]]
Labels:
[[ 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]]
[[ if .Alerts.Resolved -]]
([[ len .Alerts.Resolved ]]) RESOLVED
-------------
[[ template "__default_alerts_summarize" .Alerts.Resolved ]]
[[- end ]]
Go to the Alerts page: [[ .AlertPageUrl ]]
[[- end -]]

Go to the Alerts page:
[[.AlertPageUrl]]
[[- define "__default_alerts_summarize" -]]
[[- 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 ]]
99 changes: 61 additions & 38 deletions public/emails/ng_alert_notification.txt
Original file line number Diff line number Diff line change
@@ -1,41 +1,64 @@
{{HiddenSubject .Subject "{{.Title}}"}}

{{.Title}}
----------------

{{ .Alerts | len }} alert{{ if gt (len .Alerts) 1 }}s{{ end }} for
{{ range .GroupLabels.SortedPairs }}
{{ .Name }} = {{ .Value }}
{{ end }}
{{ if gt (len .Alerts.Firing) 0 }}({{ .Alerts.Firing | len }}) Firing{{ end }}
{{ range .Alerts.Firing }}
Labels:
{{ 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:
{{.AlertPageUrl}}
{{- define "__default_message" -}}
You have {{ len .Alerts.Firing }} firing alert(s), and {{ len .Alerts.Resolved }} resolved alert(s) for {{ .GroupLabels }}

{{ if .Alerts.Firing -}}
({{ len .Alerts.Firing }}) FIRING
-----------
{{ template "__default_alerts_summarize" .Alerts.Firing }}
{{- end }}

{{ if .Alerts.Resolved -}}
({{ len .Alerts.Resolved }}) RESOLVED
-------------
{{ template "__default_alerts_summarize" .Alerts.Resolved }}
{{- end }}
Go to the Alerts page: {{ .AlertPageUrl }}
{{- end -}}

{{- define "__default_alerts_summarize" -}}
{{- 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