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"
|
2016-07-21 10:31:46 -05:00
|
|
|
"github.com/grafana/grafana/pkg/log"
|
2016-08-11 14:12:39 -05:00
|
|
|
"github.com/grafana/grafana/pkg/metrics"
|
2016-07-21 10:31:46 -05:00
|
|
|
m "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
|
|
|
|
|
|
|
type ResultHandler interface {
|
2016-10-03 02:38:03 -05:00
|
|
|
Handle(evalContext *EvalContext) error
|
2016-06-23 05:57:10 -05:00
|
|
|
}
|
|
|
|
|
2016-07-27 09:29:28 -05:00
|
|
|
type DefaultResultHandler struct {
|
2017-02-13 05:43:12 -06:00
|
|
|
notifier NotificationService
|
2016-06-23 05:57:10 -05:00
|
|
|
log log.Logger
|
|
|
|
}
|
|
|
|
|
2018-05-24 08:26:27 -05:00
|
|
|
func NewResultHandler(renderService rendering.Service) *DefaultResultHandler {
|
2016-07-27 09:29:28 -05:00
|
|
|
return &DefaultResultHandler{
|
2016-07-26 05:29:52 -05:00
|
|
|
log: log.New("alerting.resultHandler"),
|
2018-05-24 08:26:27 -05:00
|
|
|
notifier: NewNotificationService(renderService),
|
2016-06-23 05:57:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 02:38:03 -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()
|
2016-11-04 05:28:12 -05:00
|
|
|
if evalContext.ShouldUpdateAlertState() {
|
|
|
|
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
|
|
|
|
2016-07-22 06:14:09 -05:00
|
|
|
cmd := &m.SetAlertStateCommand{
|
2016-10-03 02:38:03 -05:00
|
|
|
AlertId: evalContext.Rule.Id,
|
|
|
|
OrgId: evalContext.Rule.OrgId,
|
|
|
|
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 {
|
2016-12-19 06:24:45 -06:00
|
|
|
if err == m.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
|
|
|
|
|
|
|
if err == m.ErrRequiresNewState {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-08-01 03:07:00 -05:00
|
|
|
// save annotation
|
|
|
|
item := annotations.Item{
|
2016-10-03 02:38:03 -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-05-25 13:14:33 -05:00
|
|
|
if evalContext.Rule.State == m.AlertStateOK && evalContext.PrevAlertState != m.AlertStateOK {
|
|
|
|
for _, notifierId := range evalContext.Rule.Notifications {
|
|
|
|
cmd := &m.CleanNotificationJournalCommand{
|
|
|
|
AlertId: evalContext.Rule.Id,
|
|
|
|
NotifierId: notifierId,
|
|
|
|
OrgId: evalContext.Rule.OrgId,
|
|
|
|
}
|
2018-06-15 08:30:17 -05:00
|
|
|
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
|
2018-05-25 13:14:33 -05:00
|
|
|
handler.log.Error("Failed to clean up old notification records", "notifier", notifierId, "alert", evalContext.Rule.Id, "Error", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-15 10:53:02 -06:00
|
|
|
handler.notifier.SendIfNeeded(evalContext)
|
|
|
|
|
2016-10-03 02:38:03 -05:00
|
|
|
return nil
|
2016-06-23 05:57:10 -05:00
|
|
|
}
|