Message templating

Notifications sent via contact points are built using templates. Grafana comes with default templates which you can customize. Grafana’s notification templates are based on the Go templating system where some fields are evaluated as text, while others are evaluated as HTML which can affect escaping. Since most of the contact point fields can be templated, you can create reusable templates and them in multiple contact points. See template data reference to check what variables are available in the templates.

Using templating in contact point fields

This section shows an example of using templating to render a number of firing or resolved alerts in Slack message title, and listing alerts with status and name in the message body:

Message templating - 图1

Reusable templates

You can create named templates and then reuse them in contact point fields or other templates.

Create a template

  1. In the Grafana side bar, hover your cursor over the Alerting (bell) icon and then click Contact points.
  2. Click Add template.
  3. Fill in Name and Content fields.
  4. Click Save template button at the bottom of the page.

Note The template name used to reference this template in templating is not the value of the Name field, but the parameter to define tag in the content. When creating a template you can omit define entirely and it will be added automatically with same value as Name field. It’s recommended to use the same name for define and Name field to avoid confusion.

Message templating - 图2

Edit a template

  1. In the Grafana side bar, hover your cursor over the Alerting (bell) icon and then click Contact points.
  2. Find the template you want to edit in the templates table and click the pen icon on the right side.
  3. Make any changes and click Save template button at the bottom of the page.

Delete a template

  1. In the Grafana side bar, hover your cursor over the Alerting (bell) icon and then click Contact points.
  2. Find the template you want to edit in the templates table and click the trash can icon on the right side.
  3. A confirmation dialog will open. Click Yes, delete.

Note You are not prevented from deleting templates that are in use somewhere in contact points or other templates. Be careful!

Use a template in a contact point field

To use a template:

Enter {{ template "templatename" . }} into a contact point field, where templatename is the define parameter of a template.

Message templating - 图3

Template examples

Here is an example of a template to render a single alert:

  1. {{ define "alert" }}
  2. [{{.Status}}] {{ .Labels.alertname }}
  3. Labels:
  4. {{ range .Labels.SortedPairs }}
  5. {{ .Name }}: {{ .Value }}
  6. {{ end }}
  7. {{ if gt (len .Annotations) 0 }}
  8. Annotations:
  9. {{ range .Annotations.SortedPairs }}
  10. {{ .Name }}: {{ .Value }}
  11. {{ end }}
  12. {{ end }}
  13. {{ if gt (len .SilenceURL ) 0 }}
  14. Silence alert: {{ .SilenceURL }}
  15. {{ end }}
  16. {{ if gt (len .DashboardURL ) 0 }}
  17. Go to dashboard: {{ .Dashboard URL }}
  18. {{ end }}
  19. {{ end }}

Template to render entire notification message:

  1. {{ define "message" }}
  2. {{ if gt (len .Alerts.Firing) 0 }}
  3. {{ len .Alerts.Firing }} firing:
  4. {{ range .Alerts.Firing }} {{ template "alert" .}} {{ end }}
  5. {{ end }}
  6. {{ if gt (len .Alerts.Resolved) 0 }}
  7. {{ len .Alerts.Resolved }} resolved:
  8. {{ range .Alerts.Resolved }} {{ template "alert" .}} {{ end }}
  9. {{ end }}
  10. {{ end }}

Manage templates for an external Alertmanager

Grafana alerting UI supports managing external Alertmanager configuration. Once you add an Alertmanager data source, a dropdown displays at the top of the page, allowing you to select either Grafana or an external Alertmanager data source.

Select Alertmanager