result_handler.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package alerting
  2. import (
  3. "time"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/components/simplejson"
  6. "github.com/grafana/grafana/pkg/log"
  7. "github.com/grafana/grafana/pkg/metrics"
  8. m "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/services/annotations"
  10. "github.com/grafana/grafana/pkg/services/rendering"
  11. )
  12. type ResultHandler interface {
  13. Handle(evalContext *EvalContext) error
  14. }
  15. type DefaultResultHandler struct {
  16. notifier NotificationService
  17. log log.Logger
  18. }
  19. func NewResultHandler(renderService rendering.Service) *DefaultResultHandler {
  20. return &DefaultResultHandler{
  21. log: log.New("alerting.resultHandler"),
  22. notifier: NewNotificationService(renderService),
  23. }
  24. }
  25. func (handler *DefaultResultHandler) Handle(evalContext *EvalContext) error {
  26. executionError := ""
  27. annotationData := simplejson.New()
  28. if len(evalContext.EvalMatches) > 0 {
  29. annotationData.Set("evalMatches", simplejson.NewFromAny(evalContext.EvalMatches))
  30. }
  31. if evalContext.Error != nil {
  32. executionError = evalContext.Error.Error()
  33. annotationData.Set("error", executionError)
  34. } else if evalContext.NoDataFound {
  35. annotationData.Set("noData", true)
  36. }
  37. metrics.M_Alerting_Result_State.WithLabelValues(string(evalContext.Rule.State)).Inc()
  38. if evalContext.ShouldUpdateAlertState() {
  39. handler.log.Info("New state change", "alertId", evalContext.Rule.Id, "newState", evalContext.Rule.State, "prev state", evalContext.PrevAlertState)
  40. cmd := &m.SetAlertStateCommand{
  41. AlertId: evalContext.Rule.Id,
  42. OrgId: evalContext.Rule.OrgId,
  43. State: evalContext.Rule.State,
  44. Error: executionError,
  45. EvalData: annotationData,
  46. }
  47. if err := bus.Dispatch(cmd); err != nil {
  48. if err == m.ErrCannotChangeStateOnPausedAlert {
  49. handler.log.Error("Cannot change state on alert that's paused", "error", err)
  50. return err
  51. }
  52. if err == m.ErrRequiresNewState {
  53. handler.log.Info("Alert already updated")
  54. return nil
  55. }
  56. handler.log.Error("Failed to save state", "error", err)
  57. }
  58. // save annotation
  59. item := annotations.Item{
  60. OrgId: evalContext.Rule.OrgId,
  61. DashboardId: evalContext.Rule.DashboardId,
  62. PanelId: evalContext.Rule.PanelId,
  63. AlertId: evalContext.Rule.Id,
  64. Text: "",
  65. NewState: string(evalContext.Rule.State),
  66. PrevState: string(evalContext.PrevAlertState),
  67. Epoch: time.Now().UnixNano() / int64(time.Millisecond),
  68. Data: annotationData,
  69. }
  70. annotationRepo := annotations.GetRepository()
  71. if err := annotationRepo.Save(&item); err != nil {
  72. handler.log.Error("Failed to save annotation for new alert state", "error", err)
  73. }
  74. }
  75. if evalContext.Rule.State == m.AlertStateOK && evalContext.PrevAlertState != m.AlertStateOK {
  76. for _, notifierId := range evalContext.Rule.Notifications {
  77. cmd := &m.CleanNotificationJournalCommand{
  78. AlertId: evalContext.Rule.Id,
  79. NotifierId: notifierId,
  80. OrgId: evalContext.Rule.OrgId,
  81. }
  82. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  83. handler.log.Error("Failed to clean up old notification records", "notifier", notifierId, "alert", evalContext.Rule.Id, "Error", err)
  84. }
  85. }
  86. }
  87. handler.notifier.SendIfNeeded(evalContext)
  88. return nil
  89. }