mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
set state correctly and test (#34680)
This commit is contained in:
@@ -68,7 +68,6 @@ func (c *cache) getOrCreate(alertRule *ngModels.AlertRule, result eval.Result) *
|
|||||||
OrgID: alertRule.OrgID,
|
OrgID: alertRule.OrgID,
|
||||||
CacheId: id,
|
CacheId: id,
|
||||||
Labels: lbs,
|
Labels: lbs,
|
||||||
State: result.State,
|
|
||||||
Annotations: annotations,
|
Annotations: annotations,
|
||||||
EvaluationDuration: result.EvaluationDuration,
|
EvaluationDuration: result.EvaluationDuration,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ func (st *Manager) setNextState(alertRule *ngModels.AlertRule, result eval.Resul
|
|||||||
st.Log.Debug("setting alert state", "uid", alertRule.UID)
|
st.Log.Debug("setting alert state", "uid", alertRule.UID)
|
||||||
switch result.State {
|
switch result.State {
|
||||||
case eval.Normal:
|
case eval.Normal:
|
||||||
currentState = resultNormal(currentState, result)
|
currentState = currentState.resultNormal(result)
|
||||||
case eval.Alerting:
|
case eval.Alerting:
|
||||||
currentState = currentState.resultAlerting(alertRule, result)
|
currentState = currentState.resultAlerting(alertRule, result)
|
||||||
case eval.Error:
|
case eval.Error:
|
||||||
|
|||||||
@@ -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{
|
alertRule: &models.AlertRule{
|
||||||
OrgID: 1,
|
OrgID: 1,
|
||||||
Title: "test_title",
|
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",
|
desc: "normal -> alerting when result is NoData and NoDataState is alerting",
|
||||||
alertRule: &models.AlertRule{
|
alertRule: &models.AlertRule{
|
||||||
|
|||||||
@@ -30,15 +30,14 @@ type Evaluation struct {
|
|||||||
EvaluationString string
|
EvaluationString string
|
||||||
}
|
}
|
||||||
|
|
||||||
func resultNormal(alertState *State, result eval.Result) *State {
|
func (a *State) resultNormal(result eval.Result) *State {
|
||||||
newState := alertState
|
if a.State != eval.Normal {
|
||||||
if alertState.State != eval.Normal {
|
a.EndsAt = result.EvaluatedAt
|
||||||
newState.EndsAt = result.EvaluatedAt
|
a.StartsAt = result.EvaluatedAt
|
||||||
newState.StartsAt = result.EvaluatedAt
|
|
||||||
}
|
}
|
||||||
newState.Error = result.Error // should be nil since state is not error
|
a.Error = result.Error // should be nil since state is not error
|
||||||
newState.State = eval.Normal
|
a.State = eval.Normal
|
||||||
return newState
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *State) resultAlerting(alertRule *ngModels.AlertRule, result eval.Result) *State {
|
func (a *State) resultAlerting(alertRule *ngModels.AlertRule, result eval.Result) *State {
|
||||||
|
|||||||
Reference in New Issue
Block a user