Commit 88430f7c authored by conorbroderick's avatar conorbroderick Committed by Brian Brazil

Documentation for alert notification templates

parent 89230cb0
---
title: Notification template examples
sort_rank: 7
---
# Notification Template Examples
The following are all different examples of alerts and corresponding Alertmanager configuration file setups (alertmanager.yml).
Each use the [Go templating](http://golang.org/pkg/text/template/) system.
## Customizing Slack notifications
In this example we've customised our Slack notification to send a URL to our organisation's wiki on how to deal with the particular alert that's been sent.
```
global:
slack_api_url: '<slack_webhook_url>'
route:
- receiver: 'slack-notifications'
group_by: [alertname, datacenter, app]
receivers:
- name: 'slack-notifications'
slack_configs:
- channel: '#alerts'
text: 'https://internal.myorg.net/wiki/alerts/{{ .GroupLabels.app }}/{{ .GroupLabels.alertname }}'
```
## Accessing annotations in CommonAnnotations
In this example we again customize the text sent to our Slack receiver accessing the `summary` and `description` stored in the `CommonAnnotations` of the data sent by the Alertmanager.
Alert
```
ALERT InstanceDown
IF up == 0
FOR 5m
LABELS { severity = "page" }
ANNOTATIONS {
# Prometheus templates apply here in the annotation and label fields of the alert.
summary = "Instance {{ $labels.instance }} down",
description = "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes."
}
```
Receiver
```
- name: 'team-x'
slack_configs:
- channel: '#alerts'
# Alertmanager templates apply here.
text: "<!channel> \nsummary: {{ .CommonAnnotations.summary }}\ndescription: {{ .CommonAnnotations.description }}"
```
## Ranging over all received Alerts
Finally, assuming the same alert as the previous example, we customize our receiver to range over all of the alerts received from the Alertmanager, printing their respective annotation summaries and descriptions on new lines.
Receiver
```
- name: 'default-receiver'
slack_configs:
- channel: '#alerts'
- title: "{{ range .Alerts }}{{ .Annotations.summary }}\n{{ end }}"
- text: "{{ range .Alerts }}{{ .Annotations.description }}\n{{ end }}"
```
## Defining reusable templates
Going back to our first example, we can also provide a file containing named templates which are then loaded by Alertmanager in order to avoid complex templates that span many lines.
Create a file under `/alertmanager/template/myorg.tmpl` and create a template in it named "slack.myorg.txt":
```
{{ define "slack.myorg.text" }}https://internal.myorg.net/wiki/alerts/{{ .GroupLabels.app }}/{{ .GroupLabels.alertname }}{{ end}}
```
The configuration now loads the template with the given name for the "text" field and we provide a path to our custom template file:
```
global:
slack_api_url: '<slack_webhook_url>'
route:
- receiver: 'slack-notifications'
group_by: [alertname, datacenter, app]
receivers:
- name: 'slack-notifications'
slack_configs:
- channel: '#alerts'
text: '{{ template "slack.myorg.text" . }}'
templates:
- '/etc/alertmanager/templates/myorg.tmpl
```
This example is explained in further detail in this [blogpost](https://prometheus.io/blog/2016/03/03/custom-alertmanager-templates/).
---
title: Notification template reference
sort_rank: 6
---
# Notification Template Reference
Prometheus creates and sends alerts to the Alertmanager which then sends notifications out to different receivers based on their labels.
A receiver can be one of many integrations including: Slack, PagerDuty, email, or a custom integration via the generic webhook interface.
The notifications sent to receivers are constructed via templates. The Alertmanager comes with default templates but they can also be customized.
To avoid confusion it's important to note that the Alertmanager templates differ from [templating in Prometheus](https://prometheus.io/docs/visualization/template_reference/), however Prometheus templating also includes the templating in alert rule labels/annotations.
The Alertmanager's notification templates are based on the [Go templating](http://golang.org/pkg/text/template) system.
Note that some fields are evaluated as text, and others as HTML which will affect escaping.
# Data Structures
## Data
`Data` is the structure passed to notification templates and webhook pushes.
| Name | Type | Notes |
| ------------- | ------------- | -------- |
| Receiver | string | Defines the receiver's name that the notification will be sent to (slack, email etc.). |
| Status | string | Defined as firing if at least one alert is firing, otherwise resolved. |
| Alerts | [Alert](#alert) | List of alert objects ([see below](#alert)). |
| GroupLabels | [KV](#kv) | The labels these alerts were grouped by. |
| CommonLabels | [KV](#kv) | The labels common to all of the alerts. |
| CommonAnnotations | [KV](#kv) | Set of common annotations to all of the alerts. Used for longer additional strings of information about the alert. |
| ExternalURL | string | Backlink to the Alertmanager that sent the notification. |
## Alert
`Alert` holds one alert for notification templates.
| Name | Type | Notes |
| ------------- | ------------- | -------- |
| Status | string | Defines whether or not the alert is resolved or currently firing. |
| Labels | [KV](#kv) | A set of labels to be attached to the alert. |
| Annotations | [KV](#kv) | A set of annotations for the alert. |
| StartsAt | time.Time | The time the alert started firing. If omitted, the current time is assigned by the Alertmanager. |
| EndsAt | time.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 backlink which identifies the causing entity of this alert. |
## KV
`KV` is a set of key/value string pairs used to represent labels and annotations.
```
type KV map[string]string
```
Annotation example containing two annotations:
```
{
summary: "alert summary",
description: "alert description",
}
```
In addition to direct access of data (labels and annotations) stored as KV, there are also methods for sorting, removing, and viewing the LabelSets:
### KV methods
| Name | Arguments | Returns | Notes |
| ------------- | ------------- | -------- | -------- |
| SortedPairs | - | Pairs (list of key/value string pairs.) | Returns a sorted list of key/value pairs. |
| Remove | []string | KV | Returns a copy of the key/value map without the given keys. |
| Names | - | []string | Returns the names of the label names in the LabelSet. |
| Values | - | []string | Returns a list of the values in the LabelSet. |
# Functions
Note the [default
functions](http://golang.org/pkg/text/template/#hdr-Functions) also provided by Go
templating.
## Strings
| Name | Arguments | Returns | Notes |
| ------------- | ------------- | -------- | -------- |
| title | string |[strings.Title](http://golang.org/pkg/strings/#Title), capitalises first character of each word. |
| join | sep string, s []string | [strings.Join](http://golang.org/pkg/strings/#Join), concatenates the elements of s to create a single string. The separator string sep is placed between elements in the resulting string. (note: argument order inverted for easier pipelining in templates.) |
| toUpper | string | [strings.ToUpper](http://golang.org/pkg/strings/#ToUpper), converts all characters to upper case. |
| toLower | string | [strings.ToLower](http://golang.org/pkg/strings/#ToLower), converts all characters to lower case. |
| safeHtml | text string | [html/template.HTML](https://golang.org/pkg/html/template/#HTML), Marks string as HTML not requiring auto-escaping. |
|reReplaceAll | pattern, replacement, text | [Regexp.ReplaceAllString](http://golang.org/pkg/regexp/#Regexp.ReplaceAllString) Regexp substitution, unanchored. |
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment