Template Variables in Grafana managed alert

Hi,
I am new to Grafana and I am trying Grafana managed alerts right now. I have halfway understood the setup of the alerts, but now I want to customize the text in the emails. For this I want to use Template Variables, but all the instructions I found I either didn’t understand or it just doesn’t work.

  • What Grafana version and what operating system are you using?
    Grafana Version 8.0.6 / Ubuntu (latest Version, only for my personal test environment)

  • What are you trying to achieve?
    I would like to enrich Grafana managed alerts with content from template variables.

  • How are you trying to achieve it?
    I created a simple alert rule (grafana managed alert):

A - query:
node_cpu_seconds_total{mode=“system”,cpu=“0”}

B - expression:
Reduce / Max / A

C - Expression:
Math / $B > 2140

Alert details:
Summary
{{ $labels.mode }}
{{ $labels.job }}
{{ $values.A }}
{{ $values.B }}
{{ $values.C }}

{{ $values.A.value }}
{{ $labels.A.cpu }}
{{ $labels.A.mode }}

{{ $values.B.value }}
{{ $labels.B.cpu }}
{{ $labels.B.mode }}

Custom Labels
mymode = {{ $labels.mode }}
myjob = {{ $labels.job }}
mybvalue = {{ $values.B }}
mybvaluevalue = {{ $values.B.value }}

  • What happened?
    Only the two variables mymode and myjob are resolved correctly and are displayed in the alarm under labels. All other values appear only as strings.

  • What did you expect to happen?
    All variables should be resolved, especially in the summary.

  • Can you copy/paste the configuration(s) that you are having problems with?
    see above

  • Did you receive any errors in the Grafana UI or in related logs? If so, please tell us exactly what they were.
    no. No errors.

  • Did you follow any online instructions? If so, what is the URL?
    Create Grafana managed alert rule | Grafana documentation

Alarm email content:
Firing: 1 alertname

Firing Alert Prom 02

summary: {{ $labels.mode }} {{ $labels.job }} {{ $values.A }} {{ $values.B }} {{ $values.C }} {{ $values.A.value }} {{ $labels.A.cpu }} {{ $labels.A.mode }} {{ $values.B.value }} {{ $labels.B.cpu }} {{ $labels.B.mode }}
Labels:

alertname: Alert Prom 02
cpu: 0
instance: xyz.abc.de:9100
job: node_exporter
mode: system
mybvalue: {{ $values.B }}
mybvaluevalue: {{ $values.B.value }}
myjob: node_exporter
mymode: system

Can someone explain to me exactly how the syntax must be or explain to me what I am misunderstanding or doing wrong?
Are there any further instructions for Grafana managed alerts and template variables?

4 Likes

I have a very similar question:

  • What Grafana version and what operating system are you using?
    Grafana Version 8.2.0 Grafana Pro Online; New Alerting System Enabled

  • What are you trying to achieve?
    I would like to enrich Grafana managed alerts with content from labels using Alert Templates

  • How are you trying to achieve it?
    I created a simple alert rule (grafana managed alert):

  • Alert Source: Grafana

  • Email Template:
    {{ define “rrzTestMessageAlertWithData” }}
    Labels:
    {{ range .Labels.SortedPairs }}
    {{ .Name }}: {{ .Value }}
    {{ end }}
    {{ if gt (len .Alerts.Firing) 0 }}
    {{ len .Alerts.Firing }} firing:
    {{ range .Alerts.Firing }} {{ template “alert” .}} {{ end }}
    {{ end }}
    Annotations:
    {{ if gt (len .Annotations) 0 }}
    Annotations:
    {{ range .Annotations.SortedPairs }}
    {{ .Name }}: {{ .Value }}
    {{ end }}
    {{ end }}
    {{ if gt (len .Alerts.Resolved) 0 }}
    {{ len .Alerts.Resolved }} resolved:
    {{ range .Alerts.Resolved }} {{ template “alert” .}} {{ end }}
    {{ end }}
    {{ end }}

  • Data Source: Azure Logs/LogAnalytics

  • Example Labels

  • Q: How do I properly reference the label data: FaultingApplicationPath in the AlertTemplate?

1 Like

I have the same problem.
Only {{ $labels.alertname }} and {{ $labels.datasource_uid }} are resolved.

Did you find a solution?

This topic was automatically closed after 365 days. New replies are no longer allowed.