Menu

This is documentation for the next version of Grafana. For the latest stable release, go to the latest version.

Grafana Cloud Enterprise Open source

Notification template reference

By default, Grafana provides predefined templates to format notification messages.

You can also customize your notifications with custom templates, which are based on the Go template language.

This documentation lists the data available for use in notification templates.

Notification Data

In notification templates, dot (.) is initialized with the following data:

NameTypeDescription
ReceiverstringThe name of the contact point sending the notification
StatusstringThe status is firing if at least one alert is firing, otherwise resolved.
Alerts[]AlertList of all firing and resolved alerts in this notification.
Alerts.Firing[]AlertList of all firing alerts in this notification.
Alerts.Resolved[]AlertList of all resolved alerts in this notification.
GroupLabelsKVThe labels that group these alerts in this notification based on the Group by option.
CommonLabelsKVThe labels common to all alerts in this notification.
CommonAnnotationsKVThe annotations common to all alerts in this notification.
ExternalURLstringA link to Grafana, or the Alertmanager that sent this notification if using an external Alertmanager.

It’s important to remember that a single notification can group multiple alerts to reduce the number of alerts you receive. Alerts is an array that includes all the alerts in the notification.

Here’s an example that prints all available notification data from dot (.):

Go
{{ define "custom_template" }}
  {{ .Receiver }}
  {{ .Status }}
  There are {{ len .Alerts }} alerts
  There are {{ len .Alerts.Firing }} firing alerts
  There are {{ len .Alerts.Resolved }} resolved alerts
  {{ .GroupLabels }}
  {{ .CommonLabels }}
  {{ .CommonAnnotations }}
  {{ .ExternalURL }}
{{ end }}

Alert

Alert contains data for an individual alert:

NameTypeDescription
StatusstringFiring or resolved.
LabelsKVThe labels for this alert.
AnnotationsKVThe annotations for this alert.
StartsAttimeThe time the alert fired
EndsAttimeOnly set if the end time of an alert is known. Otherwise set to a configurable timeout period from the time since the last alert was received.
GeneratorURLstringA link to Grafana, or the source of the alert if using an external alert generator.
FingerprintstringA unique string that identifies the alert.

Grafana-managed alerts include these additional properties:

NameTypeDescription
DashboardURLstringA link to the Grafana Dashboard if the alert has a Dashboard UID annotation.
PanelURLstringA link to the panel if the alert has a Panel ID annotation.
SilenceURLstringA link to silence the alert.
ValuesKVThe values of all expressions, including Classic Conditions.
ValueStringstringA string that contains the labels and value of each reduced expression in the alert.

This example iterates over the list of firing and resolved alerts (.Alerts) in the notification and prints the data for each alert:

Go
{{ define "custom_template" }}
{{ range .Alerts }}
  {{ .Status }}
  {{ .Labels }}
  {{ .Annotations }}
  {{ .StartsAt }}
  {{ .EndsAt }}
  {{ .GeneratorURL }}
  {{ .Fingerprint }}

  {{/* Only available for Grafana-managed alerts */}}
  {{ .DashboardURL }}
  {{ .PanelURL }}
  {{ .SilenceURL }}
  {{ .Values }}
  {{ .ValueString }}
{{ end }}
{{ end }}

KV

KV is a set of key value pairs, where each key and value is a string.

Similarly to accessing variable properties, you can use . to retrieve the value of a value. For example:

Go
{{ define "custom_template" }}
  {{ .CommonLabels.grafana_folder }}
{{ end }}

If a KV happens to contain numbers or bools then these are string representations of the numeric or boolean value.

Additionally, KV provides methods to sort the pairs, remove keys, and iterate over just the keys or values:

Method nameDescriptionArgumentsReturns
SortedPairsReturns a sorted list of key/value pairs.Pairs
RemoveReturns a copy of the KV with the keys removed[]stringKV
NamesReturn the names of the label names[]string
ValuesReturn a list of the values[]string

Here’s an example of using these methods:

Go
{{ define "custom_template" }}
  {{ .CommonLabels.SortedPairs }}
  {{ .CommonLabels.Names }}
  {{ .CommonLabels.Values }}
  {{ .CommonLabels.Remove (stringSlice "grafana_folder") }}
{{ end }}

Functions

Functions can perform actions in templates such as transforming or formatting data.

Note that the functions provided by Go’s template language, such as index, and, printf, and len, are available, along with many others.

In addition, the following functions are also available for templating notifications:

Strings

NameArgumentsReturnsDescription
titlestringstringCapitalizes the first character of each word.
toUpperstringstringReturns all text in uppercase.
toLowerstringstringReturns all text in lowercase.
trimSpacestringstringRemoves leading and trailing white spaces.
matchpattern, textbooleanMatches the text against a regular expression pattern.
reReplaceAllpattern, replacement, textstringReplaces text matching the regular expression.
joinsep string, s []stringstringConcatenates the elements of s to create a single string. The separator string sep is placed between elements in the resulting string.
safeHtmlstringstringMarks string as HTML not requiring auto-escaping.
stringSlice…stringstringReturns the passed strings as a slice of strings. auto-escaping.

Here’s an example of using these functions:

Go
{{ define "custom_template" }}
  {{ title "hello, world!" }}
  {{ toUpper "Hello, world!" }}
  {{ toLower "Hello, world!" }}
  {{ trimSpace "Hello, world!" }}
  {{ match "a.*" "abc" }}
  {{ reReplaceAll "localhost:(.*)" "example.com:$1" "localhost:8080" }}
  {{ join "-" (stringSlice "a" "b" "c") }}
  {{ safeHtml "<b>Text</b>"}}
  {{ stringSlice "a" "b" "c" }}
{{ end }}

Time

You can format a time in a number of different formats using the date function. For example, to print the time that an alert fired in the format 15:04:05 MST:

Go
{{ define "custom_template" }}
  {{ with (index .Alerts 0) }}
    {{ .StartsAt | date "15:04:05 MST" }}
  {{ end}}
{{ end }}

You can also use the tz function to change the timezone from UTC to a local time. For example:

Go
{{ .StartsAt | tz "Europe/Paris" | date "15:04:05 MST" }}

You can find a reference for Go’s time format here.

Differences with annotation and label templates

In the alert rule, you can also template annotations and labels to include additional information. For example, you might add a summary annotation that displays the query value triggering the alert.

Annotation and label templates add relevant information to individual alert instances, while notification templates inform about a group of alert instances.

Since both types of templates operate in distinct contexts, the functions and variables available in annotation and label templates differ from those used in notification templates.