EMail Alerts Subject

Hello Folk,
I had the following code implemented under

Alerting → Contact Points (Grafana as Alertmanager) → EMail → Subject

{{ if eq .Status “firing” }} [Alerting] {{index .CommonLabels “alertname” }} {{end}} {{ if gt (.Alerts.Resolved | len) 0 }} [OK] {{index .CommonLabels “alertname”}} {{end}}

It stopped to work.
I tried to simplify and I came to:

{{ if .Alerts.Firing }}[Alerting] Fire ON {{ .Alerts.EntityName }} {{ end }} {{ if .Alerts.Resolved }}[OK] Fire OFF {{ .Alerts.EntityName }} {{ end }}

But this return the following outcome:
" [Alerting] Fire ON [{firing map[EntityName:XXX alertname:XXX grafana_folder:OPS API Dashboard Alerts - QFqiNzJMk rule_id:XXX] map[Rule ID:XXX] 2023-06-19 09:43:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC https://XXX/view?orgId=1 XXX https:/XXX/alerting/silence/new?alertmanager=grafana&matcher=EntityName%3DXXX&matcher=alertname%3DXXX&matcher=grafana_folder%3DOPS+API+Dashboard+Alerts±+XXX&matcher=rule_id%3DXXX https://XXX?orgId=1 https://XXX?orgId=1&viewPanel=68 map[C:122.6 D:1] [ var=‘C’ labels={EntityName=XXX} value=122.6 ], [ var=‘D’ labels={EntityName=XXX} value=1 ] }]"

I replaced with XXX internal names. Can you suggest and easy solution? I checked in documentation but it seems that there is no answer to a Simple question like “setting up dynamically the title of the alert”…

hi @danielesalvigni :wave:

what is your version of Grafana, on what platform is it running, and what data source does your alert query?

Hello,
version 9.5.2, as docker container directly from docker HUB, and I am running it as is into an app service in Azure.

{{ .Alerts.EntityName }}

Alerts is a list. It doesn’t have “EntityName” element. So you need to loop through Alerts and retrieve value of “EntityName” from each individual Alert. (even if Alerts have just a single Alert).

It’s also possible that you have “EntityName” in GroupLabels or CommonLabels depending on how you configured alert grouping.

(I might be completely wrong :slight_smile: )

1 Like

The most likely reason this stopped working is because the grouping changed in the notification policy. If alertname is no longer in the group by it won’t be a common label.

Can you explain how/where to fix it?

Make sure alertname is in Group by for your notification policies, as can be seen here: