From 2647330f71713e3a14bc7b3a9dd64bd122fea7a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Sun, 25 Jun 2017 11:21:55 -0700 Subject: [PATCH] Generate AlertGroup unique fingerprint on the fly --- alertmanager/models.go | 8 +------- models/alertgroup.go | 11 +++++++++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/alertmanager/models.go b/alertmanager/models.go index 517a28e86..7788e360e 100644 --- a/alertmanager/models.go +++ b/alertmanager/models.go @@ -1,9 +1,7 @@ package alertmanager import ( - "crypto/sha1" "fmt" - "io" "sort" "sync" "time" @@ -13,7 +11,6 @@ import ( "github.com/cloudflare/unsee/models" "github.com/cloudflare/unsee/transform" "github.com/cloudflare/unsee/transport" - "github.com/cnf/structhash" "github.com/prometheus/client_golang/prometheus" log "github.com/Sirupsen/logrus" @@ -107,10 +104,7 @@ func (am *Alertmanager) pullAlerts(version string) error { uniqueGroups := map[string]models.AlertGroup{} uniqueAlerts := map[string]map[string]models.Alert{} for _, ag := range groups { - agIDHasher := sha1.New() - io.WriteString(agIDHasher, ag.Receiver) - io.WriteString(agIDHasher, fmt.Sprintf("%x", structhash.Sha1(ag.Labels, 1))) - agID := fmt.Sprintf("%x", agIDHasher.Sum(nil)) + agID := ag.LabelsFingerprint() if _, found := uniqueGroups[agID]; !found { uniqueGroups[agID] = models.AlertGroup{ Receiver: ag.Receiver, diff --git a/models/alertgroup.go b/models/alertgroup.go index 114cdfd9f..b6bfc2e92 100644 --- a/models/alertgroup.go +++ b/models/alertgroup.go @@ -4,6 +4,8 @@ import ( "crypto/sha1" "fmt" "io" + + "github.com/cnf/structhash" ) // AlertList is flat list of UnseeAlert objects @@ -39,6 +41,15 @@ type AlertGroup struct { StateCount map[string]int `json:"stateCount"` } +// LabelsFingerprint is a checksum of this AlertGroup labels and the receiver +// it should be unique for each AlertGroup +func (ag AlertGroup) LabelsFingerprint() string { + agIDHasher := sha1.New() + io.WriteString(agIDHasher, ag.Receiver) + io.WriteString(agIDHasher, fmt.Sprintf("%x", structhash.Sha1(ag.Labels, 1))) + return fmt.Sprintf("%x", agIDHasher.Sum(nil)) +} + // ContentFingerprint is a checksum of all alerts in the group func (ag AlertGroup) ContentFingerprint() string { h := sha1.New()