base.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package notifiers
  2. import (
  3. "context"
  4. "time"
  5. "github.com/grafana/grafana/pkg/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. Id int64
  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. Id: model.Id,
  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. // Only notify on state change.
  44. if context.PrevAlertState == context.Rule.State && !n.SendReminder {
  45. return false
  46. }
  47. if context.PrevAlertState == context.Rule.State && n.SendReminder {
  48. // Do not notify if interval has not elapsed
  49. lastNotify := time.Unix(notiferState.UpdatedAt, 0)
  50. if notiferState.UpdatedAt != 0 && lastNotify.Add(n.Frequency).After(time.Now()) {
  51. return false
  52. }
  53. // Do not notify if alert state is OK or pending even on repeated notify
  54. if context.Rule.State == models.AlertStateOK || context.Rule.State == models.AlertStatePending {
  55. return false
  56. }
  57. }
  58. // Do not notify when we become OK for the first time.
  59. if context.PrevAlertState == models.AlertStatePending && context.Rule.State == models.AlertStateOK {
  60. return false
  61. }
  62. // Do not notify when we OK -> Pending
  63. if context.PrevAlertState == models.AlertStateOK && context.Rule.State == models.AlertStatePending {
  64. return false
  65. }
  66. // Do not notifu if state pending and it have been updated last minute
  67. if notiferState.State == models.AlertNotificationStatePending {
  68. lastUpdated := time.Unix(notiferState.UpdatedAt, 0)
  69. if lastUpdated.Add(1 * time.Minute).After(time.Now()) {
  70. return false
  71. }
  72. }
  73. // Do not notify when state is OK if DisableResolveMessage is set to true
  74. if context.Rule.State == models.AlertStateOK && n.DisableResolveMessage {
  75. return false
  76. }
  77. return true
  78. }
  79. func (n *NotifierBase) GetType() string {
  80. return n.Type
  81. }
  82. func (n *NotifierBase) NeedsImage() bool {
  83. return n.UploadImage
  84. }
  85. func (n *NotifierBase) GetNotifierId() int64 {
  86. return n.Id
  87. }
  88. func (n *NotifierBase) GetIsDefault() bool {
  89. return n.IsDeault
  90. }
  91. func (n *NotifierBase) GetSendReminder() bool {
  92. return n.SendReminder
  93. }
  94. func (n *NotifierBase) GetDisableResolveMessage() bool {
  95. return n.DisableResolveMessage
  96. }
  97. func (n *NotifierBase) GetFrequency() time.Duration {
  98. return n.Frequency
  99. }