2016-06-23 05:57:10 -05:00
|
|
|
package alerting
|
|
|
|
|
2016-07-21 10:31:46 -05:00
|
|
|
import (
|
2019-10-22 07:08:18 -05:00
|
|
|
"context"
|
2020-07-31 02:45:20 -05:00
|
|
|
"errors"
|
2016-08-01 03:07:00 -05:00
|
|
|
"time"
|
|
|
|
|
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"
|
2022-02-03 06:26:05 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/notifications"
|
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
|
2022-02-03 06:26:05 -06:00
|
|
|
sqlStore AlertStore
|
2016-06-23 05:57:10 -05:00
|
|
|
log log.Logger
|
|
|
|
}
|
|
|
|
|
2022-02-03 06:26:05 -06:00
|
|
|
func newResultHandler(renderService rendering.Service, sqlStore AlertStore, notificationService *notifications.NotificationService, decryptFn GetDecryptedValueFn) *defaultResultHandler {
|
2019-05-20 05:13:32 -05:00
|
|
|
return &defaultResultHandler{
|
2016-07-26 05:29:52 -05:00
|
|
|
log: log.New("alerting.resultHandler"),
|
2022-02-03 06:26:05 -06:00
|
|
|
sqlStore: sqlStore,
|
|
|
|
notifier: newNotificationService(renderService, sqlStore, notificationService, decryptFn),
|
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
|
|
|
}
|
|
|
|
|
2019-07-16 09:58:46 -05:00
|
|
|
metrics.MAlertingResultState.WithLabelValues(string(evalContext.Rule.State)).Inc()
|
2019-05-20 05:13:32 -05:00
|
|
|
if evalContext.shouldUpdateAlertState() {
|
2019-08-27 01:40:03 -05:00
|
|
|
handler.log.Info("New state change", "ruleId", 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
|
|
|
}
|
|
|
|
|
2022-02-03 06:26:05 -06:00
|
|
|
if err := handler.sqlStore.SetAlertState(evalContext.Ctx, cmd); err != nil {
|
2020-11-19 07:47:17 -06:00
|
|
|
if errors.Is(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
|
|
|
|
2020-11-19 07:47:17 -06:00
|
|
|
if errors.Is(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
|
|
|
|
2019-10-22 07:08:18 -05:00
|
|
|
if err := handler.notifier.SendIfNeeded(evalContext); err != nil {
|
2020-07-16 07:39:01 -05:00
|
|
|
switch {
|
2020-07-31 02:45:20 -05:00
|
|
|
case errors.Is(err, context.Canceled):
|
2019-10-22 07:08:18 -05:00
|
|
|
handler.log.Debug("handler.notifier.SendIfNeeded returned context.Canceled")
|
2020-07-31 02:45:20 -05:00
|
|
|
case errors.Is(err, context.DeadlineExceeded):
|
2019-10-22 07:08:18 -05:00
|
|
|
handler.log.Debug("handler.notifier.SendIfNeeded returned context.DeadlineExceeded")
|
2020-07-16 07:39:01 -05:00
|
|
|
default:
|
2019-10-22 07:08:18 -05:00
|
|
|
handler.log.Error("handler.notifier.SendIfNeeded failed", "err", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 02:38:03 -05:00
|
|
|
return nil
|
2016-06-23 05:57:10 -05:00
|
|
|
}
|