Template notifications
You can use notification templates to change the title, message, and format of notifications.
Grafana provides a default template for notification titles (default.title
) and one default template for notification messages (default.message
). Both templates display common alert details.
You can also create a notification template to customize the content and format of your notification messages. For example:
- Personalize the subject of an email or the title of a message.
- Modify text within notifications, like selecting or omitting certain labels, annotations, and links.
- Format text with bold and italic styles, and add or remove line breaks.
However, there are limitations. You cannot:
- Modify Visual Appearance: Add HTML or CSS to email notifications for visual changes. Alter the design of notifications in messaging services like Slack or Microsoft Teams, such as adding custom blocks or adaptive cards.
- Manage Media and Data: Customize the data structure or format passed to the templates, like adding new JSON fields or sending XML data for webhooks. Modify HTTP headers in webhooks beyond those defined in the configuration, or adjust the number, size, or placement of images.
Here’s an example that displays the summary and description annotations for each alert in the notification:
{{ define "custom.alerts" -}}
{{ len .Alerts }} alert(s)
{{ range .Alerts -}}
{{ template "alert.summary_and_description" . -}}
{{ end -}}
{{ end -}}
{{ define "alert.summary_and_description" }}
Summary: {{.Annotations.summary}}
Status: {{ .Status }}
Description: {{.Annotations.description}}
{{ end -}}
The notification message would look like this:
2 alert(s)
Summary: The database server db1 has exceeded 75% of available disk space.
Status: firing
Description: This alert fires when a database server is at risk of running out of disk space. You should take measures to increase the maximum available disk space as soon as possible to avoid possible corruption.
Summary: The web server web1 has been responding to 5% of HTTP requests with 5xx errors for the last 5 minutes.
Status: resolved
Description: This alert fires when a web server responds with more 5xx errors than is expected. This could be an issue with the web server or a backend service.
Note
Avoid adding extra information about alert instances in notification templates, as this information will only be visible in the notification message.
Instead, you should use annotations or labels to add information directly to the alert, ensuring it’s also visible in the alert state and alert history within Grafana. You can then print the new alert annotation or label in notification templates.
Select a notification template for a contact point
Notification templates are not tied to specific contact point integrations, such as email or Slack, and the same template can be shared across multiple contact points.
The notification template is assigned to the contact point to determine the notification message sent to contact point integrations.
By default, Grafana provides default templates, such as default.title
and default.message
, to format notification messages.
More information
For further details on how to write notification templates, refer to: