From 099b6a1d43e9c518ba698b5c22eb280a1426b879 Mon Sep 17 00:00:00 2001 From: Ted Zlatanov Date: Thu, 22 Mar 2018 15:06:37 -0400 Subject: [PATCH] Sort dispatched alerts by job+instance then rest by default (#1178) (#1234) --- dispatch/dispatch.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/dispatch/dispatch.go b/dispatch/dispatch.go index e66c4fbc..3926146d 100644 --- a/dispatch/dispatch.go +++ b/dispatch/dispatch.go @@ -424,6 +424,26 @@ func (ag *aggrGroup) flush(notify func(...*types.Alert) bool) { 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() level.Debug(ag.logger).Log("msg", "Flushing", "alerts", fmt.Sprintf("%v", alertsSlice))