Skip to main content

Notification templates

Templates control the wording of notifications. Open Insights → Alerting → Templates.

Two different things are templated in alerting, and they use different data:

WhereDataCovered in
A rule's labels and annotationsThe one series that firedAlert rules
A contact point's title and messageThe whole group of alerts being sentThis page

Start with rule annotations. A good summary and a runbook URL fix most notifications without a template anywhere.

When you need one

Reach for a template when the default message is wrong for the destination rather than wrong in general:

  • A Slack title that leads with the environment.
  • A PagerDuty summary that has to fit a paging screen.
  • A webhook body a downstream system parses.
  • The same wording reused across six contact points.

Create one

New template takes a name and a definition. The name cannot be changed after it is saved, so pick something you can live with: slack.title, pagerduty.summary.

A definition is one or more named blocks:

{{ define "slack.title" }}
[{{ .Status }}] {{ .CommonLabels.alertname }} in {{ .CommonLabels.cluster }}
{{ end }}

{{ define "slack.body" }}
{{ len .Alerts.Firing }} firing, {{ len .Alerts.Resolved }} resolved

{{ range .Alerts.Firing }}
- {{ .Labels.instance }}: {{ .Annotations.summary }}
{{ end }}
{{ end }}

Use one

In a contact point, put the template call in the field you want it to fill:

{{ template "slack.title" . }}

The fields that accept a template are marked in the contact point editor. Title, subject and message fields all take one. See Contact points.

The data available

FieldWhat it holds
.Statusfiring or resolved for the group
.ReceiverThe contact point being delivered to
.Alerts.FiringThe firing alerts in this group
.Alerts.ResolvedThe resolved alerts in this group
.GroupLabelsThe labels this group was grouped by
.CommonLabelsLabels every alert in the group shares
.CommonAnnotationsAnnotations every alert in the group shares
.GroupKeyThe group's identity

Each alert in .Alerts.Firing and .Alerts.Resolved carries:

FieldWhat it holds
.LabelsEvery label on the alert, including alertname
.AnnotationsSummary, description, runbook URL
.StartsAtWhen it started firing
.EndsAtWhen it resolved

Functions

Templates use Go template syntax: range, if, with, len, index and printf all work. A label that does not exist renders as empty rather than as an error, so a title referencing an absent label comes out short rather than broken.

Use Render preview to confirm anything beyond those. It renders with the same engine that delivers.

.CommonLabels is the field to understand. Anything in it is true of every alert in the message, so it is safe to put in a title. A label that varies across the group is missing from .CommonLabels, which is why a title built from .CommonLabels.instance sometimes comes out blank.

Test it

The right-hand panel renders the template against sample alerts. Pick a preset and use Render preview.

PresetSample
Firing alertOne critical alert
Resolved alertOne resolved alert
Multiple alertsTwo firing and one resolved, mixed severities

Test all three. A template that reads {{ (index .Alerts.Firing 0).Labels.instance }} renders on the firing preset and breaks on the resolved one, where there are no firing alerts.

When a template is broken

A template that fails to parse or render is delivered as its own source text, so the notification arrives with {{ template "slack.title" . }} in it. That is the signal to open the template and use Render preview.

Provisioned templates

A template marked Provisioned is managed outside the portal and reads as read-only.

Delete one

Contact points referencing a deleted template fall back to the default notification text. Nothing breaks, and the wording changes without warning, so check which contact points use it first.

Permissions

ActionRole
View and read templatesMember
Create, edit, render and delete themEditor