将松弛字段添加到 Prometheus 警报管理器松弛通知

Add slack fields to Prometheus alert manager slack notifications

Prometheus 警报管理器的新版本增加了对 fields section in slack attachments. I'm trying to setup a go template 的支持,可以为每个警报标签循环生成字段。测试配置后,我收到语法错误 "cannot read an implicit mapping pair; a colon is missed"。有没有人尝试过同样的事情并成功了?非常感谢。我的配置如下:

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'

试过了也不行。

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

问题是您在配置文件中使用了 go 模板,但 prometheus 仅支持在 配置值 中进行模板化。 Title 和 Value 都是 "tmpl_string" 类型,这意味着它们是一个字符串,是一个 go 模板。 https://prometheus.io/docs/alerting/configuration/#field_config

正确

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

不正确

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