Alerting: Fix state reason (#101530)

---------

Signed-off-by: Yuri Tseretyan <yuriy.tseretyan@grafana.com>
This commit is contained in:
Yuri Tseretyan
2025-03-04 17:05:41 +02:00
committed by GitHub
parent 2f3aff0e04
commit 67b44ad22a
2 changed files with 60 additions and 2 deletions
+2 -2
View File
@@ -832,9 +832,9 @@ func (a *State) transition(alertRule *models.AlertRule, result eval.Result, extr
}
func resultStateReason(result eval.Result, rule *models.AlertRule) string {
if rule.ExecErrState == models.KeepLastErrState || rule.NoDataState == models.KeepLast {
if result.State == eval.Error && rule.ExecErrState == models.KeepLastErrState ||
result.State == eval.NoData && rule.NoDataState == models.KeepLast {
return models.ConcatReasons(result.State.String(), models.StateReasonKeepLast)
}
return result.State.String()
}
+58
View File
@@ -1132,3 +1132,61 @@ func TestPatch(t *testing.T) {
assert.EqualValues(t, orig.Annotations, state.Annotations)
})
}
func TestResultStateReason(t *testing.T) {
gen := ngmodels.RuleGen
tests := []struct {
name string
result eval.Result
rule *ngmodels.AlertRule
expected string
}{
{
name: "Error state with KeepLast",
result: eval.Result{
State: eval.Error,
},
rule: gen.With(ngmodels.RuleMuts.WithErrorExecAs(ngmodels.KeepLastErrState)).GenerateRef(),
expected: "Error, KeepLast",
},
{
name: "Error state without KeepLast",
result: eval.Result{
State: eval.Error,
},
rule: gen.With(ngmodels.RuleMuts.WithErrorExecAs(ngmodels.ErrorErrState)).GenerateRef(),
expected: "Error",
},
{
name: "NoData state with KeepLast state",
result: eval.Result{
State: eval.NoData,
},
rule: gen.With(ngmodels.RuleMuts.WithNoDataExecAs(ngmodels.KeepLast)).GenerateRef(),
expected: "NoData, KeepLast",
},
{
name: "NoData state without KeepLast",
result: eval.Result{
State: eval.NoData,
},
rule: gen.With(ngmodels.RuleMuts.WithNoDataExecAs(ngmodels.NoData)).GenerateRef(),
expected: "NoData",
},
{
name: "Normal state",
result: eval.Result{
State: eval.NoData,
},
rule: gen.With(ngmodels.RuleMuts.WithErrorExecAs(ngmodels.ErrorErrState), ngmodels.RuleMuts.WithNoDataExecAs(ngmodels.NoData)).GenerateRef(),
expected: "NoData",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := resultStateReason(tc.result, tc.rule)
assert.Equal(t, tc.expected, result)
})
}
}