Hi @georgerobinson
Following this tutorial https://grafana.com/docs/grafana/latest/alerting/manage-notifications/create-message-template/ I’m trying to setup a custom template for Grafana alerts.
- I created a slack.title template with this content:
{{ define "slack.title" }}
[{{.Status}}] {{ .Labels.alertname }}
{{ end }}
- Template slack.alert
{{ define "slack.alert" }}
[{{.Status}}] {{ .Labels.alertname }}
Labels:
{{ range .Labels.SortedPairs }}
{{ .Name }}: {{ .Value }}
{{ end }}
{{ if gt (len .Annotations) 0 }}
Annotations:
{{ range .Annotations.SortedPairs }}
{{ .Name }}: {{ .Value }}
{{ end }}
{{ end }}
{{ end }}
- Template slack.message
{{ define "slack.message" }}
{{ if gt (len .Alerts.Firing) 0 }}
{{ len .Alerts.Firing }} firing:
{{ range .Alerts.Firing }} {{ template "slack.alert" .}} {{ end }}
{{ end }}
{{ if gt (len .Alerts.Resolved) 0 }}
{{ len .Alerts.Resolved }} resolved:
{{ range .Alerts.Resolved }} {{ template "slack.alert" .}} {{ end }}
{{ end }}
{{ end }}
Then in the Optional Slack settings of a contact point, I added {{ template “slack.title” . }} in the Title field and {{ template “slack.message” . }} in the Text body field.
Then clicking on Test button an empty notification shows up in the dedicated Slack channel:
Am I missing something?
Hi! The error is in slack.title
as .Labels
does not exist unless the value of .
is an alert.
Don’t forget that message templates template lists of alerts, not single alerts. You need to range over the alerts like you did in your slack.message
template, use .CommonLabels
or .GroupLabels
.
Please also check out the next documentation for 9.4, as we have improved the documentation for templating. Customize notifications | Grafana documentation Thanks!
Thanks for your reply.
I’m not sure to understand well enough how to write down the template code.
It would be very helpful if you could add comments to your code explaining carefully what each directive does!
Hi! Have you looked at the documentation I linked? It should explain everything you need to understand how to write template code.
Hi @georgerobinson
I’ve been reading through your documentation but I haven’t managed to create a custom template similar to the one we had with version 8.
Do you have a code snippet for this kind of template?
Hi! I don’t have a reference template for replicating old alerting, is that something the community would find useful?
Hi @georgerobinson
I see in this forum, that there’s a lot of people asking how to remove labels and annotations in order to create a custom template.
With v8, it was super easy when creating alerts: just filling out some settings and clicking a button to confirm. With v9 there’s a bunch of default data showing up in alerts, and no tutorial whatsoever about customising default alert messages…
For example, I created a title template with the code snippet from one of your docs:
[{{ .Status }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ if gt (.Alerts.Resolved | len) 0 }}, RESOLVED:{{ .Alerts.Resolved | len }}{{ end }}{{ end }}]{{ end }}{{ .GroupLabels.SortedPairs.Values | join " " }}{{ if gt (len .CommonLabels) (len .GroupLabels) }}({{ with .CommonLabels.Remove .GroupLabels.Names }}{{ .Values | join " " }}{{ end }}){{ end }}{{ end }}
Testing the alert, a long title shows up:
[FIRING:1] Nginx active connections alert (website.com) Monitoring - Tier 1 servers (bO1Jih54z)
Instead what I would need to show up is:
[Firing:1] Nginx active connections alert (website.com)
That means that I would need to select only a single item from labels/annotations items (in this case “message” item).
I haven’t found in any of your docs how to achieve this…