result_handler.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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) GetStateFromEvaluation(evalContext *EvalContext) m.AlertStateType {
  25. if evalContext.Error != nil {
  26. handler.log.Error("Alert Rule Result Error",
  27. "ruleId", evalContext.Rule.Id,
  28. "name", evalContext.Rule.Name,
  29. "error", evalContext.Error,
  30. "changing state to", evalContext.Rule.ExecutionErrorState.ToAlertState())
  31. if evalContext.Rule.ExecutionErrorState == m.ExecutionErrorKeepState {
  32. return evalContext.PrevAlertState
  33. } else {
  34. return evalContext.Rule.ExecutionErrorState.ToAlertState()
  35. }
  36. } else if evalContext.Firing {
  37. return m.AlertStateAlerting
  38. } else if evalContext.NoDataFound {
  39. handler.log.Info("Alert Rule returned no data",
  40. "ruleId", evalContext.Rule.Id,
  41. "name", evalContext.Rule.Name,
  42. "changing state to", evalContext.Rule.NoDataState.ToAlertState())
  43. if evalContext.Rule.NoDataState == m.NoDataKeepState {
  44. return evalContext.PrevAlertState
  45. } else {
  46. return evalContext.Rule.NoDataState.ToAlertState()
  47. }
  48. }
  49. return m.AlertStateOK
  50. }
  51. func (handler *DefaultResultHandler) Handle(evalContext *EvalContext) error {
  52. executionError := ""
  53. annotationData := simplejson.New()
  54. evalContext.Rule.State = handler.GetStateFromEvaluation(evalContext)
  55. if evalContext.Error != nil {
  56. executionError = evalContext.Error.Error()
  57. annotationData.Set("errorMessage", executionError)
  58. }
  59. if evalContext.Firing {
  60. annotationData = simplejson.NewFromAny(evalContext.EvalMatches)
  61. }
  62. countStateResult(evalContext.Rule.State)
  63. if evalContext.ShouldUpdateAlertState() {
  64. handler.log.Info("New state change", "alertId", evalContext.Rule.Id, "newState", evalContext.Rule.State, "prev state", evalContext.PrevAlertState)
  65. cmd := &m.SetAlertStateCommand{
  66. AlertId: evalContext.Rule.Id,
  67. OrgId: evalContext.Rule.OrgId,
  68. State: evalContext.Rule.State,
  69. Error: executionError,
  70. EvalData: annotationData,
  71. }
  72. if err := bus.Dispatch(cmd); err != nil {
  73. handler.log.Error("Failed to save state", "error", err)
  74. }
  75. // save annotation
  76. item := annotations.Item{
  77. OrgId: evalContext.Rule.OrgId,
  78. DashboardId: evalContext.Rule.DashboardId,
  79. PanelId: evalContext.Rule.PanelId,
  80. Type: annotations.AlertType,
  81. AlertId: evalContext.Rule.Id,
  82. Title: evalContext.Rule.Name,
  83. Text: evalContext.GetStateModel().Text,
  84. NewState: string(evalContext.Rule.State),
  85. PrevState: string(evalContext.PrevAlertState),
  86. Epoch: time.Now().Unix(),
  87. Data: annotationData,
  88. }
  89. annotationRepo := annotations.GetRepository()
  90. if err := annotationRepo.Save(&item); err != nil {
  91. handler.log.Error("Failed to save annotation for new alert state", "error", err)
  92. }
  93. if evalContext.ShouldSendNotification() {
  94. handler.notifier.Notify(evalContext)
  95. }
  96. }
  97. return nil
  98. }
  99. func countStateResult(state m.AlertStateType) {
  100. switch state {
  101. case m.AlertStatePending:
  102. metrics.M_Alerting_Result_State_Pending.Inc(1)
  103. case m.AlertStateAlerting:
  104. metrics.M_Alerting_Result_State_Alerting.Inc(1)
  105. case m.AlertStateOK:
  106. metrics.M_Alerting_Result_State_Ok.Inc(1)
  107. case m.AlertStatePaused:
  108. metrics.M_Alerting_Result_State_Paused.Inc(1)
  109. case m.AlertStateNoData:
  110. metrics.M_Alerting_Result_State_NoData.Inc(1)
  111. }
  112. }