From eddcdd8d660ac3e9fad7f97259e182b9afc73a35 Mon Sep 17 00:00:00 2001 From: George Robinson Date: Thu, 11 Nov 2021 16:01:23 +0000 Subject: [PATCH] Alerting: Fix NoDataFound for alert rules using the AND operator (#41305) (#41524) 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. (cherry picked from commit d6ed5d295eff31799524a21bb6042654a84d3d23) --- pkg/services/alerting/eval_handler.go | 7 +++++-- pkg/services/alerting/eval_handler_test.go | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/pkg/services/alerting/eval_handler.go b/pkg/services/alerting/eval_handler.go index ed77877e34d..fd6d2687a33 100644 --- a/pkg/services/alerting/eval_handler.go +++ b/pkg/services/alerting/eval_handler.go @@ -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 { diff --git a/pkg/services/alerting/eval_handler_test.go b/pkg/services/alerting/eval_handler_test.go index 271d51c385d..12ac6c84a20 100644 --- a/pkg/services/alerting/eval_handler_test.go +++ b/pkg/services/alerting/eval_handler_test.go @@ -182,7 +182,7 @@ func TestAlertingEvaluationHandler(t *testing.T) { So(context.NoDataFound, ShouldBeTrue) }) - Convey("Should not return no data if at least one condition has no data and using AND", func() { + Convey("Should return no data if at least one condition has no data and using AND", func() { context := NewEvalContext(context.TODO(), &Rule{ Conditions: []Condition{ &conditionStub{operator: "and", noData: true}, @@ -191,7 +191,7 @@ func TestAlertingEvaluationHandler(t *testing.T) { }, &validations.OSSPluginRequestValidator{}) handler.Eval(context) - So(context.NoDataFound, ShouldBeFalse) + So(context.NoDataFound, ShouldBeTrue) }) Convey("Should return no data if at least one condition has no data and using OR", func() {