Explorar o código

add channel option to disable the resolved alert (OK Message) that is sent when condition returns to normal.

Dave Waters %!s(int64=7) %!d(string=hai) anos
pai
achega
6376154b16

+ 26 - 23
pkg/api/dtos/alerting.go

@@ -49,28 +49,30 @@ func formatShort(interval time.Duration) string {
 
 func NewAlertNotification(notification *models.AlertNotification) *AlertNotification {
 	return &AlertNotification{
-		Id:           notification.Id,
-		Name:         notification.Name,
-		Type:         notification.Type,
-		IsDefault:    notification.IsDefault,
-		Created:      notification.Created,
-		Updated:      notification.Updated,
-		Frequency:    formatShort(notification.Frequency),
-		SendReminder: notification.SendReminder,
-		Settings:     notification.Settings,
+		Id:           			   notification.Id,
+		Name:         			   notification.Name,
+		Type:         			   notification.Type,
+		IsDefault:    			   notification.IsDefault,
+		Created:      			   notification.Created,
+		Updated:      			   notification.Updated,
+		Frequency:    			   formatShort(notification.Frequency),
+		SendReminder: 			   notification.SendReminder,
+		DisableResolvedMessage:    notification.DisableResolvedMessage,
+		Settings:     			   notification.Settings,
 	}
 }
 
 type AlertNotification struct {
-	Id           int64            `json:"id"`
-	Name         string           `json:"name"`
-	Type         string           `json:"type"`
-	IsDefault    bool             `json:"isDefault"`
-	SendReminder bool             `json:"sendReminder"`
-	Frequency    string           `json:"frequency"`
-	Created      time.Time        `json:"created"`
-	Updated      time.Time        `json:"updated"`
-	Settings     *simplejson.Json `json:"settings"`
+	Id           			int64            `json:"id"`
+	Name         			string           `json:"name"`
+	Type         			string           `json:"type"`
+	IsDefault    			bool             `json:"isDefault"`
+	SendReminder 			bool             `json:"sendReminder"`
+	DisableResolvedMessage  bool             `json:"disableResolvedMessage"`
+	Frequency    			string           `json:"frequency"`
+	Created      			time.Time        `json:"created"`
+	Updated      			time.Time        `json:"updated"`
+	Settings     			*simplejson.Json `json:"settings"`
 }
 
 type AlertTestCommand struct {
@@ -100,11 +102,12 @@ type EvalMatch struct {
 }
 
 type NotificationTestCommand struct {
-	Name         string           `json:"name"`
-	Type         string           `json:"type"`
-	SendReminder bool             `json:"sendReminder"`
-	Frequency    string           `json:"frequency"`
-	Settings     *simplejson.Json `json:"settings"`
+	Name         			string           `json:"name"`
+	Type         			string           `json:"type"`
+	SendReminder 			bool             `json:"sendReminder"`
+	DisableResolvedMessage  bool             `json:"disableResolvedMessage"`
+	Frequency    			string           `json:"frequency"`
+	Settings     			*simplejson.Json `json:"settings"`
 }
 
 type PauseAlertCommand struct {

+ 26 - 23
pkg/models/alert_notifications.go

@@ -23,38 +23,41 @@ var (
 )
 
 type AlertNotification struct {
-	Id           int64            `json:"id"`
-	OrgId        int64            `json:"-"`
-	Name         string           `json:"name"`
-	Type         string           `json:"type"`
-	SendReminder bool             `json:"sendReminder"`
-	Frequency    time.Duration    `json:"frequency"`
-	IsDefault    bool             `json:"isDefault"`
-	Settings     *simplejson.Json `json:"settings"`
-	Created      time.Time        `json:"created"`
-	Updated      time.Time        `json:"updated"`
+	Id           			int64            `json:"id"`
+	OrgId        			int64            `json:"-"`
+	Name         			string           `json:"name"`
+	Type         			string           `json:"type"`
+	SendReminder 			bool             `json:"sendReminder"`
+	DisableResolvedMessage  bool             `json:"disableResolvedMessage"`
+	Frequency    			time.Duration    `json:"frequency"`
+	IsDefault    			bool             `json:"isDefault"`
+	Settings     			*simplejson.Json `json:"settings"`
+	Created      			time.Time        `json:"created"`
+	Updated      			time.Time        `json:"updated"`
 }
 
 type CreateAlertNotificationCommand struct {
-	Name         string           `json:"name"  binding:"Required"`
-	Type         string           `json:"type"  binding:"Required"`
-	SendReminder bool             `json:"sendReminder"`
-	Frequency    string           `json:"frequency"`
-	IsDefault    bool             `json:"isDefault"`
-	Settings     *simplejson.Json `json:"settings"`
+	Name         		   string           `json:"name"  binding:"Required"`
+	Type         		   string           `json:"type"  binding:"Required"`
+	SendReminder 		   bool             `json:"sendReminder"`
+	DisableResolvedMessage bool             `json:"disableResolvedMessage"`
+	Frequency    		   string           `json:"frequency"`
+	IsDefault    		   bool             `json:"isDefault"`
+	Settings     		   *simplejson.Json `json:"settings"`
 
 	OrgId  int64 `json:"-"`
 	Result *AlertNotification
 }
 
 type UpdateAlertNotificationCommand struct {
-	Id           int64            `json:"id"  binding:"Required"`
-	Name         string           `json:"name"  binding:"Required"`
-	Type         string           `json:"type"  binding:"Required"`
-	SendReminder bool             `json:"sendReminder"`
-	Frequency    string           `json:"frequency"`
-	IsDefault    bool             `json:"isDefault"`
-	Settings     *simplejson.Json `json:"settings"  binding:"Required"`
+	Id           			int64            `json:"id"  binding:"Required"`
+	Name         			string           `json:"name"  binding:"Required"`
+	Type         			string           `json:"type"  binding:"Required"`
+	SendReminder 			bool             `json:"sendReminder"`
+	DisableResolvedMessage  bool             `json:"disableResolvedMessage"`
+	Frequency    			string           `json:"frequency"`
+	IsDefault    			bool             `json:"isDefault"`
+	Settings     			*simplejson.Json `json:"settings"  binding:"Required"`
 
 	OrgId  int64 `json:"-"`
 	Result *AlertNotification

+ 1 - 0
pkg/services/alerting/interfaces.go

@@ -27,6 +27,7 @@ type Notifier interface {
 	GetNotifierId() int64
 	GetIsDefault() bool
 	GetSendReminder() bool
+	GetDisableResolvedMessage() bool
 	GetFrequency() time.Duration
 }
 

+ 19 - 8
pkg/services/alerting/notifiers/base.go

@@ -21,6 +21,7 @@ type NotifierBase struct {
 	IsDeault     bool
 	UploadImage  bool
 	SendReminder bool
+	DisableResolvedMessage    bool
 	Frequency    time.Duration
 
 	log log.Logger
@@ -34,14 +35,15 @@ func NewNotifierBase(model *models.AlertNotification) NotifierBase {
 	}
 
 	return NotifierBase{
-		Id:           model.Id,
-		Name:         model.Name,
-		IsDeault:     model.IsDefault,
-		Type:         model.Type,
-		UploadImage:  uploadImage,
-		SendReminder: model.SendReminder,
-		Frequency:    model.Frequency,
-		log:          log.New("alerting.notifier." + model.Name),
+		Id:           			model.Id,
+		Name:         			model.Name,
+		IsDeault:     			model.IsDefault,
+		Type:         			model.Type,
+		UploadImage:  			uploadImage,
+		SendReminder: 			model.SendReminder,
+		DisableResolvedMessage: model.DisableResolvedMessage,
+		Frequency:    			model.Frequency,
+		log:          			log.New("alerting.notifier." + model.Name),
 	}
 }
 
@@ -83,6 +85,11 @@ func (n *NotifierBase) ShouldNotify(ctx context.Context, context *alerting.EvalC
 		}
 	}
 
+	// Do not notify when state is OK if DisableResolvedMessage is set to true
+	if context.Rule.State == models.AlertStateOK && n.DisableResolvedMessage {
+		return false
+	}
+
 	return true
 }
 
@@ -106,6 +113,10 @@ func (n *NotifierBase) GetSendReminder() bool {
 	return n.SendReminder
 }
 
+func (n *NotifierBase) GetDisableResolvedMessage() bool {
+	return n.DisableResolvedMessage
+}
+
 func (n *NotifierBase) GetFrequency() time.Duration {
 	return n.Frequency
 }

+ 13 - 9
pkg/services/sqlstore/alert_notification.go

@@ -66,6 +66,7 @@ func GetAlertNotificationsToSend(query *m.GetAlertNotificationsToSendQuery) erro
 										alert_notification.updated,
 										alert_notification.settings,
 										alert_notification.is_default,
+										alert_notification.disable_resolved_message,
 										alert_notification.send_reminder,
 										alert_notification.frequency
 										FROM alert_notification
@@ -106,6 +107,7 @@ func getAlertNotificationInternal(query *m.GetAlertNotificationsQuery, sess *DBS
 										alert_notification.updated,
 										alert_notification.settings,
 										alert_notification.is_default,
+										alert_notification.disable_resolved_message,
 										alert_notification.send_reminder,
 										alert_notification.frequency
 										FROM alert_notification
@@ -166,15 +168,16 @@ func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error
 		}
 
 		alertNotification := &m.AlertNotification{
-			OrgId:        cmd.OrgId,
-			Name:         cmd.Name,
-			Type:         cmd.Type,
-			Settings:     cmd.Settings,
-			SendReminder: cmd.SendReminder,
-			Frequency:    frequency,
-			Created:      time.Now(),
-			Updated:      time.Now(),
-			IsDefault:    cmd.IsDefault,
+			OrgId:        			cmd.OrgId,
+			Name:         			cmd.Name,
+			Type:         			cmd.Type,
+			Settings:     			cmd.Settings,
+			SendReminder: 			cmd.SendReminder,
+			DisableResolvedMessage: cmd.DisableResolvedMessage,
+			Frequency:    			frequency,
+			Created:      			time.Now(),
+			Updated:      			time.Now(),
+			IsDefault:    			cmd.IsDefault,
 		}
 
 		if _, err = sess.MustCols("send_reminder").Insert(alertNotification); err != nil {
@@ -210,6 +213,7 @@ func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error {
 		current.Type = cmd.Type
 		current.IsDefault = cmd.IsDefault
 		current.SendReminder = cmd.SendReminder
+		current.DisableResolvedMessage = cmd.DisableResolvedMessage
 
 		if current.SendReminder {
 			if cmd.Frequency == "" {

+ 3 - 0
pkg/services/sqlstore/migrations/alert_mig.go

@@ -71,6 +71,9 @@ func addAlertMigrations(mg *Migrator) {
 	mg.AddMigration("Add column send_reminder", NewAddColumnMigration(alert_notification, &Column{
 		Name: "send_reminder", Type: DB_Bool, Nullable: true, Default: "0",
 	}))
+	mg.AddMigration("Add column disable_resolved_message", NewAddColumnMigration(alert_notification, &Column{
+		Name: "disable_resolved_message", Type: DB_Bool, Nullable: false, Default: "1",
+	}))
 
 	mg.AddMigration("add index alert_notification org_id & name", NewAddIndexMigration(alert_notification, alert_notification.Indices[0]))
 

+ 1 - 0
public/app/features/alerting/NotificationsEditCtrl.ts

@@ -12,6 +12,7 @@ export class AlertNotificationEditCtrl {
   defaults: any = {
     type: 'email',
     sendReminder: false,
+    disableResolvedMessage: false,
     frequency: '15m',
     settings: {
       httpMethod: 'POST',

+ 7 - 0
public/app/features/alerting/partials/notification_edit.html

@@ -32,6 +32,13 @@
           checked="ctrl.model.settings.uploadImage"
           tooltip="Captures an image and include it in the notification">
       </gf-form-switch>
+      <gf-form-switch
+          class="gf-form"
+          label="Disable OK Alert"
+          label-class="width-12"
+          checked="ctrl.model.disableResolvedMessage"
+          tooltip="Disable the OK Alert that is sent when alerting state returns to false">
+      </gf-form-switch>
       <gf-form-switch
           class="gf-form"
           label="Send reminders"