base.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package notifiers
  2. import (
  3. "context"
  4. "time"
  5. "github.com/grafana/grafana/pkg/infra/log"
  6. "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/services/alerting"
  8. )
  9. const (
  10. triggMetrString = "Triggered metrics:\n\n"
  11. )
  12. type NotifierBase struct {
  13. Name string
  14. Type string
  15. Uid string
  16. IsDeault bool
  17. UploadImage bool
  18. SendReminder bool
  19. DisableResolveMessage bool
  20. Frequency time.Duration
  21. log log.Logger
  22. }
  23. func NewNotifierBase(model *models.AlertNotification) NotifierBase {
  24. uploadImage := true
  25. value, exist := model.Settings.CheckGet("uploadImage")
  26. if exist {
  27. uploadImage = value.MustBool()
  28. }
  29. return NotifierBase{
  30. Uid: model.Uid,
  31. Name: model.Name,
  32. IsDeault: model.IsDefault,
  33. Type: model.Type,
  34. UploadImage: uploadImage,
  35. SendReminder: model.SendReminder,
  36. DisableResolveMessage: model.DisableResolveMessage,
  37. Frequency: model.Frequency,
  38. log: log.New("alerting.notifier." + model.Name),
  39. }
  40. }
  41. // ShouldNotify checks this evaluation should send an alert notification
  42. func (n *NotifierBase) ShouldNotify(ctx context.Context, context *alerting.EvalContext, notiferState *models.AlertNotificationState) bool {
  43. prevState := context.PrevAlertState
  44. newState := context.Rule.State
  45. // Only notify on state change.
  46. if prevState == newState && !n.SendReminder {
  47. return false
  48. }
  49. if prevState == newState && n.SendReminder {
  50. // Do not notify if interval has not elapsed
  51. lastNotify := time.Unix(notiferState.UpdatedAt, 0)
  52. if notiferState.UpdatedAt != 0 && lastNotify.Add(n.Frequency).After(time.Now()) {
  53. return false
  54. }
  55. // Do not notify if alert state is OK or pending even on repeated notify
  56. if newState == models.AlertStateOK || newState == models.AlertStatePending {
  57. return false
  58. }
  59. }
  60. unknownOrNoData := prevState == models.AlertStateUnknown || prevState == models.AlertStateNoData
  61. okOrPending := newState == models.AlertStatePending || newState == models.AlertStateOK
  62. // Do not notify when new state is ok/pending when previous is unknown or no_data
  63. if unknownOrNoData && okOrPending {
  64. return false
  65. }
  66. // Do not notify when we become Pending for the first
  67. if prevState == models.AlertStateNoData && newState == models.AlertStatePending {
  68. return false
  69. }
  70. // Do not notify when we become OK from pending
  71. if prevState == models.AlertStatePending && newState == models.AlertStateOK {
  72. return false
  73. }
  74. // Do not notify when we OK -> Pending
  75. if prevState == models.AlertStateOK && newState == models.AlertStatePending {
  76. return false
  77. }
  78. // Do not notify if state pending and it have been updated last minute
  79. if notiferState.State == models.AlertNotificationStatePending {
  80. lastUpdated := time.Unix(notiferState.UpdatedAt, 0)
  81. if lastUpdated.Add(1 * time.Minute).After(time.Now()) {
  82. return false
  83. }
  84. }
  85. // Do not notify when state is OK if DisableResolveMessage is set to true
  86. if newState == models.AlertStateOK && n.DisableResolveMessage {
  87. return false
  88. }
  89. return true
  90. }
  91. func (n *NotifierBase) GetType() string {
  92. return n.Type
  93. }
  94. func (n *NotifierBase) NeedsImage() bool {
  95. return n.UploadImage
  96. }
  97. func (n *NotifierBase) GetNotifierUid() string {
  98. return n.Uid
  99. }
  100. func (n *NotifierBase) GetIsDefault() bool {
  101. return n.IsDeault
  102. }
  103. func (n *NotifierBase) GetSendReminder() bool {
  104. return n.SendReminder
  105. }
  106. func (n *NotifierBase) GetDisableResolveMessage() bool {
  107. return n.DisableResolveMessage
  108. }
  109. func (n *NotifierBase) GetFrequency() time.Duration {
  110. return n.Frequency
  111. }