|
|
@@ -21,37 +21,25 @@ type NotifierPlugin struct {
|
|
|
Factory NotifierFactory `json:"-"`
|
|
|
}
|
|
|
|
|
|
-type RootNotifier struct {
|
|
|
- log log.Logger
|
|
|
-}
|
|
|
-
|
|
|
-func NewRootNotifier() *RootNotifier {
|
|
|
- return &RootNotifier{
|
|
|
- log: log.New("alerting.notifier"),
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-func (n *RootNotifier) GetType() string {
|
|
|
- return "root"
|
|
|
+type NotificationService interface {
|
|
|
+ Send(context *EvalContext) error
|
|
|
}
|
|
|
|
|
|
-func (n *RootNotifier) NeedsImage() bool {
|
|
|
- return false
|
|
|
+func NewNotificationService() NotificationService {
|
|
|
+ return newNotificationService()
|
|
|
}
|
|
|
|
|
|
-func (n *RootNotifier) PassesFilter(rule *Rule) bool {
|
|
|
- return false
|
|
|
-}
|
|
|
-
|
|
|
-func (n *RootNotifier) GetNotifierId() int64 {
|
|
|
- return 0
|
|
|
+type notificationService struct {
|
|
|
+ log log.Logger
|
|
|
}
|
|
|
|
|
|
-func (n *RootNotifier) GetIsDefault() bool {
|
|
|
- return false
|
|
|
+func newNotificationService() *notificationService {
|
|
|
+ return ¬ificationService{
|
|
|
+ log: log.New("alerting.notifier"),
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-func (n *RootNotifier) Notify(context *EvalContext) error {
|
|
|
+func (n *notificationService) Send(context *EvalContext) error {
|
|
|
notifiers, err := n.getNotifiers(context.Rule.OrgId, context.Rule.Notifications, context)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
@@ -63,14 +51,16 @@ func (n *RootNotifier) Notify(context *EvalContext) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
- if err = n.uploadImage(context); err != nil {
|
|
|
- n.log.Error("Failed to upload alert panel image.", "error", err)
|
|
|
+ if notifiers.ShouldUploadImage() {
|
|
|
+ if err = n.uploadImage(context); err != nil {
|
|
|
+ n.log.Error("Failed to upload alert panel image.", "error", err)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return n.sendNotifications(context, notifiers)
|
|
|
}
|
|
|
|
|
|
-func (n *RootNotifier) sendNotifications(context *EvalContext, notifiers []Notifier) error {
|
|
|
+func (n *notificationService) sendNotifications(context *EvalContext, notifiers []Notifier) error {
|
|
|
g, _ := errgroup.WithContext(context.Ctx)
|
|
|
|
|
|
for _, notifier := range notifiers {
|
|
|
@@ -82,7 +72,7 @@ func (n *RootNotifier) sendNotifications(context *EvalContext, notifiers []Notif
|
|
|
return g.Wait()
|
|
|
}
|
|
|
|
|
|
-func (n *RootNotifier) uploadImage(context *EvalContext) (err error) {
|
|
|
+func (n *notificationService) uploadImage(context *EvalContext) (err error) {
|
|
|
uploader, err := imguploader.NewImageUploader()
|
|
|
if err != nil {
|
|
|
return err
|
|
|
@@ -116,7 +106,7 @@ func (n *RootNotifier) uploadImage(context *EvalContext) (err error) {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (n *RootNotifier) getNotifiers(orgId int64, notificationIds []int64, context *EvalContext) ([]Notifier, error) {
|
|
|
+func (n *notificationService) getNotifiers(orgId int64, notificationIds []int64, context *EvalContext) (NotifierSlice, error) {
|
|
|
query := &m.GetAlertNotificationsToSendQuery{OrgId: orgId, Ids: notificationIds}
|
|
|
|
|
|
if err := bus.Dispatch(query); err != nil {
|
|
|
@@ -137,7 +127,7 @@ func (n *RootNotifier) getNotifiers(orgId int64, notificationIds []int64, contex
|
|
|
return result, nil
|
|
|
}
|
|
|
|
|
|
-func (n *RootNotifier) createNotifierFor(model *m.AlertNotification) (Notifier, error) {
|
|
|
+func (n *notificationService) createNotifierFor(model *m.AlertNotification) (Notifier, error) {
|
|
|
notifierPlugin, found := notifierFactories[model.Type]
|
|
|
if !found {
|
|
|
return nil, errors.New("Unsupported notification type")
|