Alerting: Fix bug where patching recording rule queries wouldn't apply (#91011)

* the fix

* tests
This commit is contained in:
Alexander Weaver 2024-07-26 11:02:54 -05:00 committed by GitHub
parent d7e85354d1
commit b7220b532e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 1 deletions

View File

@ -768,7 +768,7 @@ func PatchPartialAlertRule(existingRule *AlertRule, ruleToPatch *AlertRuleWithOp
if ruleToPatch.Title == "" {
ruleToPatch.Title = existingRule.Title
}
if ruleToPatch.Condition == "" || len(ruleToPatch.Data) == 0 {
if !hasAnyCondition(ruleToPatch) || len(ruleToPatch.Data) == 0 {
ruleToPatch.Condition = existingRule.Condition
ruleToPatch.Data = existingRule.Data
}
@ -873,3 +873,7 @@ func (r *Record) Fingerprint() data.Fingerprint {
writeString(r.From)
return data.Fingerprint(h.Sum64())
}
func hasAnyCondition(rule *AlertRuleWithOptionals) bool {
return rule.Condition != "" || (rule.Record != nil && rule.Record.From != "")
}

View File

@ -555,6 +555,47 @@ func TestCalculateRuleUpdate(t *testing.T) {
require.Contains(t, delta.AffectedGroups, targetGroupKey)
assert.Equal(t, models.RulesGroup(targetGroup), delta.AffectedGroups[targetGroupKey])
})
t.Run("when an alert rule query is updated", func(t *testing.T) {
cp := models.CopyRule(rule)
cp.Record = nil
cp.Data = []models.AlertQuery{{RefID: "something else"}}
delta, err := CalculateRuleUpdate(context.Background(), fakeStore, &models.AlertRuleWithOptionals{
AlertRule: *cp,
HasPause: false,
})
require.NoError(t, err)
assert.Equal(t, rule.GetGroupKey(), delta.GroupKey)
assert.Empty(t, delta.New)
assert.Empty(t, delta.Delete)
assert.Len(t, delta.Update, 1)
assert.Equal(t, cp, delta.Update[0].New)
assert.Equal(t, rule, delta.Update[0].Existing)
require.Contains(t, delta.AffectedGroups, delta.GroupKey)
})
t.Run("when a recording rule query is updated", func(t *testing.T) {
base := gen.With(models.RuleGen.WithAllRecordingRules()).GenerateRef()
fakeStore.Rules[base.OrgID] = []*models.AlertRule{base}
cp := models.CopyRule(base)
cp.Data = []models.AlertQuery{{RefID: "something else"}}
delta, err := CalculateRuleUpdate(context.Background(), fakeStore, &models.AlertRuleWithOptionals{
AlertRule: *cp,
HasPause: false,
})
require.NoError(t, err)
assert.Equal(t, base.GetGroupKey(), delta.GroupKey)
assert.Empty(t, delta.New)
assert.Empty(t, delta.Delete)
assert.Len(t, delta.Update, 1)
assert.Equal(t, cp, delta.Update[0].New)
assert.Equal(t, base, delta.Update[0].Existing)
require.Contains(t, delta.AffectedGroups, delta.GroupKey)
})
}
func TestCalculateRuleCreate(t *testing.T) {