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

时间:2019-02-12 03:01:04

标签: slack prometheus prometheus-alertmanager

新版的Prometheus警报管理器在松弛附件中增加了对fields部分的支持。我正在尝试设置go template来循环为每个警报标签生成字段。测试完配置后,我收到语法错误“无法读取隐式映射对;遗漏了冒号”。有没有人尝试过同样的事情并且成功了?非常感谢。我的配置如下:

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 }}

1 个答案:

答案 0 :(得分:1)

问题是您在配置文件中使用了go模板,但是prometheus仅支持在 config值中使用go模板。 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 }}
相关问题