在警报管理器接收器中使用警报注释
Using alert annotations in an alertmanager receiver
我有这样配置的警报:
ALERT InstanceDown
IF up == 0
FOR 30s
ANNOTATIONS {
summary = "Server {{ $labels.Server }} is down.",
description = "{{ $labels.Server }} ({{ $labels.job }}) is down for more than 30 seconds."
}
松弛接收器看起来像这样:
receivers:
- name: general_receiver
slack_configs:
- api_url: https://hooks.slack.com/services/token
channel: "#telemetry"
title: "My summary"
text: "My description"
是否可以在我的接收器中使用注释? This github comment 表示它是,但我无法从中得到任何东西。
您需要定义自己的模板(我只是想走那条路)。有关简要示例,请参见:https://prometheus.io/blog/2016/03/03/custom-alertmanager-templates/
这里的所有警报至少有一个 summary
和一个 runbook
注释。 runbook
包含一个 Wiki URL。我已经定义了以下模板(如上面的博客文章中所述)以将它们包含在松弛消息正文中:
{{ 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 }}
模板会覆盖 slack.default.text
,因此无需在 receiver
配置中引用它。
默认值可以在源代码以及 "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
有关 golang 模板语言的基础知识和详细信息:
为了完整起见,最终模板:
{{ 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 }}
我知道这个响应是多年后的,但我的方法是利用 .CommonAnnotations 和标签来显示信息,并利用警报名称来查询运行手册 -
alertmanager.yml
slack_configs:
- api_url:
channel: '#XXXXXXXXXXXXXXXX'
color: '{{ template "SLACK_MSG_COLOR" . }}'
send_resolved: true
title: '{{ template "SLACK_MSG_TITLE" . }}'
text: '{{ template "SLACK_MSG_TEXT" . }}'
pagerduty_configs:
- routing_key: '{{ template "Global_PD_Service_Key" . }}'
description: '{{ template "PAGERDUTY_DESCRIPTION" . }}'
severity: '{{ if .CommonLabels.severity }}{{ .CommonLabels.severity | toLower }}{{ else }}critical{{ end }}'
links:
- text: 'Prometheus'
href: '{{ (index .Alerts 0).GeneratorURL }}'
- text: 'Search Runbooks'
href: '{{ template "RUNBOOK_SEARCH" . }}'
和.tmpl
################
# Runbook
################
# Runbook Search
{{ define "RUNBOOK_SEARCH" }}https://dsmith73.github.io/101-docs/search/?q={{ .CommonLabels.alertname }}{{ end }}
################
# Slack
################
# Slack Color
{{ define "SLACK_MSG_COLOR" }}{{ if eq .Status "firing" }}{{ if eq .CommonLabels.severity "critical" }}danger{{ else if eq .CommonLabels.severity "error" }}danger{{ else if eq .CommonLabels.severity "warning" }}warning{{ else }}#439FE0{{ end }}{{ else}}good{{ end }}{{ end }}
# Slack Text
{{define "SLACK_MSG_TEXT" }}
<!here> - {{ .CommonAnnotations.description }}
`View:` :chart_with_upwards_trend:*<{{ (index .Alerts 0).GeneratorURL }}|Prometheus>* or :notebook:*<{{ template "RUNBOOK_SEARCH" . }}|Runbook>*
*Details:*
{{ range .CommonLabels.SortedPairs }}• *{{ .Name }}:* `{{ .Value }}`
{{ end }}
{{ end }}
#Slack Summary
{{ define "SLACK_TITLE_SUMMARY" -}}
{{- if .CommonAnnotations.summary -}}
{{- .CommonAnnotations.summary -}}
{{- else -}}
{{- with index .Alerts 0 -}}
{{- .Annotations.summary -}}
{{- end -}}
{{- end -}}
{{- end -}}
# Slack Title
{{ define "SLACK_MSG_TITLE" }}
{{ if eq .Status "resolved" }}
{{- .Status | toUpper }} : {{ template "SLACK_TITLE_SUMMARY" . }}
{{ else if eq .Status "firing" }}
{{ .CommonLabels.severity | toUpper }} : {{ template "SLACK_TITLE_SUMMARY" . }}
{{ end }}
{{ end }}
对我来说,这是将警报与运行手册相关联的好方法,而不是在规则或其他位置定义它们...
我有这样配置的警报:
ALERT InstanceDown
IF up == 0
FOR 30s
ANNOTATIONS {
summary = "Server {{ $labels.Server }} is down.",
description = "{{ $labels.Server }} ({{ $labels.job }}) is down for more than 30 seconds."
}
松弛接收器看起来像这样:
receivers:
- name: general_receiver
slack_configs:
- api_url: https://hooks.slack.com/services/token
channel: "#telemetry"
title: "My summary"
text: "My description"
是否可以在我的接收器中使用注释? This github comment 表示它是,但我无法从中得到任何东西。
您需要定义自己的模板(我只是想走那条路)。有关简要示例,请参见:https://prometheus.io/blog/2016/03/03/custom-alertmanager-templates/
这里的所有警报至少有一个 summary
和一个 runbook
注释。 runbook
包含一个 Wiki URL。我已经定义了以下模板(如上面的博客文章中所述)以将它们包含在松弛消息正文中:
{{ 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 }}
模板会覆盖 slack.default.text
,因此无需在 receiver
配置中引用它。
默认值可以在源代码以及 "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
有关 golang 模板语言的基础知识和详细信息:
为了完整起见,最终模板:
{{ 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 }}
我知道这个响应是多年后的,但我的方法是利用 .CommonAnnotations 和标签来显示信息,并利用警报名称来查询运行手册 -
alertmanager.yml
slack_configs:
- api_url:
channel: '#XXXXXXXXXXXXXXXX'
color: '{{ template "SLACK_MSG_COLOR" . }}'
send_resolved: true
title: '{{ template "SLACK_MSG_TITLE" . }}'
text: '{{ template "SLACK_MSG_TEXT" . }}'
pagerduty_configs:
- routing_key: '{{ template "Global_PD_Service_Key" . }}'
description: '{{ template "PAGERDUTY_DESCRIPTION" . }}'
severity: '{{ if .CommonLabels.severity }}{{ .CommonLabels.severity | toLower }}{{ else }}critical{{ end }}'
links:
- text: 'Prometheus'
href: '{{ (index .Alerts 0).GeneratorURL }}'
- text: 'Search Runbooks'
href: '{{ template "RUNBOOK_SEARCH" . }}'
和.tmpl
################
# Runbook
################
# Runbook Search
{{ define "RUNBOOK_SEARCH" }}https://dsmith73.github.io/101-docs/search/?q={{ .CommonLabels.alertname }}{{ end }}
################
# Slack
################
# Slack Color
{{ define "SLACK_MSG_COLOR" }}{{ if eq .Status "firing" }}{{ if eq .CommonLabels.severity "critical" }}danger{{ else if eq .CommonLabels.severity "error" }}danger{{ else if eq .CommonLabels.severity "warning" }}warning{{ else }}#439FE0{{ end }}{{ else}}good{{ end }}{{ end }}
# Slack Text
{{define "SLACK_MSG_TEXT" }}
<!here> - {{ .CommonAnnotations.description }}
`View:` :chart_with_upwards_trend:*<{{ (index .Alerts 0).GeneratorURL }}|Prometheus>* or :notebook:*<{{ template "RUNBOOK_SEARCH" . }}|Runbook>*
*Details:*
{{ range .CommonLabels.SortedPairs }}• *{{ .Name }}:* `{{ .Value }}`
{{ end }}
{{ end }}
#Slack Summary
{{ define "SLACK_TITLE_SUMMARY" -}}
{{- if .CommonAnnotations.summary -}}
{{- .CommonAnnotations.summary -}}
{{- else -}}
{{- with index .Alerts 0 -}}
{{- .Annotations.summary -}}
{{- end -}}
{{- end -}}
{{- end -}}
# Slack Title
{{ define "SLACK_MSG_TITLE" }}
{{ if eq .Status "resolved" }}
{{- .Status | toUpper }} : {{ template "SLACK_TITLE_SUMMARY" . }}
{{ else if eq .Status "firing" }}
{{ .CommonLabels.severity | toUpper }} : {{ template "SLACK_TITLE_SUMMARY" . }}
{{ end }}
{{ end }}
对我来说,这是将警报与运行手册相关联的好方法,而不是在规则或其他位置定义它们...