Sort dispatched alerts by job+instance then rest by default (#1178) (#1234)

This commit is contained in:
Ted Zlatanov 2018-03-22 15:06:37 -04:00 committed by stuart nelson
parent 1531aa66f3
commit 099b6a1d43
1 changed files with 20 additions and 0 deletions

View File

@ -424,6 +424,26 @@ func (ag *aggrGroup) flush(notify func(...*types.Alert) bool) {
alertsSlice = append(alertsSlice, alert) alertsSlice = append(alertsSlice, alert)
} }
sort.SliceStable(alertsSlice, func(i, j int) bool {
// Look at labels.job, then labels.instance.
for _, override_key := range [...]model.LabelName{"job", "instance"} {
key_i, ok_i := alertsSlice[i].Labels[override_key]
if !ok_i {
return true
}
key_j, ok_j := alertsSlice[j].Labels[override_key]
if !ok_j {
return false
}
if key_i != key_j {
return key_i > key_j
}
}
return alertsSlice[i].Labels.Before(alertsSlice[j].Labels)
})
ag.mtx.Unlock() ag.mtx.Unlock()
level.Debug(ag.logger).Log("msg", "Flushing", "alerts", fmt.Sprintf("%v", alertsSlice)) level.Debug(ag.logger).Log("msg", "Flushing", "alerts", fmt.Sprintf("%v", alertsSlice))