Grafana notification template creation issue

Please avoid posting in the Grafana parent category. Whenever possible choose a subcategory.

Hi All,

We are looking to create some custom template to send the notification in good format. We able to get some sort of data there but not able to present in defined format. We are looking to split the lines in template but it looks like taking all the contents as string and wrapping as per text.

Please find below the screen shot

Regards,
Shashi

THANK YOU!!

~ the grafana team

Hi,

We are using the below template to make the alert message.

{{ define “myalert” }}
[{{.Status}}] - {{ .Labels.alertname }},

{{ with .ValueString }}
Metric Values : {{- . | reReplaceAll \[\s “” | reReplaceAll \],\s “\n” | reReplaceAll \] “” | reReplaceAll var='[A-Z0-9a-z]+'\s “” | reReplaceAll labels=\{[A-Za-z0-9_\-\=\s\.:,]+\}\s “\n” | reReplaceAll metric='(.*)' " $1 " | reReplaceAll value=([0-9\.]+) “($1),” }}
{{ end }}

Labels:
{{ range .Labels.SortedPairs }}
{{ .Name }}: {{ .Value }},
{{ end }}

{{ if gt (len .Annotations) 0 }}
Annotations:
{{ range .Annotations.SortedPairs }}
{{ .Name }}: {{ .Value }},
{{ end }}
{{ end }}

{{ if gt (len .SilenceURL ) 0 }}
Silence alert: {{ .SilenceURL }},
{{ end }}
{{ if gt (len .DashboardURL ) 0 }}
Go to dashboard: {{ .DashboardURL }}.
{{ end }}
{{ end }}

=============

{{ define “mymessage” }}
{{ if gt (len .Alerts.Firing) 0 }}
{{ len .Alerts.Firing }} firing:
{{ range .Alerts.Firing }} {{ template “myalert” .}} {{ end }}
{{ end }}
{{ if gt (len .Alerts.Resolved) 0 }}
{{ len .Alerts.Resolved }} resolved:
{{ range .Alerts.Resolved }} {{ template “myalert” .}} {{ end }}
{{ end }}
{{ end }}

Regards,
Shashi

Check out this post - it’s for slack, but has steps to get the query response labels into your message template.