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

12 KiB
Raw Blame History

canonical description keywords labels title menuTitle weight refs
https://grafana.com/docs/grafana/latest/alerting/alerting-rules/templates/examples/ Examples of templating labels and annotations in Grafana alert rules
grafana
alerting
templating
labels
annotations
products
cloud
enterprise
oss
Labels and annotations template examples Examples 102
labels annotations alert-rule-templates reference reference-labels reference-values reference-humanize reference-humanizepercentage reference-match reference-functions language-functions language-index language
pattern destination
/docs/grafana/ /docs/grafana/<GRAFANA_VERSION>/alerting/fundamentals/alert-rules/annotation-label/#labels
pattern destination
/docs/grafana-cloud/ /docs/grafana-cloud/alerting-and-irm/alerting/fundamentals/alert-rules/annotation-label/#labels
pattern destination
/docs/grafana/ /docs/grafana/<GRAFANA_VERSION>/alerting/fundamentals/alert-rules/annotation-label/#annotations
pattern destination
/docs/grafana-cloud/ /docs/grafana-cloud/alerting-and-irm/alerting/fundamentals/alert-rules/annotation-label/#annotations
pattern destination
/docs/grafana/ /docs/grafana/<GRAFANA_VERSION>/alerting/alerting-rules/templates/
pattern
/docs/grafana-cloud/alerting-and-irm/alerting/alerting-rules/templates/
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/alerting-rules/templates/reference/#labels
pattern destination
/docs/grafana-cloud/ /docs/grafana-cloud/alerting-and-irm/alerting/alerting-rules/templates/reference/#labels
pattern destination
/docs/grafana/ /docs/grafana/<GRAFANA_VERSION>/alerting/alerting-rules/templates/reference/#values
pattern destination
/docs/grafana-cloud/ /docs/grafana-cloud/alerting-and-irm/alerting/alerting-rules/templates/reference/#values
pattern destination
/docs/grafana/ /docs/grafana/<GRAFANA_VERSION>/alerting/alerting-rules/templates/reference/#humanize
pattern destination
/docs/grafana-cloud/ /docs/grafana-cloud/alerting-and-irm/alerting/alerting-rules/templates/reference/#humanize
pattern destination
/docs/grafana/ /docs/grafana/<GRAFANA_VERSION>/alerting/alerting-rules/templates/reference/#humanizepercentage
pattern destination
/docs/grafana-cloud/ /docs/grafana-cloud/alerting-and-irm/alerting/alerting-rules/templates/reference/#humanizepercentage
pattern destination
/docs/grafana/ /docs/grafana/<GRAFANA_VERSION>/alerting/alerting-rules/templates/reference/#match
pattern destination
/docs/grafana-cloud/ /docs/grafana-cloud/alerting-and-irm/alerting/alerting-rules/templates/reference/#match
pattern destination
/docs/grafana/ /docs/grafana/<GRAFANA_VERSION>/alerting/alerting-rules/templates/reference/#functions
pattern destination
/docs/grafana-cloud/ /docs/grafana-cloud/alerting-and-irm/alerting/alerting-rules/templates/reference/#functions
pattern destination
/docs/grafana/ /docs/grafana/<GRAFANA_VERSION>/alerting/alerting-rules/templates/language/#functions
pattern destination
/docs/grafana-cloud/ /docs/grafana-cloud/alerting-and-irm/alerting/alerting-rules/templates/language/#functions
pattern destination
/docs/grafana/ /docs/grafana/<GRAFANA_VERSION>/alerting/alerting-rules/templates/language/#functions
pattern destination
/docs/grafana-cloud/ /docs/grafana-cloud/alerting-and-irm/alerting/alerting-rules/templates/language/#functions
pattern destination
/docs/grafana/ /docs/grafana/<GRAFANA_VERSION>/alerting/alerting-rules/templates/language
pattern
/docs/grafana-cloud/alerting-and-irm/alerting/alerting-rules/templates/language

Labels and annotations template examples

Templating allows you to add dynamic data from queries to alert labels and annotations. Dynamic data enhances alert context, making it easier for responders to quickly assess and address the issue.

This page provides common examples for templating labels and annotations. For more information on templating, refer to:

Annotation example

Annotations add extra details to alert instances and are often used to provide helpful information for identifying the issue and guiding the response.

A common use case for annotations is to display the specific query value or threshold that triggered the alert.

For example, you can display the query value from the $values variable to inform about the CPU value that triggered the alert.

CPU usage has exceeded 80% ({{ $values.A.value }}) for the last 5 minutes.

Alternatively, you can use the index() function to retrieve the query value as follows.

CPU usage has exceeded 80% ({{ index $values "A" }}) for the last 5 minutes.
CPU usage has exceeded 80% (81.2345) for the last 5 minutes.

Include labels for extra details

To provide additional context, you can include labels from the query. For instance, access the $labels variable to display a label that informs about the affected instance:

CPU usage for {{ $labels.instance }} has exceeded 80% ({{ $values.A.Value }}) for the last 5 minutes.
CPU usage for Instance 1 has exceeded 80% (81.2345) for the last 5 minutes.

Annotations can also be used to provide a summary of key alert labels, such as the environment and alert severity. For instance, you can display a summary of the alert with important labels like:

Alert triggered in {{ $labels.environment }} with severity {{ $labels.severity }}
Alert triggered in production with severity critical.

Print a range query

To print the value of an instant query you can print its Ref ID using the index function or the $values variable:

{{ $values.A.Value }}

For range queries, reduce them from a time series to an instant vector using a reduce expression. You can then print the result by referencing its Ref ID. For example, if the reduce expression averages A with the Ref ID B, you would then print $values.B:

{{ $values.B.Value }}

Humanize the value of a query

To print the humanized value of an instant query, use the humanize function:

{{ humanize $values.A.Value }}

Alternatively:

{{ humanize (index $values "A").Value }}
554.9

To print the value of an instant query as a percentage, use the humanizePercentage function:

{{ humanizePercentage $values.A.Value }}
10%

For additional functions to display or format data, refer to:

Label example

Labels determine how alerts are routed and managed, ensuring that notifications reach the right teams at the right time. If the labels returned by your queries dont fully capture the necessary context, you can create a new label and sets its value based on query data.

Based on query value

Heres an example of creating a severity label based on a query value:

{{ if (gt $values.A.Value 90.0) -}}
critical
{{ else if (gt $values.A.Value 80.0) -}}
high
{{ else if (gt $values.A.Value 60.0) -}}
medium
{{ else -}}
low
{{- end }}

In this example, the severity label is determined by the query value:

  • critical for values above 90,
  • high for values above 80,
  • medium for values above 60,
  • and low for anything below.

You can then use the severity label to control how alerts are handled. For instance, you could send critical alerts immediately, while routing low severity alerts to a team for further investigation.

{{% admonition type="note" %}} You should avoid displaying query values in labels, as this may create many alert instances—one for each distinct label value. Instead, use annotations to convey query values. {{% /admonition %}}

Based on query label

You can use labels to differentiate alerts coming from various environments (e.g., production, staging, dev). For example, you may want to add a label that sets the environment based on the instances label. Heres how you can template it:

{{ if eq $labels.instance "prod-server-1" }}production
{{ else if eq $labels.instance "staging-server-1" }}staging
{{ else }}development
{{ end }}

This would print:

  • For instance prod-server-1, the label would be production.
  • For staging-server-1, the label would be staging.
  • All other instances would be labeled development.

To make this template more flexible, you can use a regular expression that matches the instance name with the instance name prefix using the match() function:

{{ if match "^prod-server-.*" $labels.instance }}production
{{ else if match "^staging-server-.*" $labels.instance}}staging
{{ else }}development
{{ end }}

{{< collapse title="Legacy Alerting templates" >}}

Legacy Alerting templates

For users working with Grafana's legacy alerting system, templates can still be utilized to extract useful information from alert conditions. However, it's important to note that you cannot use $labels to print labels from the query if you are using classic conditions, and must use $values instead. The reason for this is classic conditions discard these labels to enforce uni-dimensional behavior (at most one alert per alert rule). If classic conditions didn't discard these labels, then queries that returned many time series would cause alerts to flap between firing and resolved constantly as the labels would change every time the alert rule was evaluated.

Instead, the $values variable contains the reduced values of all time series for all conditions that are firing. For example, if you have an alert rule with a query A that returns two time series, and a classic condition B with two conditions, then $values would contain B0, B1, B2 and B3. If the classic condition B had just one condition, then $values would contain just B0 and B1.

Print all labels from a classic condition

To print all labels of all firing time series use the following template (make sure to replace B in the regular expression with the Ref ID of the classic condition if it's different):

{{ range $k, $v := $values -}}
{{ if (match "B[0-9]+" $k) -}}
{{ $k }}: {{ $v.Labels }}{{ end }}
{{ end }}

For example, a classic condition for two time series exceeding a single condition would print:

B0: instance=server1
B1: instance=server2

If the classic condition has two or more conditions, and a time series exceeds multiple conditions at the same time, then its labels will be duplicated for each condition that is exceeded:

B0: instance=server1
B1: instance=server2
B2: instance=server1
B3: instance=server2

If you need to print unique labels you should consider changing your alert rules from uni-dimensional to multi-dimensional instead. You can do this by replacing your classic condition with reduce and math expressions.

Print all values from a classic condition

To print all values from a classic condition take the previous example and replace $v.Labels with $v.Value:

{{ range $k, $v := $values -}}
{{ if (match "B[0-9]+" $k) -}}
{{ $k }}: {{ $v.Value }}{{ end }}
{{ end }}

For example, a classic condition for two time series exceeding a single condition would print:

B0: 81.2345
B1: 84.5678

If the classic condition has two or more conditions, and a time series exceeds multiple conditions at the same time, then $values will contain the values of all conditions:

B0: 81.2345
B1: 92.3456
B2: 84.5678
B3: 95.6789

{{< /collapse >}}