瀏覽代碼

tech(notifiers): improve logging for notifications

ref #6228
bergquist 9 年之前
父節點
當前提交
3162e680c2

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

@@ -16,6 +16,9 @@ type Notifier interface {
 	GetType() string
 	NeedsImage() bool
 	PassesFilter(rule *Rule) bool
+
+	GetNotifierId() int64
+	GetIsDefault() bool
 }
 
 type Condition interface {

+ 11 - 3
pkg/services/alerting/notifier.go

@@ -35,14 +35,22 @@ func (n *RootNotifier) PassesFilter(rule *Rule) bool {
 	return false
 }
 
-func (n *RootNotifier) Notify(context *EvalContext) error {
-	n.log.Info("Sending notifications for", "ruleId", context.Rule.Id)
+func (n *RootNotifier) GetNotifierId() int64 {
+	return 0
+}
 
+func (n *RootNotifier) GetIsDefault() bool {
+	return false
+}
+
+func (n *RootNotifier) Notify(context *EvalContext) error {
 	notifiers, err := n.getNotifiers(context.Rule.OrgId, context.Rule.Notifications, context)
 	if err != nil {
 		return err
 	}
 
+	n.log.Info("Sending notifications for", "ruleId", context.Rule.Id, "Amount to send", len(notifiers))
+
 	if len(notifiers) == 0 {
 		return nil
 	}
@@ -60,7 +68,7 @@ func (n *RootNotifier) sendNotifications(context *EvalContext, notifiers []Notif
 	g, _ := errgroup.WithContext(context.Ctx)
 
 	for _, notifier := range notifiers {
-		n.log.Info("Sending notification", "firing", context.Firing, "type", notifier.GetType())
+		n.log.Info("Sending notification", "type", notifier.GetType(), "id", notifier.GetNotifierId(), "isDefault", notifier.GetIsDefault())
 		g.Go(func() error { return notifier.Notify(context) })
 	}
 

+ 8 - 0
pkg/services/alerting/notifier_test.go

@@ -22,6 +22,14 @@ func (fn *FakeNotifier) NeedsImage() bool {
 	return true
 }
 
+func (n *FakeNotifier) GetNotifierId() int64 {
+	return 0
+}
+
+func (n *FakeNotifier) GetIsDefault() bool {
+	return false
+}
+
 func (fn *FakeNotifier) Notify(alertResult *EvalContext) error { return nil }
 
 func (fn *FakeNotifier) PassesFilter(rule *Rule) bool {

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

@@ -6,13 +6,19 @@ import (
 )
 
 type NotifierBase struct {
-	Name string
-	Type string
+	Name     string
+	Type     string
+	Id       int64
+	IsDeault bool
 }
 
-func NewNotifierBase(name, notifierType string, model *simplejson.Json) NotifierBase {
-	base := NotifierBase{Name: name, Type: notifierType}
-	return base
+func NewNotifierBase(id int64, isDefault bool, name, notifierType string, model *simplejson.Json) NotifierBase {
+	return NotifierBase{
+		Id:       id,
+		Name:     name,
+		IsDeault: isDefault,
+		Type:     notifierType,
+	}
 }
 
 func (n *NotifierBase) PassesFilter(rule *alerting.Rule) bool {
@@ -26,3 +32,11 @@ func (n *NotifierBase) GetType() string {
 func (n *NotifierBase) NeedsImage() bool {
 	return true
 }
+
+func (n *NotifierBase) GetNotifierId() int64 {
+	return n.Id
+}
+
+func (n *NotifierBase) GetIsDefault() bool {
+	return n.IsDeault
+}

+ 1 - 1
pkg/services/alerting/notifiers/email.go

@@ -29,7 +29,7 @@ func NewEmailNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
 	}
 
 	return &EmailNotifier{
-		NotifierBase: NewNotifierBase(model.Name, model.Type, model.Settings),
+		NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
 		Addresses:    strings.Split(addressesString, "\n"),
 		log:          log.New("alerting.notifier.email"),
 	}, nil

+ 1 - 1
pkg/services/alerting/notifiers/slack.go

@@ -23,7 +23,7 @@ func NewSlackNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
 	}
 
 	return &SlackNotifier{
-		NotifierBase: NewNotifierBase(model.Name, model.Type, model.Settings),
+		NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
 		Url:          url,
 		log:          log.New("alerting.notifier.slack"),
 	}, nil

+ 1 - 1
pkg/services/alerting/notifiers/webhook.go

@@ -20,7 +20,7 @@ func NewWebHookNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
 	}
 
 	return &WebhookNotifier{
-		NotifierBase: NewNotifierBase(model.Name, model.Type, model.Settings),
+		NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
 		Url:          url,
 		User:         model.Settings.Get("user").MustString(),
 		Password:     model.Settings.Get("password").MustString(),