The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
grafana/docs/sources/shared/alerts/template-language.md

238 lines
8.4 KiB

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>
8 months ago
---
title: 'Template language'
---
## If
You can use `if` statements in templates. For example, you can print `Variable empty` when a variable is empty:
```go
{{ if $element }}
Element value: {{$element}}
{{ else }}
Element is empty
{{ end }}
```
## With
`with` is similar to `if` statements, but unlike `if`, it updates dot(`.`) to refer to the value of the expression in `with`:
```go
{{ with $array }}
There are {{ len . }} item(s)
{{ else }}
There are no alerts
{{ end }}
```
## Range
`range` iterates over an array or map, and dot (`.`) is set to the current element of the array:
```go
{{ range $array }}
{{ .itemPropertyName }}
{{ end }}
```
Optionally, you can handle empty objects using `else`:
```go
{{ range $array }}
{{ .itemPropertyName }}
{{ else }}
Empty array
{{ end }}
```
You can also get the index of each item within a range by defining index and value variables at the start of the range:
```go
{{ $num_items := len $array }}
{{ range $index, $item := $array }}
This is item {{ $index }} out of {{ $num_items }}
{{ end }}
```
Additionally, you can use `{{break}}` to stop the remaining iterations or `{{continue}}` to stop the current iteration and continue with the next one.
## Functions
The global functions available in `text/template` are:
| Function | Description |
| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `and` | Returns the boolean AND of its arguments by returning the first empty argument or the last argument. |
| `call` | Returns the result of calling the first argument, which must be a function, with the remaining arguments as parameters. |
| `html` | Returns the escaped HTML equivalent of the textual representation of its arguments. |
| `index` | Returns the result of indexing its first argument by the following arguments, e.g., `{{ index $labels "instance" }}` returns the `instance` key in the `$labels` map variable. |
| `slice` | Returns the result of slicing its first argument by the remaining arguments. |
| `js` | Returns the escaped JavaScript equivalent of the textual representation of its arguments. |
| `len` | Returns the integer length of its argument, e.g., `{{ len $array }}` |
| `not` | Returns the boolean negation of its single argument. |
| `or` | Returns the boolean OR of its arguments by returning the first non-empty argument or the last argument. |
| `print` | An alias for fmt.Sprint |
| `printf` | An alias for fmt.Sprintf |
| `println` | An alias for fmt.Sprintln |
| `urlquery` | Returns the escaped value of the textual representation of its arguments in a form suitable for embedding in a URL query |
For more details, refer to the official documentation on [functions in `text/template`](https://pkg.go.dev/text/template#hdr-Functions).
## Comparison operators
Boolean comparison operators are also available in `text/template`:
| Function | Description |
| -------- | ----------------------------------------- |
| `eq` | Returns the boolean truth of arg1 == arg2 |
| `ne` | Returns the boolean truth of arg1 != arg2 |
| `lt` | Returns the boolean truth of arg1 < arg2 |
| `le` | Returns the boolean truth of arg1 <= arg2 |
| `gt` | Returns the boolean truth of arg1 > arg2 |
| `ge` | Returns the boolean truth of arg1 >= arg2 |
## Variables
Variables in `text/template` must be created within the template. For example, you can create a variable with the current value of dot (`.`) and assign a string or another object to the variable like this:
```go
{{ $variable := . }}
{{ $variable := "This is a test" }}
{{ $variable }}
```
This template outputs:
```
This is a test
```
## Templates
You can create reusable templates that can be executed from other templates or within the same template.
Define templates using `define` and the name of the template in double quotes:
```go
{{ define "print_labels" }}
{{ end }}
```
You should not define templates with the same name as other templates, including default templates such as `__subject`, `__text_values_list`, `__text_alert_list`, `default.title` and `default.message`. Where a template has been created with the same name as a default template, or a template in another notification template, Grafana might use either template. Grafana does not prevent, or show an error message, when there are two or more templates with the same name.
### Execute templates
You can execute defined templates using `template`, the name of the template in double quotes, and the cursor that should be passed to the template:
```go
{{ template "print_labels" . }}
```
Within a template dot refers to the value that is passed to the template.
For example, if a template is passed a list of firing alerts then dot refers to that list of firing alerts:
```go
{{ template "print_alerts" .Alerts }}
```
If the template is passed the sorted labels for an alert then dot refers to the list of sorted labels:
```go
{{ template "print_labels" .SortedLabels }}
```
This is useful when writing reusable templates. For example, to print all alerts you might write the following:
```go
{{ template "print_alerts" .Alerts }}
```
Then to print just the firing alerts you could write this:
```go
{{ template "print_alerts" .Alerts.Firing }}
```
This works because both `.Alerts` and `.Alerts.Firing` is a list of alerts.
```go
{{ define "print_alerts" }}
{{ range . }}
{{ template "print_labels" .SortedLabels }}
{{ end }}
{{ end }}
```
{{% admonition type="note" %}}
You cannot create independent, reusable templates for labels and annotations as you can with notification templates. In alert rule templates, you need to write each template inline within the label or annotation field.
{{% /admonition %}}
## Comments
You can add comments with `{{/*` and `*/}}`:
```go
{{/* This is a comment */}}
```
To avoid adding line breaks, use:
```go
{{- /* This is a comment with no leading or trailing line breaks */ -}}
```
## Indentation
You can use indentation, both tabs and spaces, and line breaks, to make templates more readable:
```go
{{ range .Alerts }}
{{ range .Labels.SortedPairs }}
{{ .Name }} = {{ .Value }}
{{ end }}
{{ end }}
```
However, indentation in the template is also present in the text.
### Remove spaces and line breaks
In text/template use `{{-` and `-}}` to remove leading and trailing spaces and line breaks.
For example, when using indentation and line breaks to make a template more readable:
```go
{{ range .Alerts }}
{{ range .Labels.SortedPairs }}
{{ .Name }} = {{ .Value }}
{{ end }}
{{ end }}
```
The indentation and line breaks are also present in the text:
```
alertname = "Test"
grafana_folder = "Test alerts"
```
You can remove the indentation and line breaks from the text changing `}}` to `-}}` at the start of each range:
```go
{{ range .Alerts -}}
{{ range .Labels.SortedPairs -}}
{{ .Name }} = {{ .Value }}
{{ end }}
{{ end }}
```
The indentation and line breaks in the template are now absent from the text:
```
alertname = "Test"
grafana_folder = "Test alerts"
```