Message templating

Notifications sent via contact points are built using messaging templates. Grafana’s default 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). The default template, defined in default_template.go, is a useful reference for custom templates.

Since most of the contact point fields can be templated, you can create reusable custom templates and use them in multiple contact points. The template data topic lists variables that are available for templating. The default template is defined in default_template.go which can serve as a useful reference or starting point for custom templates.

Using templates

The following example shows the use of default templates to render an alert message in slack. The message title contains a count of firing or resolved alerts and the message body has a list of alerts with status.

Message templating - 图1

The following example shows the use of a custom template within one of the contact point fields.

Message templating - 图2

Create a message template

Note: Before Grafana v8.2, the configuration of the embedded Alertmanager was shared across organisations. Users of Grafana 8.0 and 8.1 are advised to use the new Grafana 8 alerts only if they have one organisation. Otherwise, silences for the Grafana managed alerts will be visible by all organizations.

  1. In the Grafana menu, click the Alerting (bell) icon to open the Alerting page listing existing alerts.
  2. In the Alerting page, click Contact points to open the page listing existing contact points.
  3. From Alertmanager drop-down, select an external Alertmanager to create and manage templates for the external data source. Otherwise, keep the default option of Grafana.

    Select AlertmanagerMessage templating - 图4

  4. Click Add template.

  5. In Name, add a descriptive name.
  6. In Content, add the content of the template.
  7. Click Save template button at the bottom of the page. Message templating - 图5

The define tag in the Content section assigns the template name. This tag is optional, and when omitted, the template name is derived from the Name field. When both are specified, it is a best practice to ensure that they are the same.

Edit a message template

  1. In the Alerting page, click Contact points to open the page listing existing contact points.
  2. In the Template table, find the template you want to edit, then click the Edit (pen icon).
  3. Make your changes, then click Save template.

Delete a message template

  1. In the Alerting page, click Contact points to open the page listing existing contact points.
  2. In the Template table, find the template you want to delete, then click the Delete (trash icon).
  3. In the confirmation dialog, click Yes, delete to delete the template.

Use caution when deleting a template since Grafana does not prevent you from deleting templates that are in use.

Custom template examples

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: {{ .DashboardURL }}
  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 }}