Search code examples
slackprometheusprometheus-alertmanager

Add slack fields to Prometheus alert manager slack notifications


The new version of Prometheus alert manager added support for fields section in slack attachments. I'm trying to setup a go template to loop generate fields for each alert label. After testing the config, I got syntax error "cannot read an implicit mapping pair; a colon is missed". Did anyone tried the same thing and succeed? Thanks very much. My config is below:

global:
  resolve_timeout: 5m
templates:
- '/etc/alertmanager/template/*.tmpl'
route:
  # All alerts in a notification have the same value for these labels.
  group_by: ['alertname', 'instance', 'pod']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 4h
  receiver: 'slack-test'
  routes:
  # Go spam channel
  - match:
      alertname: DeadMansSwitch
    receiver: 'null'
- name: 'slack-test'
  slack_configs:
  - channel: '#alert'
    api_url: 'https://hooks.slack.com/services/XXXXX/XXXX/XXXX'
    username: 'Prometheus Event Notification'
    color: '{{ if eq .Status "firing" }}danger{{ else }}good{{ end }}'
    title: '[`{{ .Labels.severity }}`] Server alert'
    text: |-
      {{ range .Alerts }}
        {{ .Annotations.message }}
      {{ end }}
    short_fields: true
    fields:
    {{ range .Labels.SortedPairs }}
      title:{{ .Name }}
      value:`{{ .Value }}`
    {{ end }}
    send_resolved: true
  #email_configs:
  #- to: 'your_alert_email_address'
  #  send_resolved: true
- name: 'null'

Tried this not work too.

    fields:
    {{ range .Labels.SortedPairs }}
     - title: {{ .Name }}
       value: `{{ .Value }}`
    {{ end }}

Solution

  • The issue is that you are using a go template inside the config file, but prometheus only supports go templating inside the config values. Title and Value both are of type "tmpl_string", meaning they are a string which is a go template. https://prometheus.io/docs/alerting/configuration/#field_config

    correct

     fields:
       title: '{{ if (true) }}inside the title VALUE{{ end }}'
       value: 'foo'
    

    incorrect

     fields:
       {{ if (true) }}outside the config values
       title: 'inside the title VALUE'
       value: 'foo'
      {{ end }}