Error when templating grafana alerts

I am trying to template a grafana alert:

{{ define "template" }}
host {{ .Labels.alertname }} has been caused by {{ .Labels.host }} 
{{ .Annotations.Summary }}
{{ end }}

sc

But the alert is only sent up to the first label
sc3

This is the error log:

logger=alerting.notifier.slack level=error msg="Missing receiver"
logger=alerting.notifier.slack level=error msg="Missing group labels"
logger=alerting.notifier.slack lvl=warn msg="failed to template Slack message" err="template: template:3:15: executing \"template\" at <.Labels.alertname>: can't evaluate field Labels in type *channels.ExtendedData"

If I remove the template, the alert works fine

Any ideas what could cause this?
Thanks

Can you confirm: are you using the new Grafana Alerting platform (first released in Grafana 8) or the legacy alerting platform? I should note that the legacy platform is no longer actively developed:

https://grafana.com/docs/grafana/next/alerting/migrating-alerts/

I am using the new Grafana Alerting platform

1 Like

what is your grafana version?

We recently upgraded to grafana 9 and the error still persists

Other people seem to have the same issue as well:

An alert message may contain a collection of alerts instead of a single object. You should first {{ range .Alerts }} before calling other template variables or use {{ (index .Alerts 0) }} if you only care about the first alert message. I’d suggest using the range clause.

1 Like

I just responded to the post that you linked - so check it out and see if it’s helpful.

Something that can get confusing is that .Labels mentioned in the Template Data docs refer to the labels attached to the alert object:

If you want to get to the query response labels, you’ll want access them in the values map you see in the alert preview. There’s more in the other post if that’s what you’re after.

2 Likes