base.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. // NotifierBase is the base implementation of a notifier.
  13. type NotifierBase struct {
  14. Name string
  15. Type string
  16. UID string
  17. IsDeault bool
  18. UploadImage bool
  19. SendReminder bool
  20. DisableResolveMessage bool
  21. Frequency time.Duration
  22. log log.Logger
  23. }
  24. // NewNotifierBase returns a new `NotifierBase`.
  25. func NewNotifierBase(model *models.AlertNotification) NotifierBase {
  26. uploadImage := true
  27. value, exist := model.Settings.CheckGet("uploadImage")
  28. if exist {
  29. uploadImage = value.MustBool()
  30. }
  31. return NotifierBase{
  32. UID: model.Uid,
  33. Name: model.Name,
  34. IsDeault: model.IsDefault,
  35. Type: model.Type,
  36. UploadImage: uploadImage,
  37. SendReminder: model.SendReminder,
  38. DisableResolveMessage: model.DisableResolveMessage,
  39. Frequency: model.Frequency,
  40. log: log.New("alerting.notifier." + model.Name),
  41. }
  42. }
  43. // ShouldNotify checks this evaluation should send an alert notification
  44. func (n *NotifierBase) ShouldNotify(ctx context.Context, context *alerting.EvalContext, notiferState *models.AlertNotificationState) bool {
  45. prevState := context.PrevAlertState
  46. newState := context.Rule.State
  47. // Only notify on state change.
  48. if prevState == newState && !n.SendReminder {
  49. return false
  50. }
  51. if prevState == newState && n.SendReminder {
  52. // Do not notify if interval has not elapsed
  53. lastNotify := time.Unix(notiferState.UpdatedAt, 0)
  54. if notiferState.UpdatedAt != 0 && lastNotify.Add(n.Frequency).After(time.Now()) {
  55. return false
  56. }
  57. // Do not notify if alert state is OK or pending even on repeated notify
  58. if newState == models.AlertStateOK || newState == models.AlertStatePending {
  59. return false
  60. }
  61. }
  62. okOrPending := newState == models.AlertStatePending || newState == models.AlertStateOK
  63. // Do not notify when new state is ok/pending when previous is unknown
  64. if prevState == models.AlertStateUnknown && okOrPending {
  65. return false
  66. }
  67. // Do not notify when we become Pending for the first
  68. if prevState == models.AlertStateNoData && newState == models.AlertStatePending {
  69. return false
  70. }
  71. // Do not notify when we become OK from pending
  72. if prevState == models.AlertStatePending && newState == models.AlertStateOK {
  73. return false
  74. }
  75. // Do not notify when we OK -> Pending
  76. if prevState == models.AlertStateOK && newState == models.AlertStatePending {
  77. return false
  78. }
  79. // Do not notify if state pending and it have been updated last minute
  80. if notiferState.State == models.AlertNotificationStatePending {
  81. lastUpdated := time.Unix(notiferState.UpdatedAt, 0)
  82. if lastUpdated.Add(1 * time.Minute).After(time.Now()) {
  83. return false
  84. }
  85. }
  86. // Do not notify when state is OK if DisableResolveMessage is set to true
  87. if newState == models.AlertStateOK && n.DisableResolveMessage {
  88. return false
  89. }
  90. return true
  91. }
  92. // GetType returns the notifier type.
  93. func (n *NotifierBase) GetType() string {
  94. return n.Type
  95. }
  96. // NeedsImage returns true if an image is expected in the notification.
  97. func (n *NotifierBase) NeedsImage() bool {
  98. return n.UploadImage
  99. }
  100. // GetNotifierUID returns the notifier `uid`.
  101. func (n *NotifierBase) GetNotifierUID() string {
  102. return n.UID
  103. }
  104. // GetIsDefault returns true if the notifiers should
  105. // be used for all alerts.
  106. func (n *NotifierBase) GetIsDefault() bool {
  107. return n.IsDeault
  108. }
  109. // GetSendReminder returns true if reminders should be sent.
  110. func (n *NotifierBase) GetSendReminder() bool {
  111. return n.SendReminder
  112. }
  113. // GetDisableResolveMessage returns true if ok alert notifications
  114. // should be skipped.
  115. func (n *NotifierBase) GetDisableResolveMessage() bool {
  116. return n.DisableResolveMessage
  117. }
  118. // GetFrequency returns the freqency for how often
  119. // alerts should be evaluated.
  120. func (n *NotifierBase) GetFrequency() time.Duration {
  121. return n.Frequency
  122. }