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/alerting/contact-points/message-templating/example-template.md

69 lines
1.6 KiB

---
aliases:
- /docs/grafana/latest/alerting/contact-points/message-templating/
- /docs/grafana/latest/alerting/contact-points/message-templating/example-template/
- /docs/grafana/latest/alerting/message-templating/
- /docs/grafana/latest/alerting/unified-alerting/message-templating/
keywords:
- grafana
- alerting
- guide
- contact point
- templating
title: Example message template
weight: 115
---
# Example of a custom template
Here's an example of how to use a custom template. You can also use the default template included in the setup.
Step 1: Configure a template to render a single alert.
```
{{ define "myalert" }}
[{{.Status}}] {{ .Labels.alertname }}
Labels:
{{ range .Labels.SortedPairs }}
{{ .Name }}: {{ .Value }}
{{ end }}
{{ if gt (len .Annotations) 0 }}
Annotations:
{{ range .Annotations.SortedPairs }}
{{ .Name }}: {{ .Value }}
{{ end }}
{{ end }}
{{ if gt (len .SilenceURL ) 0 }}
Silence alert: {{ .SilenceURL }}
{{ end }}
{{ if gt (len .DashboardURL ) 0 }}
Go to dashboard: {{ .DashboardURL }}
{{ end }}
{{ end }}
```
Step 2: Configure a template to render entire notification message.
```
{{ define "mymessage" }}
{{ if gt (len .Alerts.Firing) 0 }}
{{ len .Alerts.Firing }} firing:
{{ range .Alerts.Firing }} {{ template "myalert" .}} {{ end }}
{{ end }}
{{ if gt (len .Alerts.Resolved) 0 }}
{{ len .Alerts.Resolved }} resolved:
{{ range .Alerts.Resolved }} {{ template "myalert" .}} {{ end }}
{{ end }}
{{ end }}
```
Step 3: Add `mymessage` in the notification message field.
```
Alert summary:
{{ template "mymessage" . }}
```