diff --git a/prometheus-ksonnet/lib/alertmanager.libsonnet b/prometheus-ksonnet/lib/alertmanager.libsonnet index 07aeb4f8f..7cae51071 100644 --- a/prometheus-ksonnet/lib/alertmanager.libsonnet +++ b/prometheus-ksonnet/lib/alertmanager.libsonnet @@ -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, @@ -40,6 +68,7 @@ configMap.new('alertmanager-config') + configMap.withData({ 'alertmanager.yml': $.util.manifestYaml($.alertmanager_config), + 'templates.tmpl': (importstr 'files/alertmanager_config.tmpl'), }) else {}, diff --git a/prometheus-ksonnet/lib/files/alertmanager_config.tmpl b/prometheus-ksonnet/lib/files/alertmanager_config.tmpl new file mode 100644 index 000000000..a44867d36 --- /dev/null +++ b/prometheus-ksonnet/lib/files/alertmanager_config.tmpl @@ -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 }}