Files
Pepe Cano 813632dff1 Alerting Docs: Update Alert rule and Notification Templating (#94069)
* Update links to the new location of the Templates intro page

* Set alert rule template structure

* Update the `Template notifications` structure/sidebar

* Move examples to examples pages

* Update menu titles and URL paths

* WIP reference

* added intro to examples.md

* reference page section intros

* examples title

* Template

* Continue Alert Template Reference

* example sections

* Almost finish with Reference page

* weight pages

* WIP - template variables Intro

* Edits Alert rule templates + Differences with notification templates

* examples-draft-2

* how to map annotations to notif templates

* examples v2

* moved example

* Continue Template annotations and labels

* Preview annotation templates

* Complete `Template annotations and labels`

* Update `Differences with notification templates`

* Polish a bit `Differences with notification tempaltes`

* added logic examples to `Examples`

* Finish sharing Template language

* Minor: set `anchor` link

* Minor edit

* Minor edit

* Minor changes

* Template language changes

* Document missing functions for alert rule templates

* 'Examples' v3

* `Examples` v3.1

* `Examples` v3.2

* added context/intro to `Examples`

* added a how to section to `Examples`

* moved legacy info in `Examples`

* added examples, sections in `Examples`

* formatting

* examples v5

* examples v5.1

* Complete update to `Notification template reference`

* Update `$values` description

* moved steps to `index`

* fixed urls

* removed section in `examples`

* added refs

* fixed urls

* refs fix

* blended the use case into the explanation

* Notification template reference: Minor readibility changes

* Edit `$value` in Alert rule template reference

* Edit `Manage notification templates

* links to ref and lang pages

* Minor copy changes

* Edit Template annotation overview + Prepare Template notifications overview

* `Select` vs `Choose`

* Template Overview

* Fix `heading` does not match `title`

* examples v6

* examples v6.1

* Fix spell errors

* Run `prettier --write` to fix md format errors

* examples v7

* examples v7.1

* examples v7.2

* examples v7.4

* examples v7.5

* examples v7.6

* all pretty, no pity

* Update alert rule examples

* Complete `Labels and annotations template examples`

* Minor changes

* Use `.` notation instead of `index` in examples

* rename `extendeddata` to `notificationdata`

* Complete Notification examples

* minor changes

* Set `go` language when displaying template examples

* Revert "minor changes"

This reverts commit f647c55c8718a4b5c5ab51b3ef88ef0605de3c84.

* Update example and admonition for consistency

* Update docs/sources/alerting/alerting-rules/templates/_index.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/alerting-rules/templates/_index.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/alerting-rules/templates/_index.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/alerting-rules/templates/_index.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/configure-notifications/template-notifications/manage-notification-templates.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/configure-notifications/template-notifications/manage-notification-templates.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/shared/alerts/template-language.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/shared/alerts/template-language.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/configure-notifications/template-notifications/manage-notification-templates.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/alerting-rules/templates/_index.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/alerting-rules/templates/_index.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/alerting-rules/templates/_index.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/alerting-rules/templates/_index.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/alerting-rules/templates/_index.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/alerting-rules/templates/_index.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/configure-notifications/template-notifications/_index.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/alerting-rules/templates/_index.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/alerting-rules/templates/_index.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/alerting-rules/templates/_index.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/alerting-rules/templates/_index.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/alerting-rules/templates/_index.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/alerting-rules/templates/_index.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/alerting-rules/templates/language.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/alerting-rules/templates/_index.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/alerting-rules/templates/_index.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/alerting-rules/templates/_index.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/alerting-rules/templates/examples.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/alerting-rules/templates/examples.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/configure-notifications/template-notifications/examples.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/alerting-rules/templates/examples.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/configure-notifications/template-notifications/examples.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/configure-notifications/template-notifications/manage-notification-templates.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/configure-notifications/template-notifications/manage-notification-templates.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/configure-notifications/template-notifications/manage-notification-templates.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/configure-notifications/template-notifications/manage-notification-templates.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* Update docs/sources/alerting/configure-notifications/template-notifications/manage-notification-templates.md

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>

* use `add` instead of `enrich`

* fix prettier content errors

* minor copy change

* Specify which Alert properties are exclusive to Grafana-managed alerts

* Use `Monitor` instead of `Meta monitoring`

* Be more detailed about notifation template limitations

---------

Co-authored-by: tonypowa <tonypowa@gmail.com>
Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>
2024-10-23 12:43:28 +02:00

13 KiB

aliases canonical description keywords labels title menuTitle weight refs
../../manage-notifications/template-notifications/reference/
https://grafana.com/docs/grafana/latest/alerting/configure-notifications/template-notifications/reference/ Learn about templating notifications options
grafana
alerting
notifications
templates
products
cloud
enterprise
oss
Notification template reference Template reference 102
alert-rule-template-reference alert-grouping template-language template-language-functions
pattern destination
/docs/grafana/ /docs/grafana/<GRAFANA_VERSION>/alerting/alerting-rules/templates/reference/
pattern destination
/docs/grafana-cloud/ /docs/grafana-cloud/alerting-and-irm/alerting/alerting-rules/templates/reference/
pattern destination
/docs/grafana/ /docs/grafana/<GRAFANA_VERSION>/alerting/fundamentals/notifications/group-alert-notifications/
pattern destination
/docs/grafana-cloud/ /docs/grafana-cloud/alerting-and-irm/alerting/fundamentals/notifications/group-alert-notifications/
pattern destination
/docs/grafana/ /docs/grafana/<GRAFANA_VERSION>/alerting/configure-notifications/template-notifications/language/
pattern destination
/docs/grafana-cloud/ /docs/grafana-cloud/alerting-and-irm/alerting/configure-notifications/template-notifications/language/
pattern destination
/docs/grafana/ /docs/grafana/<GRAFANA_VERSION>/alerting/configure-notifications/template-notifications/language/#functions
pattern destination
/docs/grafana-cloud/ /docs/grafana-cloud/alerting-and-irm/alerting/configure-notifications/template-notifications/language/#functions

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:

Name Type Description
Receiver string The name of the contact point sending the notification
Status string The status is firing if at least one alert is firing, otherwise resolved.
Alerts []Alert List of all firing and resolved alerts in this notification.
Alerts.Firing []Alert List of all firing alerts in this notification.
Alerts.Resolved []Alert List of all resolved alerts in this notification.
GroupLabels KV The labels that group these alerts in this notification based on the Group by option.
CommonLabels KV The labels common to all alerts in this notification.
CommonAnnotations KV The annotations common to all alerts in this notification.
ExternalURL string A 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 (.):

{{ 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:

Name Type Description
Status string Firing or resolved.
Labels KV The labels for this alert.
Annotations KV The annotations for this alert.
StartsAt time The time the alert fired
EndsAt time Only 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.
GeneratorURL string A link to Grafana, or the source of the alert if using an external alert generator.
Fingerprint string A unique string that identifies the alert.

Grafana-managed alerts include these additional properties:

Name Type Description
DashboardURL string A link to the Grafana Dashboard if the alert has a Dashboard UID annotation.
PanelURL string A link to the panel if the alert has a Panel ID annotation.
SilenceURL string A link to silence the alert.
Values KV The values of all expressions, including Classic Conditions.
ValueString string A 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:

{{ 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:

{{ 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 name Description Arguments Returns
SortedPairs Returns a sorted list of key/value pairs. Pairs
Remove Returns a copy of the KV with the keys removed []string KV
Names Return the names of the label names []string
Values Return a list of the values []string

Here's an example of using these methods:

{{ 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

Name Arguments Returns Description
title string string Capitalizes the first character of each word.
toUpper string string Returns all text in uppercase.
toLower string string Returns all text in lowercase.
trimSpace string string Removes leading and trailing white spaces.
match pattern, text boolean Matches the text against a regular expression pattern.
reReplaceAll pattern, replacement, text string Replaces text matching the regular expression.
join sep string, s []string string Concatenates the elements of s to create a single string. The separator string sep is placed between elements in the resulting string.
safeHtml string string Marks string as HTML not requiring auto-escaping.
stringSlice ...string string Returns the passed strings as a slice of strings. auto-escaping.

Here's an example of using these functions:

{{ 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:

{{ 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:

{{ .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.