Using alert annotations in an alertmanager receiver

You need to define your own template (I just hat to walk that path). For a brief example see: https://prometheus.io/blog/2016/03/03/custom-alertmanager-templates/

All alerts here have at least a summary and a runbook annotation. runbook contains an Wiki URL. I've defined the following template (as described in the blog article above) to include them into the slack message body:

{{ define "__slack_text" }}                                                                                                                                                
{{ range .Alerts }}{{ .Annotations.summary }}                                                                                                                                    
{{ if gt (len .Labels) (len .GroupLabels) }}({{ with .Labels.Remove .GroupLabels.Names }}{{ .Values | join " " }}{{ end }}){{ end }}                                             
{{ end }}<{{ (index .Alerts 0).GeneratorURL }}|Source> | {{ if .CommonAnnotations.runbook }}<{{ .CommonAnnotations.runbook }}|:notebook_with_decorative_cover: Runbook>{{ else }}<https://wiki.some.where/Runbooks|:exclamation:*NO RUNBOOK*:exclamation:>{{ end }}                                                                                
{{ end }}   
{{ define "slack.default.text" }}{{ template "__slack_text" . }}{{ end }}  

The templates overwrites the slack.default.text so there is no need to reference it in the receiver configuration.

Defaults can be found in source as well as the "documentation":

  • https://github.com/prometheus/alertmanager/blob/v0.4.2/template/default.tmpl
  • https://github.com/prometheus/alertmanager/blob/v0.4.2/template/template.go

For basics and details about the golang templating language:

  • https://gohugo.io/templates/go-templates/
  • https://golang.org/pkg/text/template/

For completeness' sake, the eventual template:

{{ define "__slack_text" }}                                                                                                                                                
{{ range .Alerts }}{{ .Annotations.text }}{{ end }}                                                                                
{{ end }}

{{ define "__slack_title" }}                                                                                                                                                
{{ range .Alerts }}{{ .Annotations.title }}{{ end }}                                                                                
{{ end }}

{{ define "slack.default.text" }}{{ template "__slack_text" . }}{{ end }}
{{ define "slack.default.title" }}{{ template "__slack_title" . }}{{ end }}

Tags:

Prometheus