Alerting: Keep last state on no data (#6354)

* feat(alerting): add support to keep last state on no data

closes #6332

* refactoring(alerting PR #6354): added new option type for NoData option so AlertStateType does not have to contain invalid state, #6354
This commit is contained in:
Carl Bergquist
2016-10-22 10:56:04 +02:00
committed by Torkel Ödegaard
parent 4999fff29b
commit bc90e6ce46
5 changed files with 41 additions and 47 deletions
+12 -7
View File
@@ -30,34 +30,35 @@ func NewResultHandler() *DefaultResultHandler {
func (handler *DefaultResultHandler) Handle(evalContext *EvalContext) error {
oldState := evalContext.Rule.State
exeuctionError := ""
executionError := ""
annotationData := simplejson.New()
if evalContext.Error != nil {
handler.log.Error("Alert Rule Result Error", "ruleId", evalContext.Rule.Id, "error", evalContext.Error)
evalContext.Rule.State = m.AlertStateExecError
exeuctionError = evalContext.Error.Error()
annotationData.Set("errorMessage", exeuctionError)
executionError = evalContext.Error.Error()
annotationData.Set("errorMessage", executionError)
} else if evalContext.Firing {
evalContext.Rule.State = m.AlertStateAlerting
annotationData = simplejson.NewFromAny(evalContext.EvalMatches)
} else {
// handle no data case
if evalContext.NoDataFound {
evalContext.Rule.State = evalContext.Rule.NoDataState
if evalContext.Rule.NoDataState != m.NoDataKeepState {
evalContext.Rule.State = evalContext.Rule.NoDataState.ToAlertState()
}
} else {
evalContext.Rule.State = m.AlertStateOK
}
}
countStateResult(evalContext.Rule.State)
if evalContext.Rule.State != oldState {
if handler.shouldUpdateAlertState(evalContext, oldState) {
handler.log.Info("New state change", "alertId", evalContext.Rule.Id, "newState", evalContext.Rule.State, "oldState", oldState)
cmd := &m.SetAlertStateCommand{
AlertId: evalContext.Rule.Id,
OrgId: evalContext.Rule.OrgId,
State: evalContext.Rule.State,
Error: exeuctionError,
Error: executionError,
EvalData: annotationData,
}
@@ -91,6 +92,10 @@ func (handler *DefaultResultHandler) Handle(evalContext *EvalContext) error {
return nil
}
func (handler *DefaultResultHandler) shouldUpdateAlertState(evalContext *EvalContext, oldState m.AlertStateType) bool {
return evalContext.Rule.State != oldState
}
func countStateResult(state m.AlertStateType) {
switch state {
case m.AlertStateAlerting: