docs/sources/alerting/fundamentals/templates.md
Use templating to customize, format, and reuse alert notification messages. Create more flexible and informative alert notification messages by incorporating dynamic content, such as metric values, labels, and other contextual information.
In Grafana, you have various options to template your alert notification messages:
summary and description, to alert instances for notification messages.{{< admonition type="tip" >}} For a practical example of templating, refer to our Getting Started with Templating tutorial. {{< /admonition >}}
This diagram illustrates the entire templating process, from querying labels and templating the alert summary and notification to the final alert notification message.
{{< figure src="/media/docs/alerting/how-notification-templates-works.png" max-width="1200px" caption="How templating works" >}}
In this diagram:
12345, along with the values of the instance and job labels.instance label: server1.Annotations can be defined in the alert rule to add extra information to alert instances.
When creating an alert rule, Grafana suggests several optional annotations, such as description, summary, and runbook_url, which help identify and respond to alerts. You can also create custom annotations.
Annotations are key-value pairs, and their values can contain a combination of text and template code that is evaluated when the alert fires.
Annotations can contain plain text, but you should template annotations if you need to display query values that are relevant to the alert, for example:
Here’s an example of templating an annotation, which explains where and why the alert was triggered. In this case, the alert triggers when CPU usage exceeds a threshold, and the summary annotation provides the relevant details.
CPU usage for {{ $labels.instance }} has exceeded 80% ({{ $values.A.Value }}) for the last 5 minutes.
The outcome of this template would be:
CPU usage for Instance 1 has exceeded 80% (81.2345) for the last 5 minutes.
Implement annotations that provide meaningful information to respond to your alerts. Annotations are displayed in the Grafana alert detail view and are included by default in notifications.
For more details on how to template annotations, refer to Template annotations and labels.
Labels are used to differentiate one alert instance from all other alert instances, as the set of labels uniquely identifies an alert instance. Notification policies and silences use labels to handle alert instances.
You can also template labels based on query results. This is helpful if the labels you get from your query aren't detailed enough. For instance:
Here’s an example of templating a new env label based on the value of a query label:
{{- if eq $labels.instance "prod-server-1" -}}
production
{{- else if eq $labels.instance "staging-server-1" -}}
staging
{{- else -}}
development
{{- end -}}
For more details on how to template labels, refer to Template annotations and labels.
Notification templates allow you to customize the content of your notifications, such as the subject of an email or the body of a Slack message.
Notification templates differ from templating annotations and labels in the following ways:
Here is an example of a notification template that summarizes all firing and resolved alerts in a notification group:
{{ define "alerts.message" -}}
{{ if .Alerts.Firing -}}
{{ len .Alerts.Firing }} firing alert(s)
{{ template "alerts.summarize" .Alerts.Firing }}
{{- end }}
{{- if .Alerts.Resolved -}}
{{ len .Alerts.Resolved }} resolved alert(s)
{{ template "alerts.summarize" .Alerts.Resolved }}
{{- end }}
{{- end }}
{{ define "alerts.summarize" -}}
{{ range . -}}
- {{ index .Annotations "summary" }}
{{ end }}
{{ end }}
The notification message to the contact point would look like this:
1 firing alert(s)
- The database server db1 has exceeded 75% of available disk space. Disk space used is 76%, please resize the disk size within the next 24 hours.
1 resolved alert(s)
- The web server web1 has been responding to 5% of HTTP requests with 5xx errors for the last 5 minutes.
For more details, refer to Template notifications.