From 4a9d4de80cb800efa35d16439fdc7589e3e25157 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Thu, 19 Nov 2015 14:45:49 +0100 Subject: [PATCH 1/2] Improve default subject template --- template/default.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/template/default.tmpl b/template/default.tmpl index 323d8618..5f5925d9 100644 --- a/template/default.tmpl +++ b/template/default.tmpl @@ -1,4 +1,4 @@ -{{ define "__subject" }}{{$dot := .}}[{{ .Status }}] {{ range $k := .GroupLabelnames }}{{ index $dot.GroupLabels $k }} {{ end }}{{if gt (len .AlertCommonLabels) (len .GroupLabels) }}({{ range $k := .AlertCommonLabelnames }}{{ if eq "" (index $dot.GroupLabels $k) }}{{ index $dot.AlertCommonLabels $k }}{{ end }}){{ end }}{{ end }}{{ end }} +{{ define "__subject" }}{{$dot := .}}[{{ .Status }}:{{ .Alerts | len }}] {{ range $k := .GroupLabelnames }}{{ index $dot.GroupLabels $k }} {{ end }}{{if gt (len .AlertCommonLabels) (len .GroupLabels) }}({{ range $k := .AlertCommonLabelnames }}{{ if eq "" (index $dot.GroupLabels $k) }}{{ index $dot.AlertCommonLabels $k }}{{ end }}{{ end }}){{ end }}{{ end }} {{ define "slack.default.fallback" }} {{ template "__subject" . }} From 1951593cc95950cbcfaf9c59cd29b9427f2f0c01 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Thu, 19 Nov 2015 15:49:37 +0100 Subject: [PATCH 2/2] Always have alertname in first position --- notify/impl.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/notify/impl.go b/notify/impl.go index 1abec92f..dd7fd405 100644 --- a/notify/impl.go +++ b/notify/impl.go @@ -120,11 +120,19 @@ func generateTemplateData(ctx context.Context, as ...*types.Alert) *TemplateData data.Alerts = append(data.Alerts, alert) } + sortStart := 0 for k, v := range groupLabels { data.GroupLabels[string(k)] = string(v) - data.GroupLabelnames = append(data.GroupLabelnames, string(k)) + + // Always have the alertname label at the first position. + if k == model.AlertNameLabel { + data.GroupLabelnames = append([]string{string(k)}, data.GroupLabelnames...) + sortStart = 1 + } else { + data.GroupLabelnames = append(data.GroupLabelnames, string(k)) + } } - sort.Strings(data.GroupLabelnames) + sort.Strings(data.GroupLabelnames[sortStart:]) if len(alerts) >= 1 { common := alerts[0].Labels.Clone()