Implement merging of alerts on insert

This commit is contained in:
Fabian Reinartz 2015-10-02 16:52:04 +02:00
parent 0e742ecf33
commit 856d5091c1
2 changed files with 27 additions and 20 deletions

View File

@ -165,21 +165,20 @@ func (a *MemAlerts) Put(alerts ...*types.Alert) error {
defer a.data.mtx.Unlock()
for _, alert := range alerts {
a.data.alerts[alert.Fingerprint()] = alert
fp := alert.Fingerprint()
// Merge the alert with the existant one.
if old, ok := a.data.alerts[fp]; ok {
alert = old.Merge(alert)
}
a.data.alerts[fp] = alert
for _, ch := range a.listeners {
ch <- alert
}
}
ch := make(chan *types.Alert)
go func() {
for _, a := range alerts {
ch <- a
}
}()
return nil
}

View File

@ -47,19 +47,27 @@ func Alerts(alerts ...*Alert) model.Alerts {
// Merges the timespan of two alerts based and overwrites annotations
// based on the authoritative timestamp.
// A new alert is returned, the labels are assumed to be equal.
// func (a *Alert) Merge(o *Alert) *Alert {
// // Let o always be the younger alert.
// if a.Timestamp.Before(a.Timestamp) {
// return o.Merge(a)
// }
func (a *Alert) Merge(o *Alert) *Alert {
// Let o always be the younger alert.
if !a.UpdatedAt.Before(o.UpdatedAt) {
return o.Merge(a)
}
// res := &Alert{
// Labels: o.Labels,
// Annotiations: o.Annotations,
// Timestamp: o.Timestamp,
// }
res := *o
// }
// Always pick the earliest starting time.
if a.StartsAt.Before(o.StartsAt) {
res.StartsAt = a.StartsAt
}
// An non-timeout resolved timestamp always rules.
// The latest explicit resolved timestamp wins.
if a.EndsAt.After(o.EndsAt) && !a.Timeout {
res.EndsAt = a.EndsAt
}
return &res
}
// A Silencer determines whether a given label set is muted.
type Muter interface {