diff --git a/pkg/services/alerting/notifiers/pagerduty.go b/pkg/services/alerting/notifiers/pagerduty.go index a90e7635a04..f9b0af39602 100644 --- a/pkg/services/alerting/notifiers/pagerduty.go +++ b/pkg/services/alerting/notifiers/pagerduty.go @@ -19,54 +19,37 @@ func NewPagerdutyNotifier(model *m.AlertNotification) (alerting.Notifier, error) return nil, alerting.ValidationError{Reason: "Could not find integration key property in settings"} } - alertingStates := make([]m.AlertStateType, 0) - alertingStates = append(alertingStates, m.AlertStateAlerting) - if model.Settings.Get("alertOnExecError").MustBool() { - alertingStates = append(alertingStates, m.AlertStateExecError) - } - if model.Settings.Get("alertOnNoData").MustBool() { - alertingStates = append(alertingStates, m.AlertStateNoData) - } - return &PagerdutyNotifier{ - NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings), + NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings), Key: key, - AlertingStates: alertingStates, + AlertOnExecError: model.Settings.Get("alertOnExecError").MustBool(), log: log.New("alerting.notifier.pagerduty"), }, nil } type PagerdutyNotifier struct { NotifierBase - Key string - AlertingStates []m.AlertStateType - log log.Logger + Key string + AlertOnExecError bool + log log.Logger } func (this *PagerdutyNotifier) Notify(evalContext *alerting.EvalContext) error { this.log.Info("Notifying Pagerduty") metrics.M_Alerting_Notification_Sent_PagerDuty.Inc(1) - shouldNotify := false - - for _, state := range this.AlertingStates { - if evalContext.Rule.State == state { - shouldNotify = true - break - } - } - - if shouldNotify { + if (evalContext.Rule.State == m.AlertStateAlerting) || + ((this.AlertOnExecError) && (evalContext.Rule.State == m.AlertStateExecError)) { // Pagerduty Events API URL pgEventsUrl := "https://events.pagerduty.com/generic/2010-04-15/create_event.json" bodyJSON := simplejson.New() bodyJSON.Set("service_key", this.Key) - bodyJSON.Set("description", evalContext.Rule.Name + "-" + evalContext.Rule.Message) + bodyJSON.Set("description", evalContext.Rule.Name+"-"+evalContext.Rule.Message) bodyJSON.Set("client", "Grafana") bodyJSON.Set("event_type", "trigger") - + ruleUrl, err := evalContext.GetRuleUrl() if err != nil { this.log.Error("Failed get rule link", "error", err) diff --git a/pkg/services/alerting/notifiers/pagerduty_test.go b/pkg/services/alerting/notifiers/pagerduty_test.go index d75126527d5..fb20450d930 100644 --- a/pkg/services/alerting/notifiers/pagerduty_test.go +++ b/pkg/services/alerting/notifiers/pagerduty_test.go @@ -26,59 +26,10 @@ func TestPagerdutyNotifier(t *testing.T) { So(err, ShouldNotBeNil) }) - Convey("settings with only integrationKey should contain AlertStateAlerting", func() { - json := ` - { - "integrationKey": "abcdefgh0123456789" - }` - - settingsJSON, _ := simplejson.NewJson([]byte(json)) - model := &m.AlertNotification{ - Name: "pagerduty_testing", - Type: "pagerduty", - Settings: settingsJSON, - } - - not, err := NewPagerdutyNotifier(model) - pagerdutyNotifier := not.(*PagerdutyNotifier) - - So(err, ShouldBeNil) - So(pagerdutyNotifier.Name, ShouldEqual, "pagerduty_testing") - So(pagerdutyNotifier.Type, ShouldEqual, "pagerduty") - So(pagerdutyNotifier.Key, ShouldEqual, "abcdefgh0123456789") - So(pagerdutyNotifier.AlertingStates, ShouldContain, m.AlertStateAlerting) - }) - - Convey("settings with alertOnNoData should contain AlertStateNoData too", func() { + Convey("settings with alertOnExecError should trigger incident", func() { json := ` { "integrationKey": "abcdefgh0123456789", - "alertOnNoData": true - }` - - settingsJSON, _ := simplejson.NewJson([]byte(json)) - model := &m.AlertNotification{ - Name: "pagerduty_testing", - Type: "pagerduty", - Settings: settingsJSON, - } - - not, err := NewPagerdutyNotifier(model) - pagerdutyNotifier := not.(*PagerdutyNotifier) - - So(err, ShouldBeNil) - So(pagerdutyNotifier.Name, ShouldEqual, "pagerduty_testing") - So(pagerdutyNotifier.Type, ShouldEqual, "pagerduty") - So(pagerdutyNotifier.Key, ShouldEqual, "abcdefgh0123456789") - So(pagerdutyNotifier.AlertingStates, ShouldContain, m.AlertStateNoData) - So(pagerdutyNotifier.AlertingStates, ShouldContain, m.AlertStateAlerting) - }) - - Convey("settings with alertOnNoData, alertOnExecError should contain both", func() { - json := ` - { - "integrationKey": "abcdefgh0123456789", - "alertOnNoData": true, "alertOnExecError": true }` @@ -96,9 +47,7 @@ func TestPagerdutyNotifier(t *testing.T) { So(pagerdutyNotifier.Name, ShouldEqual, "pagerduty_testing") So(pagerdutyNotifier.Type, ShouldEqual, "pagerduty") So(pagerdutyNotifier.Key, ShouldEqual, "abcdefgh0123456789") - So(pagerdutyNotifier.AlertingStates, ShouldContain, m.AlertStateNoData) - So(pagerdutyNotifier.AlertingStates, ShouldContain, m.AlertStateAlerting) - So(pagerdutyNotifier.AlertingStates, ShouldContain, m.AlertStateExecError) + So(pagerdutyNotifier.AlertOnExecError, ShouldContain, true) }) }) diff --git a/public/app/features/alerting/partials/notification_edit.html b/public/app/features/alerting/partials/notification_edit.html index 379eca2e168..904fd9df0e5 100644 --- a/public/app/features/alerting/partials/notification_edit.html +++ b/public/app/features/alerting/partials/notification_edit.html @@ -103,22 +103,13 @@ Integration Key -