Skip to content

Commit

Permalink
Return early in Inhibitor.MutesAll when all alerts are muted
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuckal777 committed Aug 1, 2024
1 parent f960293 commit 4cdbffc
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion inhibit/inhibit.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,13 @@ func (ih *Inhibitor) Mutes(lset model.LabelSet) bool {
}

func (ih *Inhibitor) MutesAll(lsets ...model.LabelSet) []bool {
mutes := make([]bool, len(lsets))
// Cache fingerprints, so we don't calculate them in the quadratic loop.
fingerprints := make([]model.Fingerprint, len(lsets))
for i, lset := range lsets {
fingerprints[i] = lset.Fingerprint()
}
var muteCount int
mutes := make([]bool, len(lsets))
for _, r := range ih.rules {
// The scache evaluation does not depend on the the lsets, cache it.
var alerts []*types.Alert
Expand All @@ -173,8 +174,13 @@ func (ih *Inhibitor) MutesAll(lsets ...model.LabelSet) []bool {
if inhibitedByFP, eq := r.hasEqualCached(lset, r.SourceMatchers.Matches(lset), alerts, scacheEval); eq {
ih.marker.SetInhibited(fingerprints[i], inhibitedByFP.String())
mutes[i] = true
muteCount++
}
}
// When all alerts are muted, there's no need to evaluate the rest of the rules.
if muteCount == len(lsets) {
return mutes
}
}
// So far we have only set the inhibited state for the alerts that are muted.
// Set the uninhibited state for the alerts that are not muted.
Expand Down

0 comments on commit 4cdbffc

Please sign in to comment.