Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
support for generating slack receivers with buttons for alerts
Signed-off-by: Sandeep Sukhani <[email protected]>
  • Loading branch information
sandeepsukhani committed Apr 20, 2020
commit a9f13f6eac941e57d33f3214d335be2cd0da6fa8
55 changes: 47 additions & 8 deletions prometheus-ksonnet/lib/alertmanager.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,58 @@
]
else [],

slackAlertTitle:: '[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .GroupLabels.cluster }}: {{ .GroupLabels.alertname }} ({{ .GroupLabels.namespace }})',
slackAlertText:: |||
{{ .CommonAnnotations.summary }}
{{ if .Alerts.Firing | len }}Firing alerts:
{{ range .Alerts.Firing }}- {{ .Annotations.message }}{{ .Annotations.description }}
{{ end }}{{ end }}{{ if .Alerts.Resolved | len }}Resolved alerts:
{{ range .Alerts.Resolved }}- {{ .Annotations.message }}{{ .Annotations.description }}
{{ end }}{{ end }}
|||,

build_slack_receiver(name, slack_channel)::
{
name: name,
slack_configs: [{
api_url: $._config.slack_url,
channel: slack_channel,
send_resolved: true,
title: $.slackAlertTitle,
text: $.slackAlertText,
actions: [
{
type: 'button',
text: 'Runbook :green_book:',
url: '{{ (index .Alerts 0).Annotations.runbook_url }}',
},
{
type: 'button',
text: 'Source :information_source:',
url: '{{ (index .Alerts 0).GeneratorURL }}',
},
{
type: 'button',
text: 'Silence :no_bell:',
url: '{{ template "__alert_silence_link" . }}',
},
],
}],
},

alertmanager_config:: {
templates: ['/etc/alertmanager/*.tmpl'],
templates: [
'/etc/alertmanager/*.tmpl',
'/etc/alertmanager/config/templates.tmpl',
],
route: {
group_by: ['alertname'],
receiver: 'slack',
},

receivers: [{
name: 'slack',
slack_configs: [{
api_url: $._config.slack_url,
channel: $._config.slack_channel,
}],
}],
receivers: [
$.build_slack_receiver('slack', $._config.slack_channel),
],
},

local configMap = $.core.v1.configMap,
Expand All @@ -40,6 +78,7 @@
configMap.new('alertmanager-config') +
configMap.withData({
'alertmanager.yml': $.util.manifestYaml($.alertmanager_config),
'templates.tmpl': (importstr 'files/alertmanager_config.tmpl'),
})
else {},

Expand Down
12 changes: 12 additions & 0 deletions prometheus-ksonnet/lib/files/alertmanager_config.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This builds the silence URL. We exclude the alertname in the range
# to avoid the issue of having trailing comma separator (%2C) at the end
# of the generated URL
{{ define "__alert_silence_link" -}}
{{ .ExternalURL }}/#/silences/new?filter=%7B
{{- range .CommonLabels.SortedPairs -}}
{{- if ne .Name "alertname" -}}
{{- .Name }}%3D"{{- .Value -}}"%2C%20
{{- end -}}
{{- end -}}
alertname%3D"{{ .CommonLabels.alertname }}"%7D
{{- end }}