2016-06-23 05:57:10 -05:00
|
|
|
package alerting
|
|
|
|
|
2016-07-21 10:31:46 -05:00
|
|
|
import (
|
2016-08-01 03:07:00 -05:00
|
|
|
"time"
|
|
|
|
|
2016-07-21 10:31:46 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2016-08-30 06:22:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2019-02-23 16:35:26 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/metrics"
|
2019-05-14 01:15:05 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
|
2016-08-01 03:07:00 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/annotations"
|
2018-05-24 08:26:27 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/rendering"
|
2016-07-21 10:31:46 -05:00
|
|
|
)
|
2016-06-23 05:57:10 -05:00
|
|
|
|
2019-05-20 05:13:32 -05:00
|
|
|
type resultHandler interface {
|
|
|
|
handle(evalContext *EvalContext) error
|
2016-06-23 05:57:10 -05:00
|
|
|
}
|
|
|
|
|
2019-05-20 05:13:32 -05:00
|
|
|
type defaultResultHandler struct {
|
|
|
|
notifier *notificationService
|
2016-06-23 05:57:10 -05:00
|
|
|
log log.Logger
|
|
|
|
}
|
|
|
|
|
2019-05-20 05:13:32 -05:00
|
|
|
func newResultHandler(renderService rendering.Service) *defaultResultHandler {
|
|
|
|
return &defaultResultHandler{
|
2016-07-26 05:29:52 -05:00
|
|
|
log: log.New("alerting.resultHandler"),
|
2019-05-20 05:13:32 -05:00
|
|
|
notifier: newNotificationService(renderService),
|
2016-06-23 05:57:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-20 05:13:32 -05:00
|
|
|
func (handler *defaultResultHandler) handle(evalContext *EvalContext) error {
|
2016-10-22 03:56:04 -05:00
|
|
|
executionError := ""
|
2016-08-31 07:06:54 -05:00
|
|
|
annotationData := simplejson.New()
|
2016-11-07 05:42:39 -06:00
|
|
|
|
2017-05-15 09:17:05 -05:00
|
|
|
if len(evalContext.EvalMatches) > 0 {
|
|
|
|
annotationData.Set("evalMatches", simplejson.NewFromAny(evalContext.EvalMatches))
|
2017-01-16 11:01:08 -06:00
|
|
|
}
|
|
|
|
|
2016-10-03 02:38:03 -05:00
|
|
|
if evalContext.Error != nil {
|
2016-10-22 03:56:04 -05:00
|
|
|
executionError = evalContext.Error.Error()
|
2017-05-15 09:17:05 -05:00
|
|
|
annotationData.Set("error", executionError)
|
|
|
|
} else if evalContext.NoDataFound {
|
|
|
|
annotationData.Set("noData", true)
|
2016-07-21 10:31:46 -05:00
|
|
|
}
|
|
|
|
|
2017-09-06 04:23:52 -05:00
|
|
|
metrics.M_Alerting_Result_State.WithLabelValues(string(evalContext.Rule.State)).Inc()
|
2019-05-20 05:13:32 -05:00
|
|
|
if evalContext.shouldUpdateAlertState() {
|
2019-06-03 03:25:58 -05:00
|
|
|
handler.log.Info("New state change", "alertId", evalContext.Rule.ID, "newState", evalContext.Rule.State, "prev state", evalContext.PrevAlertState)
|
2016-07-21 10:31:46 -05:00
|
|
|
|
2019-05-14 01:15:05 -05:00
|
|
|
cmd := &models.SetAlertStateCommand{
|
2019-06-03 03:25:58 -05:00
|
|
|
AlertId: evalContext.Rule.ID,
|
|
|
|
OrgId: evalContext.Rule.OrgID,
|
2016-10-03 02:38:03 -05:00
|
|
|
State: evalContext.Rule.State,
|
2016-10-22 03:56:04 -05:00
|
|
|
Error: executionError,
|
2016-09-14 07:35:05 -05:00
|
|
|
EvalData: annotationData,
|
2016-07-21 10:31:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(cmd); err != nil {
|
2019-05-14 01:15:05 -05:00
|
|
|
if err == models.ErrCannotChangeStateOnPausedAlert {
|
2018-04-13 11:40:14 -05:00
|
|
|
handler.log.Error("Cannot change state on alert that's paused", "error", err)
|
2016-12-19 06:24:45 -06:00
|
|
|
return err
|
|
|
|
}
|
2017-02-21 07:47:02 -06:00
|
|
|
|
2019-05-14 01:15:05 -05:00
|
|
|
if err == models.ErrRequiresNewState {
|
2017-02-21 07:47:02 -06:00
|
|
|
handler.log.Info("Alert already updated")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-21 10:31:46 -05:00
|
|
|
handler.log.Error("Failed to save state", "error", err)
|
2018-10-01 07:13:03 -05:00
|
|
|
} else {
|
|
|
|
|
2018-10-02 08:11:33 -05:00
|
|
|
// StateChanges is used for de duping alert notifications
|
|
|
|
// when two servers are raising. This makes sure that the server
|
|
|
|
// with the last state change always sends a notification.
|
2018-10-01 07:13:03 -05:00
|
|
|
evalContext.Rule.StateChanges = cmd.Result.StateChanges
|
2018-11-01 10:04:38 -05:00
|
|
|
|
|
|
|
// Update the last state change of the alert rule in memory
|
|
|
|
evalContext.Rule.LastStateChange = time.Now()
|
2016-07-21 10:31:46 -05:00
|
|
|
}
|
|
|
|
|
2016-08-01 03:07:00 -05:00
|
|
|
// save annotation
|
|
|
|
item := annotations.Item{
|
2019-06-03 03:25:58 -05:00
|
|
|
OrgId: evalContext.Rule.OrgID,
|
|
|
|
DashboardId: evalContext.Rule.DashboardID,
|
|
|
|
PanelId: evalContext.Rule.PanelID,
|
|
|
|
AlertId: evalContext.Rule.ID,
|
2017-10-07 03:31:39 -05:00
|
|
|
Text: "",
|
2016-10-03 02:38:03 -05:00
|
|
|
NewState: string(evalContext.Rule.State),
|
2016-11-04 05:28:12 -05:00
|
|
|
PrevState: string(evalContext.PrevAlertState),
|
2018-04-09 05:48:01 -05:00
|
|
|
Epoch: time.Now().UnixNano() / int64(time.Millisecond),
|
2016-09-09 04:30:55 -05:00
|
|
|
Data: annotationData,
|
2016-08-01 03:07:00 -05:00
|
|
|
}
|
2016-08-16 14:26:54 -05:00
|
|
|
|
2016-08-01 03:07:00 -05:00
|
|
|
annotationRepo := annotations.GetRepository()
|
|
|
|
if err := annotationRepo.Save(&item); err != nil {
|
|
|
|
handler.log.Error("Failed to save annotation for new alert state", "error", err)
|
|
|
|
}
|
2016-07-21 10:31:46 -05:00
|
|
|
}
|
2016-10-03 02:38:03 -05:00
|
|
|
|
2018-09-24 08:58:22 -05:00
|
|
|
handler.notifier.SendIfNeeded(evalContext)
|
2016-10-03 02:38:03 -05:00
|
|
|
return nil
|
2016-06-23 05:57:10 -05:00
|
|
|
}
|