How to get alertname in notification title

  • What Grafana version and what operating system are you using?
    Grafana 8.1.2 with NGAlert enabled

  • What are you trying to achieve?
    I want to see alert name in notifications titles using my own template

  • How are you trying to achieve it?
    I’ve created template:

{{ define "template_title" }}
[{{.Status}}] {{ .Labels.alertname }}
{{ end }}

and set it as templated title of the Slack message.
image

I get this code here - Message templating | Grafana documentation

  • What happened?
    Alert message in Slack is empty with just status
    image

  • What did you expect to happen?
    I expect to get normal alert message

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

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

No

Can you screenshot your full alert config? If you are using Grafana managed alerts with a classic condition, templating will not work.

You must use a multi-dimensional rule.

Hi Matt!
I don’t know how it works, but now I use template based on default one and get only title.

{{ define "template_title" }}
[{{ .Status | toUpper }}: {{ if eq .Status "firing" }}{{ .Alerts.Firing | len }}{{ else if eq .Status "resolved" }}{{.Alerts.Resolved | len }}{{ end }}] {{ .GroupLabels.SortedPairs.Values | join " " }}
{{ end }}

And yes, I use multi-dimensional rules everywhere.
I can’t explain why {{ .Labels.alertname }} isn’t working

3 Likes

I’m aware this is old, but I could find no other information on the fix. The documentation is severely lacking, but fortunately I found the answer in the source code.

Use {{ .CommonLabels.alertname }}

Hope this helps someone.

1 Like

@arrudick This is a great find. Thank you for posting. Can you paste a screenshot of a working example of this?

Thank you so much for this!!! I have lost the best part of the afternoon trying to get the alertname on the title of a MS teams alert and this has done the trick. Would have saved me a lot of time if this was made clear in the documentation! Thanks again.

Apologies @grant2 I did not see your reply.

1 Like

I did according to the example above but in my case it didn’t work. Does anyone have an idea why it didn’t work, I’m using grafana version 9.2.5