result_handler.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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(ctx *EvalContext)
  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(ctx *EvalContext) {
  25. oldState := ctx.Rule.State
  26. exeuctionError := ""
  27. annotationData := simplejson.New()
  28. if ctx.Error != nil {
  29. handler.log.Error("Alert Rule Result Error", "ruleId", ctx.Rule.Id, "error", ctx.Error)
  30. ctx.Rule.State = m.AlertStateExeuctionError
  31. exeuctionError = ctx.Error.Error()
  32. annotationData.Set("errorMessage", exeuctionError)
  33. } else if ctx.Firing {
  34. ctx.Rule.State = m.AlertStateType(ctx.Rule.Severity)
  35. annotationData = simplejson.NewFromAny(ctx.EvalMatches)
  36. } else {
  37. // handle no data case
  38. if ctx.NoDataFound {
  39. ctx.Rule.State = ctx.Rule.NoDataState
  40. } else {
  41. ctx.Rule.State = m.AlertStateOK
  42. }
  43. }
  44. countStateResult(ctx.Rule.State)
  45. if ctx.Rule.State != oldState {
  46. handler.log.Info("New state change", "alertId", ctx.Rule.Id, "newState", ctx.Rule.State, "oldState", oldState)
  47. cmd := &m.SetAlertStateCommand{
  48. AlertId: ctx.Rule.Id,
  49. OrgId: ctx.Rule.OrgId,
  50. State: ctx.Rule.State,
  51. Error: exeuctionError,
  52. }
  53. if err := bus.Dispatch(cmd); err != nil {
  54. handler.log.Error("Failed to save state", "error", err)
  55. }
  56. // save annotation
  57. item := annotations.Item{
  58. OrgId: ctx.Rule.OrgId,
  59. Type: annotations.AlertType,
  60. AlertId: ctx.Rule.Id,
  61. Title: ctx.Rule.Name,
  62. Text: ctx.GetStateModel().Text,
  63. NewState: string(ctx.Rule.State),
  64. PrevState: string(oldState),
  65. Timestamp: time.Now(),
  66. Data: annotationData,
  67. }
  68. annotationRepo := annotations.GetRepository()
  69. if err := annotationRepo.Save(&item); err != nil {
  70. handler.log.Error("Failed to save annotation for new alert state", "error", err)
  71. }
  72. handler.notifier.Notify(ctx)
  73. }
  74. }
  75. func countStateResult(state m.AlertStateType) {
  76. switch state {
  77. case m.AlertStateCritical:
  78. metrics.M_Alerting_Result_State_Critical.Inc(1)
  79. case m.AlertStateWarning:
  80. metrics.M_Alerting_Result_State_Warning.Inc(1)
  81. case m.AlertStateOK:
  82. metrics.M_Alerting_Result_State_Ok.Inc(1)
  83. case m.AlertStatePaused:
  84. metrics.M_Alerting_Result_State_Paused.Inc(1)
  85. case m.AlertStateUnknown:
  86. metrics.M_Alerting_Result_State_Unknown.Inc(1)
  87. case m.AlertStateExeuctionError:
  88. metrics.M_Alerting_Result_State_ExecutionError.Inc(1)
  89. }
  90. }