Alerting: Fix NoDataFound for alert rules using the AND operator (#41305)

This commit fixes an issue in alerting where NoDataFound is false
when using the AND operator to compare two conditions in an alert
rule and one of the conditions has no data.
This commit is contained in:
George Robinson 2021-11-10 10:28:49 +00:00 committed by GitHub
parent 7d2e9aa979
commit d6ed5d295e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 4 deletions

View File

@ -52,12 +52,15 @@ func (e *DefaultEvalHandler) Eval(context *EvalContext) {
// calculating Firing based on operator
if cr.Operator == "or" {
firing = firing || cr.Firing
noDataFound = noDataFound || cr.NoDataFound
} else {
firing = firing && cr.Firing
noDataFound = noDataFound && cr.NoDataFound
}
// We cannot evaluate the expression when one or more conditions are missing data
// and so noDataFound should be true if at least one condition returns no data,
// irrespective of the operator.
noDataFound = noDataFound || cr.NoDataFound
if i > 0 {
conditionEvals = "[" + conditionEvals + " " + strings.ToUpper(cr.Operator) + " " + strconv.FormatBool(cr.Firing) + "]"
} else {

View File

@ -181,7 +181,7 @@ func TestAlertingEvaluationHandler(t *testing.T) {
require.True(t, context.NoDataFound)
})
t.Run("Should not return no data if at least one condition has no data and using AND", func(t *testing.T) {
t.Run("Should return no data if at least one condition has no data and using AND", func(t *testing.T) {
context := NewEvalContext(context.TODO(), &Rule{
Conditions: []Condition{
&conditionStub{operator: "and", noData: true},
@ -190,7 +190,7 @@ func TestAlertingEvaluationHandler(t *testing.T) {
}, &validations.OSSPluginRequestValidator{})
handler.Eval(context)
require.False(t, context.NoDataFound)
require.True(t, context.NoDataFound)
})
t.Run("Should return no data if at least one condition has no data and using OR", func(t *testing.T) {