result_handler.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. executionError := ""
  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. executionError = evalContext.Error.Error()
  32. annotationData.Set("errorMessage", executionError)
  33. } else if evalContext.Firing {
  34. evalContext.Rule.State = m.AlertStateAlerting
  35. annotationData = simplejson.NewFromAny(evalContext.EvalMatches)
  36. } else {
  37. if evalContext.NoDataFound {
  38. if evalContext.Rule.NoDataState != m.NoDataKeepState {
  39. evalContext.Rule.State = evalContext.Rule.NoDataState.ToAlertState()
  40. }
  41. } else {
  42. evalContext.Rule.State = m.AlertStateOK
  43. }
  44. }
  45. countStateResult(evalContext.Rule.State)
  46. if handler.shouldUpdateAlertState(evalContext, oldState) {
  47. handler.log.Info("New state change", "alertId", evalContext.Rule.Id, "newState", evalContext.Rule.State, "oldState", oldState)
  48. cmd := &m.SetAlertStateCommand{
  49. AlertId: evalContext.Rule.Id,
  50. OrgId: evalContext.Rule.OrgId,
  51. State: evalContext.Rule.State,
  52. Error: executionError,
  53. EvalData: annotationData,
  54. }
  55. if err := bus.Dispatch(cmd); err != nil {
  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. Type: annotations.AlertType,
  64. AlertId: evalContext.Rule.Id,
  65. Title: evalContext.Rule.Name,
  66. Text: evalContext.GetStateModel().Text,
  67. NewState: string(evalContext.Rule.State),
  68. PrevState: string(oldState),
  69. Epoch: time.Now().Unix(),
  70. Data: annotationData,
  71. }
  72. annotationRepo := annotations.GetRepository()
  73. if err := annotationRepo.Save(&item); err != nil {
  74. handler.log.Error("Failed to save annotation for new alert state", "error", err)
  75. }
  76. handler.notifier.Notify(evalContext)
  77. }
  78. return nil
  79. }
  80. func (handler *DefaultResultHandler) shouldUpdateAlertState(evalContext *EvalContext, oldState m.AlertStateType) bool {
  81. return evalContext.Rule.State != oldState
  82. }
  83. func countStateResult(state m.AlertStateType) {
  84. switch state {
  85. case m.AlertStateAlerting:
  86. metrics.M_Alerting_Result_State_Alerting.Inc(1)
  87. case m.AlertStateOK:
  88. metrics.M_Alerting_Result_State_Ok.Inc(1)
  89. case m.AlertStatePaused:
  90. metrics.M_Alerting_Result_State_Paused.Inc(1)
  91. case m.AlertStateNoData:
  92. metrics.M_Alerting_Result_State_NoData.Inc(1)
  93. case m.AlertStateExecError:
  94. metrics.M_Alerting_Result_State_ExecError.Inc(1)
  95. }
  96. }