From 0d2a01f03ff04b16ef5ac109a05be18a431b1115 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Mon, 6 Mar 2023 19:33:30 +0200 Subject: [PATCH] [v9.4.x] Alerting: Fix Classic Conditions $values variable (#64249) Alerting: Fix Classic Conditions $values variable (#64243) This commit fixes a bug in the $values variable in notification templates when using Classic Conditions. Since Classic Conditions are not multi-dimensional, the values of each series that exceeded the condition should be available as a RefID and offset. For example, B0, B1, etc. However, this bug meant that instead just a single condition would be printed as B, not B0. (cherry picked from commit ed71012ced9198017b84c14f2a6de0e576b9f401) Co-authored-by: George Robinson --- pkg/services/ngalert/state/cache.go | 6 ++--- pkg/services/ngalert/state/cache_test.go | 30 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/pkg/services/ngalert/state/cache.go b/pkg/services/ngalert/state/cache.go index 97531420c58..b0f2925a51e 100644 --- a/pkg/services/ngalert/state/cache.go +++ b/pkg/services/ngalert/state/cache.go @@ -52,11 +52,11 @@ func (rs *ruleStates) getOrCreate(ctx context.Context, log log.Logger, alertRule ruleLabels, annotations := rs.expandRuleLabelsAndAnnotations(ctx, log, alertRule, result, extraLabels, externalURL) values := make(map[string]float64) - for _, v := range result.Values { + for refID, v := range result.Values { if v.Value != nil { - values[v.Var] = *v.Value + values[refID] = *v.Value } else { - values[v.Var] = math.NaN() + values[refID] = math.NaN() } } diff --git a/pkg/services/ngalert/state/cache_test.go b/pkg/services/ngalert/state/cache_test.go index 73691fd5a53..6050971007d 100644 --- a/pkg/services/ngalert/state/cache_test.go +++ b/pkg/services/ngalert/state/cache_test.go @@ -9,6 +9,7 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/data" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/xorcare/pointer" "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/services/ngalert/eval" @@ -108,6 +109,7 @@ func Test_getOrCreate(t *testing.T) { assert.Equal(t, expected, state.Labels["rule-"+key]) } }) + t.Run("rule annotations should be able to be expanded with result and extra labels", func(t *testing.T) { result := eval.Result{ Instance: models.GenerateAlertLabels(5, "result-"), @@ -134,6 +136,34 @@ func Test_getOrCreate(t *testing.T) { assert.Equal(t, expected, state.Annotations["rule-"+key]) } }) + + t.Run("expected Reduce and Math expression values", func(t *testing.T) { + result := eval.Result{ + Instance: models.GenerateAlertLabels(5, "result-"), + Values: map[string]eval.NumberValueCapture{ + "A": eval.NumberValueCapture{Var: "A", Value: pointer.Float64(1.0)}, + "B": eval.NumberValueCapture{Var: "B", Value: pointer.Float64(2.0)}, + }, + } + rule := generateRule() + + state := c.getOrCreate(context.Background(), l, rule, result, nil, url) + assert.Equal(t, map[string]float64{"A": 1, "B": 2}, state.Values) + }) + + t.Run("expected Classic Condition values", func(t *testing.T) { + result := eval.Result{ + Instance: models.GenerateAlertLabels(5, "result-"), + Values: map[string]eval.NumberValueCapture{ + "B0": eval.NumberValueCapture{Var: "B", Value: pointer.Float64(1.0)}, + "B1": eval.NumberValueCapture{Var: "B", Value: pointer.Float64(2.0)}, + }, + } + rule := generateRule() + + state := c.getOrCreate(context.Background(), l, rule, result, nil, url) + assert.Equal(t, map[string]float64{"B0": 1, "B1": 2}, state.Values) + }) } func Test_mergeLabels(t *testing.T) {