Create notification templates
This documentation topic is designed for Grafana workspaces that support Grafana version 9.x.
For Grafana workspaces that support Grafana version 10.x, see Working in Grafana version 10.
For Grafana workspaces that support Grafana version 8.x, see Working in Grafana version 8.
Create reusable notification templates to send to your contact points.
You can add one or more templates to your notification template.
Your notification template name must be unique. You cannot have two templates
with the same name in the same notification template or in different
notification templates. Avoid defining templates with the same name as default
templates, such as: __subject
,
__text_values_list
,
__text_alert_list
,
default.title
and
default.message
.
In the Contact points tab, you can see a list of your notification templates.
Creating notification templates
To create a notification template
-
Click Add template.
-
Choose a name for the notification template, such as
email.subject
. -
Write the content of the template in the content field.
For example:
{{ if .Alerts.Firing -}} {{ len .Alerts.Firing }} firing alerts {{ end }} {{ if .Alerts.Resolved -}} {{ len .Alerts.Resolved }} resolved alerts {{ end }}
-
Click Save.
{{ define "email.subject" }}
(whereemail.subject
is the name of your template) and{{ end }}
is automatically added to the start and end of the content.
To create a notification template that contains more than one template:
-
Click Add Template.
-
Enter a name for the overall notification template. For example,
email
. -
Write each template in the Content field, including
{{ define "
andname-of-template
" }}{{ end }}
at the start and end of each template. You can use descriptive names for each of the templates in the notification template, for example,email.subject
oremail.message
. In this case, do not reuse the name of the notification template you entered above.The following sections show detailed examples for templates you might create.
-
Click Save.
Creating a template for the subject of an email
Create a template for the subject of an email that contains the number of firing and resolved alerts, as in this example:
1 firing alerts, 0 resolved alerts
To create a template for the subject of an email
-
Create a template called
email.subject
with the following content:{{ define "email.subject" }} {{ len .Alerts.Firing }} firing alerts, {{ len .Alerts.Resolved }} resolved alerts {{ end }}
-
Use the template when creating your contact point integration by putting it into the Subject field with the
template
keyword.{{ template "email.subject" . }}
Creating a template for the message of an email
Create a template for the message of an email that contains a summary of all firing and resolved alerts, as in this example:
There are 2 firing alerts, and 1 resolved alerts Firing alerts: - alertname=Test 1 grafana_folder=GrafanaCloud has value(s) B=1 - alertname=Test 2 grafana_folder=GrafanaCloud has value(s) B=2 Resolved alerts: - alertname=Test 3 grafana_folder=GrafanaCloud has value(s) B=0
To create a template for the message of an email
-
Create a notification template called
email
with two templates in the content:email.message_alert
andemail.message
.The
email.message_alert
template is used to print the labels and values for each firing and resolved alert while theemail.message
template contains the structure of the email.{{- define "email.message_alert" -}} {{- range .Labels.SortedPairs }}{{ .Name }}={{ .Value }} {{ end }} has value(s) {{- range $k, $v := .Values }} {{ $k }}={{ $v }}{{ end }} {{- end -}} {{ define "email.message" }} There are {{ len .Alerts.Firing }} firing alerts, and {{ len .Alerts.Resolved }} resolved alerts {{ if .Alerts.Firing -}} Firing alerts: {{- range .Alerts.Firing }} - {{ template "email.message_alert" . }} {{- end }} {{- end }} {{ if .Alerts.Resolved -}} Resolved alerts: {{- range .Alerts.Resolved }} - {{ template "email.message_alert" . }} {{- end }} {{- end }} {{ end }}
-
Use the template when creating your contact point integration by putting it into the Text Body field with the
template
keyword.{{ template "email.message" . }}
Creating a template for the title of a Slack message
Create a template for the title of a Slack message that contains the number of firing and resolved alerts, as in the following example:
1 firing alerts, 0 resolved alerts
To create a template for the title of a Slack message
-
Create a template called
slack.title
with the following content:{{ define "slack.title" }} {{ len .Alerts.Firing }} firing alerts, {{ len .Alerts.Resolved }} resolved alerts {{ end }}
-
Use the template when creating your contact point integration by putting it into the Title field with the
template
keyword.{{ template "slack.title" . }}
Creating a template for the content of a Slack message
Create a template for the content of a Slack message that contains a description of all firing and resolved alerts, including their labels, annotations, and Dashboard URL:
1 firing alerts: [firing] Test1 Labels: - alertname: Test1 - grafana_folder: GrafanaCloud Annotations: - description: This is a test alert Go to dashboard: https://example.com/d/dlhdLqF4z?orgId=1 1 resolved alerts: [firing] Test2 Labels: - alertname: Test2 - grafana_folder: GrafanaCloud Annotations: - description: This is another test alert Go to dashboard: https://example.com/d/dlhdLqF4z?orgId=1
To create a template for the content of a Slack message
-
Create a template called
slack
with two templates in the content:slack.print_alert
andslack.message
.The
slack.print_alert
template is used to print the labels, annotations, and DashboardURL while theslack.message
template contains the structure of the notification.{{ define "slack.print_alert" -}} [{{.Status}}] {{ .Labels.alertname }} Labels: {{ range .Labels.SortedPairs -}} - {{ .Name }}: {{ .Value }} {{ end -}} {{ if .Annotations -}} Annotations: {{ range .Annotations.SortedPairs -}} - {{ .Name }}: {{ .Value }} {{ end -}} {{ end -}} {{ if .DashboardURL -}} Go to dashboard: {{ .DashboardURL }} {{- end }} {{- end }} {{ define "slack.message" -}} {{ if .Alerts.Firing -}} {{ len .Alerts.Firing }} firing alerts: {{ range .Alerts.Firing }} {{ template "slack.print_alert" . }} {{ end -}} {{ end }} {{ if .Alerts.Resolved -}} {{ len .Alerts.Resolved }} resolved alerts: {{ range .Alerts.Resolved }} {{ template "slack.print_alert" .}} {{ end -}} {{ end }} {{- end }}
-
Use the template when creating your contact point integration by putting it into the Text Body field with the
template
keyword.{{ template "slack.message" . }}
Template both email and Slack with shared templates
Instead of creating separate notification templates for each contact point, such as email and Slack, you can share the same template.
For example, if you want to send an email with this subject and Slack
message with this title 1 firing alerts, 0 resolved
alerts
, you can create a shared template.
To create a shared template
-
Create a template called
common.subject_title
with the following content:{{ define "common.subject_title" }} {{ len .Alerts.Firing }} firing alerts, {{ len .Alerts.Resolved }} resolved alerts {{ end }}
-
For email, run the template from the subject field in your email contact point integration:
{{ template "common.subject_title" . }}
-
For Slack, run the template from the title field in your Slack contact point integration:
{{ template "common.subject_title" . }}
Using notification templates
Use templates in contact points to customize your notifications.
To use a template when creating a contact point
-
From the Alerting menu, choose Contact points to see a list of existing contact points.
-
Choose Add contact point. Alternately, you can edit an existing contact point by choosing the Edit icon (pen) next to the contact point you wish to edit.
-
Enter the templates you wish to use in one or more field, such as Message or Subject. To enter a template, use the form
{{ template "
, replacingtemplate_name
" . }}template_name
with the name of the template you want to use. -
Click Save contact point.