result_handler.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. ctx.Rule.State = m.AlertStateOK
  38. }
  39. countStateResult(ctx.Rule.State)
  40. if ctx.Rule.State != oldState {
  41. handler.log.Info("New state change", "alertId", ctx.Rule.Id, "newState", ctx.Rule.State, "oldState", oldState)
  42. cmd := &m.SetAlertStateCommand{
  43. AlertId: ctx.Rule.Id,
  44. OrgId: ctx.Rule.OrgId,
  45. State: ctx.Rule.State,
  46. Error: exeuctionError,
  47. }
  48. if err := bus.Dispatch(cmd); err != nil {
  49. handler.log.Error("Failed to save state", "error", err)
  50. }
  51. // save annotation
  52. item := annotations.Item{
  53. OrgId: ctx.Rule.OrgId,
  54. Type: annotations.AlertType,
  55. AlertId: ctx.Rule.Id,
  56. Title: ctx.Rule.Name,
  57. Text: ctx.GetStateText(),
  58. NewState: string(ctx.Rule.State),
  59. PrevState: string(oldState),
  60. Timestamp: time.Now(),
  61. Data: annotationData,
  62. }
  63. annotationRepo := annotations.GetRepository()
  64. if err := annotationRepo.Save(&item); err != nil {
  65. handler.log.Error("Failed to save annotation for new alert state", "error", err)
  66. }
  67. handler.notifier.Notify(ctx)
  68. }
  69. }
  70. func countStateResult(state m.AlertStateType) {
  71. switch state {
  72. case m.AlertStateCritical:
  73. metrics.M_Alerting_Result_State_Critical.Inc(1)
  74. case m.AlertStateWarning:
  75. metrics.M_Alerting_Result_State_Warning.Inc(1)
  76. case m.AlertStateOK:
  77. metrics.M_Alerting_Result_State_Ok.Inc(1)
  78. case m.AlertStatePaused:
  79. metrics.M_Alerting_Result_State_Paused.Inc(1)
  80. case m.AlertStatePending:
  81. metrics.M_Alerting_Result_State_Pending.Inc(1)
  82. case m.AlertStateExeuctionError:
  83. metrics.M_Alerting_Result_State_ExecutionError.Inc(1)
  84. }
  85. }