Generate AlertGroup unique fingerprint on the fly

This commit is contained in:
Łukasz Mierzwa
2017-06-25 11:21:55 -07:00
parent b0d6628f82
commit 2647330f71
2 changed files with 12 additions and 7 deletions

View File

@@ -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,

View File

@@ -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()