Browse Source

alerting: support `for` on execution errors and notdata

bergquist 7 years ago
parent
commit
28029ce4a7
1 changed files with 19 additions and 13 deletions
  1. 19 13
      pkg/services/alerting/eval_context.go

+ 19 - 13
pkg/services/alerting/eval_context.go

@@ -118,7 +118,26 @@ func (c *EvalContext) GetRuleUrl() (string, error) {
 	return fmt.Sprintf(urlFormat, m.GetFullDashboardUrl(ref.Uid, ref.Slug), c.Rule.PanelId, c.Rule.OrgId), nil
 	return fmt.Sprintf(urlFormat, m.GetFullDashboardUrl(ref.Uid, ref.Slug), c.Rule.PanelId, c.Rule.OrgId), nil
 }
 }
 
 
+// GetNewState returns the new state from the alert rule evaluation
 func (c *EvalContext) GetNewState() m.AlertStateType {
 func (c *EvalContext) GetNewState() m.AlertStateType {
+	ns := getNewStateInternal(c)
+	if ns != m.AlertStateAlerting || c.Rule.For == 0 {
+		return ns
+	}
+
+	since := time.Now().Sub(c.Rule.LastStateChange)
+	if since > c.Rule.For {
+		return m.AlertStateAlerting
+	}
+
+	if c.PrevAlertState == m.AlertStateAlerting {
+		return m.AlertStateAlerting
+	}
+
+	return m.AlertStatePending
+}
+
+func getNewStateInternal(c *EvalContext) m.AlertStateType {
 	if c.Error != nil {
 	if c.Error != nil {
 		c.log.Error("Alert Rule Result Error",
 		c.log.Error("Alert Rule Result Error",
 			"ruleId", c.Rule.Id,
 			"ruleId", c.Rule.Id,
@@ -132,19 +151,6 @@ func (c *EvalContext) GetNewState() m.AlertStateType {
 		return c.Rule.ExecutionErrorState.ToAlertState()
 		return c.Rule.ExecutionErrorState.ToAlertState()
 	}
 	}
 
 
-	if c.Firing && c.Rule.For != 0 {
-		since := time.Now().Sub(c.Rule.LastStateChange)
-		if since > c.Rule.For {
-			return m.AlertStateAlerting
-		}
-
-		if c.PrevAlertState == m.AlertStateAlerting {
-			return m.AlertStateAlerting
-		}
-
-		return m.AlertStatePending
-	}
-
 	if c.Firing {
 	if c.Firing {
 		return m.AlertStateAlerting
 		return m.AlertStateAlerting
 	}
 	}