mirror of
https://github.com/prymitive/karma
synced 2026-05-21 04:33:07 +00:00
Generate AlertGroup unique fingerprint on the fly
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user