diff --git a/pkg/services/alerting/notifiers/base.go b/pkg/services/alerting/notifiers/base.go index 601f8fc24b1..7a3cc71c4db 100644 --- a/pkg/services/alerting/notifiers/base.go +++ b/pkg/services/alerting/notifiers/base.go @@ -27,15 +27,21 @@ func NewNotifierBase(id int64, isDefault bool, name, notifierType string, model } func defaultShouldNotify(context *alerting.EvalContext) bool { + // Only notify on state change. if context.PrevAlertState == context.Rule.State { return false } + // Do not notify when we become OK for the first time. if (context.PrevAlertState == m.AlertStatePending) && (context.Rule.State == m.AlertStateOK) { return false } return true } +func (n *NotifierBase) ShouldNotify(context *alerting.EvalContext) bool { + return defaultShouldNotify(context) +} + func (n *NotifierBase) GetType() string { return n.Type } diff --git a/pkg/services/alerting/notifiers/dingding.go b/pkg/services/alerting/notifiers/dingding.go index c2029b1173c..e32b9d34f91 100644 --- a/pkg/services/alerting/notifiers/dingding.go +++ b/pkg/services/alerting/notifiers/dingding.go @@ -38,10 +38,6 @@ func NewDingDingNotifier(model *m.AlertNotification) (alerting.Notifier, error) }, nil } -func (this *DingDingNotifier) ShouldNotify(context *alerting.EvalContext) bool { - return defaultShouldNotify(context) -} - type DingDingNotifier struct { NotifierBase Url string diff --git a/pkg/services/alerting/notifiers/email.go b/pkg/services/alerting/notifiers/email.go index 095f7c7156a..562ffbe1269 100644 --- a/pkg/services/alerting/notifiers/email.go +++ b/pkg/services/alerting/notifiers/email.go @@ -58,10 +58,6 @@ func NewEmailNotifier(model *m.AlertNotification) (alerting.Notifier, error) { }, nil } -func (this *EmailNotifier) ShouldNotify(context *alerting.EvalContext) bool { - return defaultShouldNotify(context) -} - func (this *EmailNotifier) Notify(evalContext *alerting.EvalContext) error { this.log.Info("Sending alert notification to", "addresses", this.Addresses) diff --git a/pkg/services/alerting/notifiers/hipchat.go b/pkg/services/alerting/notifiers/hipchat.go index b65f25b1422..f1f63d42a04 100644 --- a/pkg/services/alerting/notifiers/hipchat.go +++ b/pkg/services/alerting/notifiers/hipchat.go @@ -75,10 +75,6 @@ type HipChatNotifier struct { log log.Logger } -func (this *HipChatNotifier) ShouldNotify(context *alerting.EvalContext) bool { - return defaultShouldNotify(context) -} - func (this *HipChatNotifier) Notify(evalContext *alerting.EvalContext) error { this.log.Info("Executing hipchat notification", "ruleId", evalContext.Rule.Id, "notification", this.Name) diff --git a/pkg/services/alerting/notifiers/kafka.go b/pkg/services/alerting/notifiers/kafka.go index e885d44405d..92f6489106b 100644 --- a/pkg/services/alerting/notifiers/kafka.go +++ b/pkg/services/alerting/notifiers/kafka.go @@ -57,10 +57,6 @@ type KafkaNotifier struct { log log.Logger } -func (this *KafkaNotifier) ShouldNotify(context *alerting.EvalContext) bool { - return defaultShouldNotify(context) -} - func (this *KafkaNotifier) Notify(evalContext *alerting.EvalContext) error { state := evalContext.Rule.State diff --git a/pkg/services/alerting/notifiers/line.go b/pkg/services/alerting/notifiers/line.go index bc0b0c984a4..4fbaa2d543e 100644 --- a/pkg/services/alerting/notifiers/line.go +++ b/pkg/services/alerting/notifiers/line.go @@ -51,10 +51,6 @@ type LineNotifier struct { log log.Logger } -func (this *LineNotifier) ShouldNotify(context *alerting.EvalContext) bool { - return defaultShouldNotify(context) -} - func (this *LineNotifier) Notify(evalContext *alerting.EvalContext) error { this.log.Info("Executing line notification", "ruleId", evalContext.Rule.Id, "notification", this.Name) diff --git a/pkg/services/alerting/notifiers/opsgenie.go b/pkg/services/alerting/notifiers/opsgenie.go index 863b4f1c286..5d8b15160c4 100644 --- a/pkg/services/alerting/notifiers/opsgenie.go +++ b/pkg/services/alerting/notifiers/opsgenie.go @@ -72,10 +72,6 @@ type OpsGenieNotifier struct { log log.Logger } -func (this *OpsGenieNotifier) ShouldNotify(context *alerting.EvalContext) bool { - return defaultShouldNotify(context) -} - func (this *OpsGenieNotifier) Notify(evalContext *alerting.EvalContext) error { var err error diff --git a/pkg/services/alerting/notifiers/pagerduty.go b/pkg/services/alerting/notifiers/pagerduty.go index 6013648d9dd..58484051432 100644 --- a/pkg/services/alerting/notifiers/pagerduty.go +++ b/pkg/services/alerting/notifiers/pagerduty.go @@ -65,10 +65,6 @@ type PagerdutyNotifier struct { log log.Logger } -func (this *PagerdutyNotifier) ShouldNotify(context *alerting.EvalContext) bool { - return defaultShouldNotify(context) -} - func (this *PagerdutyNotifier) Notify(evalContext *alerting.EvalContext) error { if evalContext.Rule.State == m.AlertStateOK && !this.AutoResolve { diff --git a/pkg/services/alerting/notifiers/pushover.go b/pkg/services/alerting/notifiers/pushover.go index 067c02a4a5d..cbe9e16801a 100644 --- a/pkg/services/alerting/notifiers/pushover.go +++ b/pkg/services/alerting/notifiers/pushover.go @@ -123,10 +123,6 @@ type PushoverNotifier struct { log log.Logger } -func (this *PushoverNotifier) ShouldNotify(context *alerting.EvalContext) bool { - return defaultShouldNotify(context) -} - func (this *PushoverNotifier) Notify(evalContext *alerting.EvalContext) error { ruleUrl, err := evalContext.GetRuleUrl() if err != nil { diff --git a/pkg/services/alerting/notifiers/sensu.go b/pkg/services/alerting/notifiers/sensu.go index 7a34d51b493..9f77801d458 100644 --- a/pkg/services/alerting/notifiers/sensu.go +++ b/pkg/services/alerting/notifiers/sensu.go @@ -71,10 +71,6 @@ type SensuNotifier struct { log log.Logger } -func (this *SensuNotifier) ShouldNotify(context *alerting.EvalContext) bool { - return defaultShouldNotify(context) -} - func (this *SensuNotifier) Notify(evalContext *alerting.EvalContext) error { this.log.Info("Sending sensu result") diff --git a/pkg/services/alerting/notifiers/slack.go b/pkg/services/alerting/notifiers/slack.go index c5bb9344e30..e051a71740a 100644 --- a/pkg/services/alerting/notifiers/slack.go +++ b/pkg/services/alerting/notifiers/slack.go @@ -98,10 +98,6 @@ type SlackNotifier struct { log log.Logger } -func (this *SlackNotifier) ShouldNotify(context *alerting.EvalContext) bool { - return defaultShouldNotify(context) -} - func (this *SlackNotifier) Notify(evalContext *alerting.EvalContext) error { this.log.Info("Executing slack notification", "ruleId", evalContext.Rule.Id, "notification", this.Name) diff --git a/pkg/services/alerting/notifiers/teams.go b/pkg/services/alerting/notifiers/teams.go index 851e5a01c75..9a9e93dbc47 100644 --- a/pkg/services/alerting/notifiers/teams.go +++ b/pkg/services/alerting/notifiers/teams.go @@ -47,10 +47,6 @@ type TeamsNotifier struct { log log.Logger } -func (this *TeamsNotifier) ShouldNotify(context *alerting.EvalContext) bool { - return defaultShouldNotify(context) -} - func (this *TeamsNotifier) Notify(evalContext *alerting.EvalContext) error { this.log.Info("Executing teams notification", "ruleId", evalContext.Rule.Id, "notification", this.Name) diff --git a/pkg/services/alerting/notifiers/telegram.go b/pkg/services/alerting/notifiers/telegram.go index 88100afe7a1..5cbdad60906 100644 --- a/pkg/services/alerting/notifiers/telegram.go +++ b/pkg/services/alerting/notifiers/telegram.go @@ -208,6 +208,7 @@ func generateImageCaption(evalContext *alerting.EvalContext, ruleUrl string, met return message } + func appendIfPossible(message string, extra string, sizeLimit int) string { if len(extra)+len(message) <= sizeLimit { return message + extra @@ -216,10 +217,6 @@ func appendIfPossible(message string, extra string, sizeLimit int) string { return message } -func (this *TelegramNotifier) ShouldNotify(context *alerting.EvalContext) bool { - return defaultShouldNotify(context) -} - func (this *TelegramNotifier) Notify(evalContext *alerting.EvalContext) error { var cmd *m.SendWebhookSync if evalContext.ImagePublicUrl == "" && this.UploadImage == true { diff --git a/pkg/services/alerting/notifiers/threema.go b/pkg/services/alerting/notifiers/threema.go index b8455dcfbfd..e4ffffc9108 100644 --- a/pkg/services/alerting/notifiers/threema.go +++ b/pkg/services/alerting/notifiers/threema.go @@ -114,10 +114,6 @@ func NewThreemaNotifier(model *m.AlertNotification) (alerting.Notifier, error) { }, nil } -func (this *ThreemaNotifier) ShouldNotify(context *alerting.EvalContext) bool { - return defaultShouldNotify(context) -} - func (notifier *ThreemaNotifier) Notify(evalContext *alerting.EvalContext) error { notifier.log.Info("Sending alert notification from", "threema_id", notifier.GatewayID) notifier.log.Info("Sending alert notification to", "threema_id", notifier.RecipientID) diff --git a/pkg/services/alerting/notifiers/victorops.go b/pkg/services/alerting/notifiers/victorops.go index a2b770dfd02..4b4db553cde 100644 --- a/pkg/services/alerting/notifiers/victorops.go +++ b/pkg/services/alerting/notifiers/victorops.go @@ -68,10 +68,6 @@ type VictoropsNotifier struct { log log.Logger } -func (this *VictoropsNotifier) ShouldNotify(context *alerting.EvalContext) bool { - return defaultShouldNotify(context) -} - // Notify sends notification to Victorops via POST to URL endpoint func (this *VictoropsNotifier) Notify(evalContext *alerting.EvalContext) error { this.log.Info("Executing victorops notification", "ruleId", evalContext.Rule.Id, "notification", this.Name) diff --git a/pkg/services/alerting/notifiers/webhook.go b/pkg/services/alerting/notifiers/webhook.go index d2d6ec636b7..4c97ed2b75e 100644 --- a/pkg/services/alerting/notifiers/webhook.go +++ b/pkg/services/alerting/notifiers/webhook.go @@ -65,10 +65,6 @@ type WebhookNotifier struct { log log.Logger } -func (this *WebhookNotifier) ShouldNotify(context *alerting.EvalContext) bool { - return defaultShouldNotify(context) -} - func (this *WebhookNotifier) Notify(evalContext *alerting.EvalContext) error { this.log.Info("Sending webhook")