Prometheus Slack alert with FAQ url

帅比萌擦擦* 提交于 2019-12-13 05:42:19

问题


I have a Prometheus alert like this:

- alert: NginxCrashLoop
  annotations:
    description: Nginx at {{ $labels.pod }} is in Crash Loop
    identifier: '{{ $labels.node }}'
    runbook_url: https://sites.google.com/a/domain.com/bi/faq/nginx
  expr: |
    rate(kube_pod_container_status_restarts_total{container="frontend"}[2m]) * on(namespace, pod) group_left(node) kube_pod_info * 3600 > 3
  labels:
    severity: critical

and a Go template for Slack custom alerting:

{{ define "__single_message_title" }}{{ range .Alerts.Firing }}{{ .Labels.alertname }} at {{ .Annotations.identifier }}{{ end }}{{ range .Alerts.Resolved }}{{ .Labels.alertname }} at {{ .Annotations.identifier }}{{ end }}{{ end }}

{{ define "custom_title" }}[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ if or (and (eq (len .Alerts.Firing) 1) (eq (len .Alerts.Resolved) 0)) (and (eq (len .Alerts.Firing) 0) (eq (len .Alerts.Resolved) 1)) }}{{ template "__single_message_title" . }}{{ end }}{{ end }}

{{ define "faqURL" }}{{ range .Alerts.Firing }}{{ .Annotations.runbook_url }}
{{ end }}{{ end }}

{{ define "custom_slack_message" }}
{{ if or (and (eq (len .Alerts.Firing) 1) (eq (len .Alerts.Resolved) 0)) (and (eq (len .Alerts.Firing) 0) (eq (len .Alerts.Resolved) 1)) }}
{{ range .Alerts.Firing }}{{ .Annotations.description }}{{ end }}{{ range .Alerts.Resolved }}{{ .Annotations.description }}{{ end }}
{{ else }}
{{ if gt (len .Alerts.Firing) 0 }}
*Alerts Firing:*
{{ range .Alerts.Firing }}- {{ .Annotations.identifier }}: {{ .Annotations.description }}: <{{ template "faqURL" . }}|Quick FAQ>
{{ end }}{{ end }}
{{ if gt (len .Alerts.Resolved) 0 }}
*Alerts Resolved:*
{{ range .Alerts.Resolved }}- {{ .Annotations.identifier }}: {{ .Annotations.description }}
{{ end }}{{ end }}
{{ end }}
{{ end }}

I wish it was able to send me a link to a FAQ documentation to mitigate the problem, shown after the description. But, the slack message still doesn't contain the link. What am I doing wrong?


回答1:


Here is how I used

I have a custom template with slack text defined as below

{{`{{ define "slack.custom.text" }}`}}
{{`{{ range .Alerts }}`}}
*Alert:* {{`{{ .Annotations.summary }}`}} - `{{`{{ .Labels.severity }}`}}`
*Description:* {{`{{ .Annotations.description }}`}}
*Runbook:* {{`{{ .Annotations.runbook}}`}}
*Details:*
{{`{{ range .Labels.SortedPairs }}`}} • *{{`{{ .Name }}`}}:* `{{`{{ .Value }}`}}`
{{`{{ end }}`}}
{{`{{ end }}`}}
{{`{{ end }}`}}

and then call this in alert_manager_config like

slack_configs:
      - channel: slack-channel-id
        title: '{{`{{ template "slack.default.title" . }}`}}'
        text: '{{`{{ template "slack.custom.text" . }}`}}'

templates:
- '/etc/alertmanager/config/custom.tmpl'


来源:https://stackoverflow.com/questions/51301123/prometheus-slack-alert-with-faq-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!