Generate alert unique fingerprint on the fly

This commit is contained in:
Łukasz Mierzwa
2017-06-25 11:07:28 -07:00
parent 97e3728dab
commit b0d6628f82
4 changed files with 14 additions and 12 deletions

View File

@@ -27,14 +27,15 @@ func DedupAlerts() []models.AlertGroup {
alerts := map[string]models.Alert{}
for _, ag := range agList {
for _, alert := range ag.Alerts {
a, found := alerts[alert.ID]
alertLFP := alert.LabelsFingerprint()
a, found := alerts[alertLFP]
if found {
for _, am := range alert.Alertmanager {
a.Alertmanager = append(a.Alertmanager, am)
alerts[alert.ID] = a
alerts[alertLFP] = a
}
} else {
alerts[alert.ID] = alert
alerts[alertLFP] = alert
}
}
}

View File

@@ -119,16 +119,12 @@ func (am *Alertmanager) pullAlerts(version string) error {
}
}
for _, alert := range ag.Alerts {
// generate alert id from labels
aID := fmt.Sprintf("%x", structhash.Sha1(alert.Labels, 1))
alert.ID = aID
// generate alert fingerprint from a raw, unaltered alert object
fp := fmt.Sprintf("%x", structhash.Sha1(alert, 1))
if _, found := uniqueAlerts[agID]; !found {
uniqueAlerts[agID] = map[string]models.Alert{}
}
if _, found := uniqueAlerts[agID][fp]; !found {
uniqueAlerts[agID][fp] = alert
alertCFP := alert.ContentFingerprint()
if _, found := uniqueAlerts[agID][alertCFP]; !found {
uniqueAlerts[agID][alertCFP] = alert
}
}

View File

@@ -42,7 +42,12 @@ type Alert struct {
Alertmanager []AlertmanagerUpstream `json:"alertmanager"`
Receiver string `json:"receiver"`
Links map[string]string `json:"links"`
ID string `json:"-"`
}
// LabelsFingerprint is a checksum computed only from labels which should be
// unique for every alert
func (a Alert) LabelsFingerprint() string {
return fmt.Sprintf("%x", structhash.Sha1(a.Labels, 1))
}
// ContentFingerprint is a checksum computed from entire alert object

View File

@@ -114,7 +114,7 @@ var agFPTests = []agFPTest{
},
StateCount: map[string]int{"default": 0},
},
fingerprint: "c4a76e2d59f7ef3d49b20da06c4a89f63fdf9e3f",
fingerprint: "954a7283c2cda4179b63499465d82a313d21180f",
},
}