Message templating

    This section shows an example of using templating to render a number of firing or resolved alerts in Slack message title, and listing alerts with status and name in the message body:

    You can create named templates and then reuse them in contact point fields or other templates.

    Grafana alerting UI allows you to configure templates for the Grafana managed alerts (handled by the embedded Alertmanager) as well as templates for an external Alertmanager if one is configured, using the Alertmanager dropdown.

    1. Click Add template.
    2. Fill in Name and Content fields.
    3. Click Save template button at the bottom of the page.

    Note The template name used to reference this template in templating is not the value of the Name field, but the parameter to tag in the content. When creating a template you can omit define entirely and it will be added automatically with same value as Name field. It’s recommended to use the same name for define and Name field to avoid confusion.

    Message templating - 图2

    1. In the Grafana side bar, hover your cursor over the Alerting (bell) icon and then click Contact points.
    2. Find the template you want to edit in the templates table and click the pen icon on the right side.
    3. Make any changes and click Save template button at the bottom of the page.
    1. In the Grafana side bar, hover your cursor over the Alerting (bell) icon and then click Contact points.
    2. Find the template you want to edit in the templates table and click the trash can icon on the right side.

    Note You are not prevented from deleting templates that are in use somewhere in contact points or other templates. Be careful!

    To use a template:

    Here is an example of a template to render a single alert:

    Template to render entire notification message:

    1. {{ define "message" }}
    2. {{ if gt (len .Alerts.Firing) 0 }}
    3. {{ len .Alerts.Firing }} firing:
    4. {{ range .Alerts.Firing }} {{ template "alert" .}} {{ end }}
    5. {{ if gt (len .Alerts.Resolved) 0 }}
    6. {{ len .Alerts.Resolved }} resolved:
    7. {{ range .Alerts.Resolved }} {{ template "alert" .}} {{ end }}

    Grafana alerting UI supports managing external Alertmanager configuration. Once you add an , a dropdown displays at the top of the page, allowing you to select either Grafana or an external Alertmanager data source.