result_handler.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. )
  11. type ResultHandler interface {
  12. Handle(evalContext *EvalContext) error
  13. }
  14. type DefaultResultHandler struct {
  15. notifier Notifier
  16. log log.Logger
  17. }
  18. func NewResultHandler() *DefaultResultHandler {
  19. return &DefaultResultHandler{
  20. log: log.New("alerting.resultHandler"),
  21. notifier: NewRootNotifier(),
  22. }
  23. }
  24. func (handler *DefaultResultHandler) Handle(evalContext *EvalContext) error {
  25. oldState := evalContext.Rule.State
  26. exeuctionError := ""
  27. annotationData := simplejson.New()
  28. if evalContext.Error != nil {
  29. handler.log.Error("Alert Rule Result Error", "ruleId", evalContext.Rule.Id, "error", evalContext.Error)
  30. evalContext.Rule.State = m.AlertStateExecError
  31. exeuctionError = evalContext.Error.Error()
  32. annotationData.Set("errorMessage", exeuctionError)
  33. } else if evalContext.Firing {
  34. evalContext.Rule.State = m.AlertStateAlerting
  35. annotationData = simplejson.NewFromAny(evalContext.EvalMatches)
  36. } else {
  37. // handle no data case
  38. if evalContext.NoDataFound {
  39. evalContext.Rule.State = evalContext.Rule.NoDataState
  40. } else {
  41. evalContext.Rule.State = m.AlertStateOK
  42. }
  43. }
  44. countStateResult(evalContext.Rule.State)
  45. if evalContext.Rule.State != oldState {
  46. handler.log.Info("New state change", "alertId", evalContext.Rule.Id, "newState", evalContext.Rule.State, "oldState", oldState)
  47. cmd := &m.SetAlertStateCommand{
  48. AlertId: evalContext.Rule.Id,
  49. OrgId: evalContext.Rule.OrgId,
  50. State: evalContext.Rule.State,
  51. Error: exeuctionError,
  52. EvalData: annotationData,
  53. }
  54. if err := bus.Dispatch(cmd); err != nil {
  55. handler.log.Error("Failed to save state", "error", err)
  56. }
  57. // save annotation
  58. item := annotations.Item{
  59. OrgId: evalContext.Rule.OrgId,
  60. DashboardId: evalContext.Rule.DashboardId,
  61. PanelId: evalContext.Rule.PanelId,
  62. Type: annotations.AlertType,
  63. AlertId: evalContext.Rule.Id,
  64. Title: evalContext.Rule.Name,
  65. Text: evalContext.GetStateModel().Text,
  66. NewState: string(evalContext.Rule.State),
  67. PrevState: string(oldState),
  68. Epoch: time.Now().Unix(),
  69. Data: annotationData,
  70. }
  71. annotationRepo := annotations.GetRepository()
  72. if err := annotationRepo.Save(&item); err != nil {
  73. handler.log.Error("Failed to save annotation for new alert state", "error", err)
  74. }
  75. handler.notifier.Notify(evalContext)
  76. }
  77. return nil
  78. }
  79. func countStateResult(state m.AlertStateType) {
  80. switch state {
  81. case m.AlertStateAlerting:
  82. metrics.M_Alerting_Result_State_Alerting.Inc(1)
  83. case m.AlertStateOK:
  84. metrics.M_Alerting_Result_State_Ok.Inc(1)
  85. case m.AlertStatePaused:
  86. metrics.M_Alerting_Result_State_Paused.Inc(1)
  87. case m.AlertStateNoData:
  88. metrics.M_Alerting_Result_State_NoData.Inc(1)
  89. case m.AlertStateExecError:
  90. metrics.M_Alerting_Result_State_ExecError.Inc(1)
  91. }
  92. }