set state correctly and test (#34680)

This commit is contained in:
David Parrott 2021-05-26 11:37:42 -07:00 committed by GitHub
parent 3aba08f708
commit 20d356947c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 11 deletions

View File

@ -68,7 +68,6 @@ func (c *cache) getOrCreate(alertRule *ngModels.AlertRule, result eval.Result) *
OrgID: alertRule.OrgID,
CacheId: id,
Labels: lbs,
State: result.State,
Annotations: annotations,
EvaluationDuration: result.EvaluationDuration,
}

View File

@ -83,7 +83,7 @@ func (st *Manager) setNextState(alertRule *ngModels.AlertRule, result eval.Resul
st.Log.Debug("setting alert state", "uid", alertRule.UID)
switch result.State {
case eval.Normal:
currentState = resultNormal(currentState, result)
currentState = currentState.resultNormal(result)
case eval.Alerting:
currentState = currentState.resultAlerting(alertRule, result)
case eval.Error:

View File

@ -346,7 +346,7 @@ func TestProcessEvalResults(t *testing.T) {
},
},
{
desc: "normal -> pending when For is set but not exceeded",
desc: "normal -> pending when For is set but not exceeded and first result is normal",
alertRule: &models.AlertRule{
OrgID: 1,
Title: "test_title",
@ -406,6 +406,67 @@ func TestProcessEvalResults(t *testing.T) {
},
},
},
{
desc: "normal -> pending when For is set but not exceeded and first result is alerting",
alertRule: &models.AlertRule{
OrgID: 1,
Title: "test_title",
UID: "test_alert_rule_uid_2",
NamespaceUID: "test_namespace_uid",
Annotations: map[string]string{"annotation": "test"},
Labels: map[string]string{"label": "test"},
IntervalSeconds: 10,
For: 1 * time.Minute,
},
evalResults: []eval.Results{
{
eval.Result{
Instance: data.Labels{"instance_label": "test"},
State: eval.Alerting,
EvaluatedAt: evaluationTime,
EvaluationDuration: evaluationDuration,
},
},
{
eval.Result{
Instance: data.Labels{"instance_label": "test"},
State: eval.Alerting,
EvaluatedAt: evaluationTime.Add(10 * time.Second),
EvaluationDuration: evaluationDuration,
},
},
},
expectedStates: map[string]*state.State{
`[["__alert_rule_namespace_uid__","test_namespace_uid"],["__alert_rule_uid__","test_alert_rule_uid_2"],["alertname","test_title"],["instance_label","test"],["label","test"]]`: {
AlertRuleUID: "test_alert_rule_uid_2",
OrgID: 1,
CacheId: `[["__alert_rule_namespace_uid__","test_namespace_uid"],["__alert_rule_uid__","test_alert_rule_uid_2"],["alertname","test_title"],["instance_label","test"],["label","test"]]`,
Labels: data.Labels{
"__alert_rule_namespace_uid__": "test_namespace_uid",
"__alert_rule_uid__": "test_alert_rule_uid_2",
"alertname": "test_title",
"label": "test",
"instance_label": "test",
},
State: eval.Pending,
Results: []state.Evaluation{
{
EvaluationTime: evaluationTime,
EvaluationState: eval.Alerting,
},
{
EvaluationTime: evaluationTime.Add(10 * time.Second),
EvaluationState: eval.Alerting,
},
},
StartsAt: evaluationTime,
EndsAt: evaluationTime.Add(1 * time.Minute),
LastEvaluationTime: evaluationTime.Add(10 * time.Second),
EvaluationDuration: evaluationDuration,
Annotations: map[string]string{"annotation": "test"},
},
},
},
{
desc: "normal -> alerting when result is NoData and NoDataState is alerting",
alertRule: &models.AlertRule{

View File

@ -30,15 +30,14 @@ type Evaluation struct {
EvaluationString string
}
func resultNormal(alertState *State, result eval.Result) *State {
newState := alertState
if alertState.State != eval.Normal {
newState.EndsAt = result.EvaluatedAt
newState.StartsAt = result.EvaluatedAt
func (a *State) resultNormal(result eval.Result) *State {
if a.State != eval.Normal {
a.EndsAt = result.EvaluatedAt
a.StartsAt = result.EvaluatedAt
}
newState.Error = result.Error // should be nil since state is not error
newState.State = eval.Normal
return newState
a.Error = result.Error // should be nil since state is not error
a.State = eval.Normal
return a
}
func (a *State) resultAlerting(alertRule *ngModels.AlertRule, result eval.Result) *State {