mirror of
https://github.com/grafana/grafana.git
synced 2025-09-28 18:04:20 +08:00

* 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>
86 lines
4.3 KiB
Markdown
86 lines
4.3 KiB
Markdown
---
|
||
aliases:
|
||
- ../../manage-notifications/template-notifications/using-go-templating-language/ # /docs/grafana/<GRAFANA_VERSION>/alerting/manage-notifications/template-notifications/using-go-templating-language/
|
||
- ../../configure-notifications/template-notifications/using-go-templating-language/ # /docs/grafana/<GRAFANA_VERSION>/alerting/configure-notifications/template-notifications/using-go-templating-language/
|
||
weight: 104
|
||
canonical: https://grafana.com/docs/grafana/latest/alerting/configure-notifications/template-notifications/language/
|
||
description: Use Go template language to create your notification and alert rule templates
|
||
keywords:
|
||
- grafana
|
||
- alerting
|
||
- templates
|
||
- write templates
|
||
labels:
|
||
products:
|
||
- cloud
|
||
- enterprise
|
||
- oss
|
||
title: Alerting template language
|
||
menuTitle: Template language
|
||
refs:
|
||
alert-rule-template-reference:
|
||
- pattern: /docs/grafana/
|
||
destination: /docs/grafana/<GRAFANA_VERSION>/alerting/alerting-rules/templates/reference/
|
||
- pattern: /docs/grafana-cloud/
|
||
destination: /docs/grafana-cloud/alerting-and-irm/alerting/alerting-rules/templates/reference/
|
||
alert-rule-template-reference-variables:
|
||
- pattern: /docs/grafana/
|
||
destination: /docs/grafana/<GRAFANA_VERSION>/alerting/alerting-rules/templates/reference/#variables
|
||
- pattern: /docs/grafana-cloud/
|
||
destination: /docs/grafana-cloud/alerting-and-irm/alerting/alerting-rules/templates/reference/#variables
|
||
notification-template-reference:
|
||
- pattern: /docs/grafana/
|
||
destination: /docs/grafana/<GRAFANA_VERSION>/alerting/configure-notifications/template-notifications/reference/
|
||
- pattern: /docs/grafana-cloud/
|
||
destination: /docs/grafana-cloud/alerting-and-irm/alerting/configure-notifications/template-notifications/reference/
|
||
reference-notificationdata:
|
||
- pattern: /docs/grafana/
|
||
destination: /docs/grafana/<GRAFANA_VERSION>/alerting/configure-notifications/template-notifications/reference/#notification-data
|
||
- pattern: /docs/grafana-cloud/
|
||
destination: /docs/grafana-cloud/alerting-and-irm/alerting/configure-notifications/template-notifications/reference/#notification-data
|
||
---
|
||
|
||
# Alerting template language
|
||
|
||
Notification templates and alert rule templates, such as annotations and labels, both use the Go template language, [text/template](https://pkg.go.dev/text/template).
|
||
|
||
Both types of templates can use the same keywords, functions, and comparison operators of the Go template language, such as `range`, `if`, `and`, `index`, `eq`, and more.
|
||
|
||
However, it's important to note that because notifications and alert rules operate in distinct context, some additional variables and functions are only available for either notification or alert rule templates. Refer to:
|
||
|
||
- [Annotation and label template reference](ref:alert-rule-template-reference)
|
||
- [Notification template reference](ref:notification-template-reference)
|
||
|
||
This documentation provides an overview of the functions and operators of the Go template language that are available for both notification and alert rule templates.
|
||
|
||
## Print
|
||
|
||
To print the value of something, use `{{` and `}}`. You can print the value of a [variable](#variables), a field of a variable, the result of a function, or the value of dot.
|
||
|
||
```
|
||
{{ $values }}
|
||
{{ $values.A.Value }}
|
||
{{ humanize 1000.0 }}
|
||
{{ .Alerts }}
|
||
```
|
||
|
||
## Dot
|
||
|
||
In `text/template`, there is a special cursor called dot, written as `.`. You can think of this cursor as a variable whose value changes depending on where in the template it is used.
|
||
|
||
At the start of notification templates, dot (`.`) refers to [Notification Data](ref:reference-notificationdata).
|
||
|
||
```
|
||
{{ .Alerts }}
|
||
```
|
||
|
||
In annotation and label templates, dot (`.`) is initialized with all alert data. It’s recommended to use the [`$labels` and `$values` variables](ref:alert-rule-template-reference-variables) instead to directly access the alert labels and query values.
|
||
|
||
{{% admonition type="note" %}}
|
||
Dot (`.`) might refer to something else when used in a [range](#range), a [with](#with), or when writing [templates](#templates) used in other templates.
|
||
{{% /admonition %}}
|
||
|
||
[//]: <> (The above section is not included in the shared file because `refs` links are not supported in shared files.)
|
||
|
||
{{< docs/shared lookup="alerts/template-language.md" source="grafana" version="<GRAFANA VERSION>" >}}
|