2

I'm trying to include dynamic subject into my Grafana (Grafana v9.5.0, datasource - Logstash) alerts. Goal would be to have subject similar to : "$serviceName is failing...". I was trying to achieve this with alerting labels (e.g. include label subject:serviceName} or take subject from alert annotations, description or basically anything.

So far I tried a few different template scripts:

{{ define "email.subject" }} Service is failing in SYST env - {{ .Alert.Labels }} {{ end }}

{{ define "email.subject" }} {{- range .Alerts.Labels }}{{ .Name }}={{ .Value }} {{ end }} {{- range $k, $v := .Values }} {{ $k }}={{ $v }}{{ end }} {{ end }}

None of them work as expected. Usually I get empty subject or (no Subject). I tried reading Grafana documentation, but couldn't find anything concrete for this use case.

Any help or links to more information/examples would be highly appreciated. Cheers

markalex
  • 8,623
  • 2
  • 7
  • 32
eirugn
  • 21
  • 2

2 Answers2

1

I'm no expert in go templating, but I feel like range .Alerts.Labels is incorrect approach: .Alerts is an array, and doesn't have .Labels property.

You should try

{{ range .Alerts }}
  {{ range .Labels }}
    {{ .Name }}={{ .Value }}
  {{ end }}
{{ end }}
markalex
  • 8,623
  • 2
  • 7
  • 32
0

Thanks for the help, it helped to reached final template that look something like this:

{{ define "email.subject" }}

{{ range .Alerts }}
{{ $alert := . }}
{{ range .Labels.SortedPairs }}
{{ if eq .Name  "service" }}
Service -  {{ .Value }} failing with status - 500;
{{end}}

{{ end }}
{{ end }}

{{ end }}
eirugn
  • 21
  • 2