2016-06-23 12:57:10 +02:00
|
|
|
package alerting
|
|
|
|
|
|
2016-07-21 17:31:46 +02:00
|
|
|
import (
|
2019-10-22 14:08:18 +02:00
|
|
|
"context"
|
2020-07-31 09:45:20 +02:00
|
|
|
"errors"
|
2016-08-01 10:07:00 +02:00
|
|
|
"time"
|
|
|
|
|
|
2016-07-21 17:31:46 +02:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2016-08-30 13:22:59 +02:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2019-05-13 14:45:54 +08:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2019-02-23 23:35:26 +01:00
|
|
|
"github.com/grafana/grafana/pkg/infra/metrics"
|
2019-05-14 08:15:05 +02:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
|
|
2016-08-01 10:07:00 +02:00
|
|
|
"github.com/grafana/grafana/pkg/services/annotations"
|
2018-05-24 15:26:27 +02:00
|
|
|
"github.com/grafana/grafana/pkg/services/rendering"
|
2016-07-21 17:31:46 +02:00
|
|
|
)
|
2016-06-23 12:57:10 +02:00
|
|
|
|
2019-05-20 12:13:32 +02:00
|
|
|
type resultHandler interface {
|
|
|
|
|
handle(evalContext *EvalContext) error
|
2016-06-23 12:57:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-20 12:13:32 +02:00
|
|
|
type defaultResultHandler struct {
|
|
|
|
|
notifier *notificationService
|
2016-06-23 12:57:10 +02:00
|
|
|
log log.Logger
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-20 12:13:32 +02:00
|
|
|
func newResultHandler(renderService rendering.Service) *defaultResultHandler {
|
|
|
|
|
return &defaultResultHandler{
|
2016-07-26 12:29:52 +02:00
|
|
|
log: log.New("alerting.resultHandler"),
|
2019-05-20 12:13:32 +02:00
|
|
|
notifier: newNotificationService(renderService),
|
2016-06-23 12:57:10 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-20 12:13:32 +02:00
|
|
|
func (handler *defaultResultHandler) handle(evalContext *EvalContext) error {
|
2016-10-22 10:56:04 +02:00
|
|
|
executionError := ""
|
2016-08-31 14:06:54 +02:00
|
|
|
annotationData := simplejson.New()
|
2016-11-07 12:42:39 +01:00
|
|
|
|
2017-05-15 16:17:05 +02:00
|
|
|
if len(evalContext.EvalMatches) > 0 {
|
|
|
|
|
annotationData.Set("evalMatches", simplejson.NewFromAny(evalContext.EvalMatches))
|
2017-01-16 18:01:08 +01:00
|
|
|
}
|
|
|
|
|
|
2016-10-03 09:38:03 +02:00
|
|
|
if evalContext.Error != nil {
|
2016-10-22 10:56:04 +02:00
|
|
|
executionError = evalContext.Error.Error()
|
2017-05-15 16:17:05 +02:00
|
|
|
annotationData.Set("error", executionError)
|
|
|
|
|
} else if evalContext.NoDataFound {
|
|
|
|
|
annotationData.Set("noData", true)
|
2016-07-21 17:31:46 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-16 17:58:46 +03:00
|
|
|
metrics.MAlertingResultState.WithLabelValues(string(evalContext.Rule.State)).Inc()
|
2019-05-20 12:13:32 +02:00
|
|
|
if evalContext.shouldUpdateAlertState() {
|
2019-08-27 08:40:03 +02:00
|
|
|
handler.log.Info("New state change", "ruleId", evalContext.Rule.ID, "newState", evalContext.Rule.State, "prev state", evalContext.PrevAlertState)
|
2016-07-21 17:31:46 +02:00
|
|
|
|
2019-05-14 08:15:05 +02:00
|
|
|
cmd := &models.SetAlertStateCommand{
|
2019-06-03 10:25:58 +02:00
|
|
|
AlertId: evalContext.Rule.ID,
|
|
|
|
|
OrgId: evalContext.Rule.OrgID,
|
2016-10-03 09:38:03 +02:00
|
|
|
State: evalContext.Rule.State,
|
2016-10-22 10:56:04 +02:00
|
|
|
Error: executionError,
|
2016-09-14 14:35:05 +02:00
|
|
|
EvalData: annotationData,
|
2016-07-21 17:31:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := bus.Dispatch(cmd); err != nil {
|
2020-11-19 14:47:17 +01:00
|
|
|
if errors.Is(err, models.ErrCannotChangeStateOnPausedAlert) {
|
2018-04-13 18:40:14 +02:00
|
|
|
handler.log.Error("Cannot change state on alert that's paused", "error", err)
|
2016-12-19 13:24:45 +01:00
|
|
|
return err
|
|
|
|
|
}
|
2017-02-21 14:47:02 +01:00
|
|
|
|
2020-11-19 14:47:17 +01:00
|
|
|
if errors.Is(err, models.ErrRequiresNewState) {
|
2017-02-21 14:47:02 +01:00
|
|
|
handler.log.Info("Alert already updated")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-21 17:31:46 +02:00
|
|
|
handler.log.Error("Failed to save state", "error", err)
|
2018-10-01 14:13:03 +02:00
|
|
|
} else {
|
2018-10-02 15:11:33 +02: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 14:13:03 +02:00
|
|
|
evalContext.Rule.StateChanges = cmd.Result.StateChanges
|
2018-11-01 16:04:38 +01:00
|
|
|
|
|
|
|
|
// Update the last state change of the alert rule in memory
|
|
|
|
|
evalContext.Rule.LastStateChange = time.Now()
|
2016-07-21 17:31:46 +02:00
|
|
|
}
|
|
|
|
|
|
2016-08-01 10:07:00 +02:00
|
|
|
// save annotation
|
|
|
|
|
item := annotations.Item{
|
2019-06-03 10:25:58 +02:00
|
|
|
OrgId: evalContext.Rule.OrgID,
|
|
|
|
|
DashboardId: evalContext.Rule.DashboardID,
|
|
|
|
|
PanelId: evalContext.Rule.PanelID,
|
|
|
|
|
AlertId: evalContext.Rule.ID,
|
2017-10-07 10:31:39 +02:00
|
|
|
Text: "",
|
2016-10-03 09:38:03 +02:00
|
|
|
NewState: string(evalContext.Rule.State),
|
2016-11-04 11:28:12 +01:00
|
|
|
PrevState: string(evalContext.PrevAlertState),
|
2018-04-09 12:48:01 +02:00
|
|
|
Epoch: time.Now().UnixNano() / int64(time.Millisecond),
|
2016-09-09 11:30:55 +02:00
|
|
|
Data: annotationData,
|
2016-08-01 10:07:00 +02:00
|
|
|
}
|
2016-08-16 21:26:54 +02:00
|
|
|
|
2016-08-01 10:07:00 +02: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 17:31:46 +02:00
|
|
|
}
|
2016-10-03 09:38:03 +02:00
|
|
|
|
2019-10-22 14:08:18 +02:00
|
|
|
if err := handler.notifier.SendIfNeeded(evalContext); err != nil {
|
2020-07-16 14:39:01 +02:00
|
|
|
switch {
|
2020-07-31 09:45:20 +02:00
|
|
|
case errors.Is(err, context.Canceled):
|
2019-10-22 14:08:18 +02:00
|
|
|
handler.log.Debug("handler.notifier.SendIfNeeded returned context.Canceled")
|
2020-07-31 09:45:20 +02:00
|
|
|
case errors.Is(err, context.DeadlineExceeded):
|
2019-10-22 14:08:18 +02:00
|
|
|
handler.log.Debug("handler.notifier.SendIfNeeded returned context.DeadlineExceeded")
|
2020-07-16 14:39:01 +02:00
|
|
|
default:
|
2019-10-22 14:08:18 +02:00
|
|
|
handler.log.Error("handler.notifier.SendIfNeeded failed", "err", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-03 09:38:03 +02:00
|
|
|
return nil
|
2016-06-23 12:57:10 +02:00
|
|
|
}
|