From a4337345b3ee52ef180171df1bb7c57970ee68aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Mon, 8 May 2017 15:33:17 +0100 Subject: [PATCH] Deduplicate alerts Alertmanager 0.6.1 can return multiple instances of the same alert either in a different block object or as a duplicated alert group, ensure that we deduplicate those --- timer.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/timer.go b/timer.go index eb9506cfe..7951ece1d 100644 --- a/timer.go +++ b/timer.go @@ -64,6 +64,8 @@ func PullFromAlertmanager() { metricAlerts.With(prometheus.Labels{"status": state}).Set(0) } + uniqueAlerts := map[string]bool{} + for _, ag := range alertGroups { // used to generate group content hash agHasher := sha1.New() @@ -76,14 +78,23 @@ func PullFromAlertmanager() { } for _, alert := range ag.Alerts { - // skip duplicated alerts + // generate alert fingerprint from a raw, unaltered alert object + alert.Fingerprint = fmt.Sprintf("%x", structhash.Sha1(alert, 1)) + + // skip global duplicated alerts (shared between multiple groups) + if _, found := uniqueAlerts[alert.Fingerprint]; found { + continue + } + // skip group duplicated alerts (shared between multiple blocks) if _, found := alerts[alert.Fingerprint]; found { continue } + // mark this alert as seen + uniqueAlerts[alert.Fingerprint] = true + alert.Annotations, alert.Links = transform.DetectLinks(alert.Annotations) alert.Labels = transform.StripLables(ignoredLabels, alert.Labels) - alert.Fingerprint = fmt.Sprintf("%x", structhash.Sha1(alert, 1)) alerts[alert.Fingerprint] = alert