Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
45 changes: 37 additions & 8 deletions prometheus-ksonnet/lib/alertmanager.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,48 @@
]
else [],

build_slack_receiver(name, slack_channel)::
{
name: name,
slack_configs: [{
api_url: $._config.slack_url,
channel: slack_channel,
send_resolved: true,
title: '{{ template "__alert_title" . }}',
text: '{{ template "__alert_text" . }}',
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 +68,7 @@
configMap.new('alertmanager-config') +
configMap.withData({
'alertmanager.yml': $.util.manifestYaml($.alertmanager_config),
'templates.tmpl': (importstr 'files/alertmanager_config.tmpl'),
})
else {},

Expand Down
25 changes: 25 additions & 0 deletions prometheus-ksonnet/lib/files/alertmanager_config.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{{ define "__alert_title" -}}
[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .GroupLabels.cluster }}: {{ .GroupLabels.alertname }} ({{ .GroupLabels.namespace }})
{{- end }}

{{ define "__alert_text" -}}
{{ .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 }}
{{- end }}

# 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. Inspired from https://gist.github.com/milesbxf/e2744fc90e9c41b47aa47925f8ff6512
{{ 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 }}