Commit bd010289 by bergquist

alerting: images in alert notifications is now optional

its now possible to turn of image uploading in alert notifications
for those who operate on very sensitive data.

closes #7419
parent f14e2561
...@@ -21,6 +21,18 @@ type Notifier interface { ...@@ -21,6 +21,18 @@ type Notifier interface {
GetIsDefault() bool GetIsDefault() bool
} }
type NotifierSlice []Notifier
func (notifiers NotifierSlice) ShouldUploadImage() bool {
for _, notifier := range notifiers {
if notifier.NeedsImage() {
return true
}
}
return false
}
type ConditionResult struct { type ConditionResult struct {
Firing bool Firing bool
NoDataFound bool NoDataFound bool
......
...@@ -21,37 +21,25 @@ type NotifierPlugin struct { ...@@ -21,37 +21,25 @@ type NotifierPlugin struct {
Factory NotifierFactory `json:"-"` Factory NotifierFactory `json:"-"`
} }
type RootNotifier struct { type NotificationService interface {
log log.Logger Send(context *EvalContext) error
}
func NewRootNotifier() *RootNotifier {
return &RootNotifier{
log: log.New("alerting.notifier"),
}
}
func (n *RootNotifier) GetType() string {
return "root"
} }
func (n *RootNotifier) NeedsImage() bool { func NewNotificationService() NotificationService {
return false return newNotificationService()
} }
func (n *RootNotifier) PassesFilter(rule *Rule) bool { type notificationService struct {
return false log log.Logger
}
func (n *RootNotifier) GetNotifierId() int64 {
return 0
} }
func (n *RootNotifier) GetIsDefault() bool { func newNotificationService() *notificationService {
return false return &notificationService{
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) notifiers, err := n.getNotifiers(context.Rule.OrgId, context.Rule.Notifications, context)
if err != nil { if err != nil {
return err return err
...@@ -63,14 +51,16 @@ func (n *RootNotifier) Notify(context *EvalContext) error { ...@@ -63,14 +51,16 @@ func (n *RootNotifier) Notify(context *EvalContext) error {
return nil return nil
} }
if err = n.uploadImage(context); err != nil { if notifiers.ShouldUploadImage() {
n.log.Error("Failed to upload alert panel image.", "error", err) if err = n.uploadImage(context); err != nil {
n.log.Error("Failed to upload alert panel image.", "error", err)
}
} }
return n.sendNotifications(context, notifiers) 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) g, _ := errgroup.WithContext(context.Ctx)
for _, notifier := range notifiers { for _, notifier := range notifiers {
...@@ -82,7 +72,7 @@ func (n *RootNotifier) sendNotifications(context *EvalContext, notifiers []Notif ...@@ -82,7 +72,7 @@ func (n *RootNotifier) sendNotifications(context *EvalContext, notifiers []Notif
return g.Wait() return g.Wait()
} }
func (n *RootNotifier) uploadImage(context *EvalContext) (err error) { func (n *notificationService) uploadImage(context *EvalContext) (err error) {
uploader, err := imguploader.NewImageUploader() uploader, err := imguploader.NewImageUploader()
if err != nil { if err != nil {
return err return err
...@@ -116,7 +106,7 @@ func (n *RootNotifier) uploadImage(context *EvalContext) (err error) { ...@@ -116,7 +106,7 @@ func (n *RootNotifier) uploadImage(context *EvalContext) (err error) {
return nil 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} query := &m.GetAlertNotificationsToSendQuery{OrgId: orgId, Ids: notificationIds}
if err := bus.Dispatch(query); err != nil { if err := bus.Dispatch(query); err != nil {
...@@ -137,7 +127,7 @@ func (n *RootNotifier) getNotifiers(orgId int64, notificationIds []int64, contex ...@@ -137,7 +127,7 @@ func (n *RootNotifier) getNotifiers(orgId int64, notificationIds []int64, contex
return result, nil 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] notifierPlugin, found := notifierFactories[model.Type]
if !found { if !found {
return nil, errors.New("Unsupported notification type") return nil, errors.New("Unsupported notification type")
......
...@@ -6,18 +6,22 @@ import ( ...@@ -6,18 +6,22 @@ import (
) )
type NotifierBase struct { type NotifierBase struct {
Name string Name string
Type string Type string
Id int64 Id int64
IsDeault bool IsDeault bool
UploadImage bool
} }
func NewNotifierBase(id int64, isDefault bool, name, notifierType string, model *simplejson.Json) NotifierBase { func NewNotifierBase(id int64, isDefault bool, name, notifierType string, model *simplejson.Json) NotifierBase {
uploadImage := model.Get("uploadImage").MustBool(true)
return NotifierBase{ return NotifierBase{
Id: id, Id: id,
Name: name, Name: name,
IsDeault: isDefault, IsDeault: isDefault,
Type: notifierType, Type: notifierType,
UploadImage: uploadImage,
} }
} }
...@@ -30,7 +34,7 @@ func (n *NotifierBase) GetType() string { ...@@ -30,7 +34,7 @@ func (n *NotifierBase) GetType() string {
} }
func (n *NotifierBase) NeedsImage() bool { func (n *NotifierBase) NeedsImage() bool {
return true return n.UploadImage
} }
func (n *NotifierBase) GetNotifierId() int64 { func (n *NotifierBase) GetNotifierId() int64 {
......
...@@ -16,14 +16,14 @@ type ResultHandler interface { ...@@ -16,14 +16,14 @@ type ResultHandler interface {
} }
type DefaultResultHandler struct { type DefaultResultHandler struct {
notifier Notifier notifier NotificationService
log log.Logger log log.Logger
} }
func NewResultHandler() *DefaultResultHandler { func NewResultHandler() *DefaultResultHandler {
return &DefaultResultHandler{ return &DefaultResultHandler{
log: log.New("alerting.resultHandler"), log: log.New("alerting.resultHandler"),
notifier: NewRootNotifier(), notifier: NewNotificationService(),
} }
} }
...@@ -85,7 +85,7 @@ func (handler *DefaultResultHandler) Handle(evalContext *EvalContext) error { ...@@ -85,7 +85,7 @@ func (handler *DefaultResultHandler) Handle(evalContext *EvalContext) error {
} }
if evalContext.ShouldSendNotification() { if evalContext.ShouldSendNotification() {
handler.notifier.Notify(evalContext) handler.notifier.Send(evalContext)
} }
} }
......
...@@ -23,7 +23,7 @@ func init() { ...@@ -23,7 +23,7 @@ func init() {
} }
func handleNotificationTestCommand(cmd *NotificationTestCommand) error { func handleNotificationTestCommand(cmd *NotificationTestCommand) error {
notifier := NewRootNotifier() notifier := newNotificationService()
model := &m.AlertNotification{ model := &m.AlertNotification{
Name: cmd.Name, Name: cmd.Name,
...@@ -38,10 +38,10 @@ func handleNotificationTestCommand(cmd *NotificationTestCommand) error { ...@@ -38,10 +38,10 @@ func handleNotificationTestCommand(cmd *NotificationTestCommand) error {
return err return err
} }
return notifier.sendNotifications(createTestEvalContext(), []Notifier{notifiers}) return notifier.sendNotifications(createTestEvalContext(cmd), []Notifier{notifiers})
} }
func createTestEvalContext() *EvalContext { func createTestEvalContext(cmd *NotificationTestCommand) *EvalContext {
testRule := &Rule{ testRule := &Rule{
DashboardId: 1, DashboardId: 1,
PanelId: 1, PanelId: 1,
...@@ -51,7 +51,9 @@ func createTestEvalContext() *EvalContext { ...@@ -51,7 +51,9 @@ func createTestEvalContext() *EvalContext {
} }
ctx := NewEvalContext(context.TODO(), testRule) ctx := NewEvalContext(context.TODO(), testRule)
ctx.ImagePublicUrl = "http://grafana.org/assets/img/blog/mixed_styles.png" if cmd.Settings.Get("uploadImage").MustBool(true) {
ctx.ImagePublicUrl = "http://grafana.org/assets/img/blog/mixed_styles.png"
}
ctx.IsTestRun = true ctx.IsTestRun = true
ctx.Firing = true ctx.Firing = true
ctx.Error = nil ctx.Error = nil
......
...@@ -17,6 +17,7 @@ export class AlertNotificationEditCtrl { ...@@ -17,6 +17,7 @@ export class AlertNotificationEditCtrl {
settings: { settings: {
httpMethod: 'POST', httpMethod: 'POST',
autoResolve: true, autoResolve: true,
uploadImage: true,
}, },
isDefault: false isDefault: false
}; };
...@@ -32,7 +33,7 @@ export class AlertNotificationEditCtrl { ...@@ -32,7 +33,7 @@ export class AlertNotificationEditCtrl {
} }
if (!this.$routeParams.id) { if (!this.$routeParams.id) {
return this.model; return _.defaults(this.model, this.defaults);
} }
return this.backendSrv.get(`/api/alert-notifications/${this.$routeParams.id}`).then(result => { return this.backendSrv.get(`/api/alert-notifications/${this.$routeParams.id}`).then(result => {
......
...@@ -24,15 +24,20 @@ ...@@ -24,15 +24,20 @@
</select> </select>
</div> </div>
</div> </div>
<div class="gf-form"> <gf-form-switch
<gf-form-switch class="gf-form"
class="gf-form" label="Send on all alerts"
label="Send on all alerts" label-class="width-12"
label-class="width-12" checked="ctrl.model.isDefault"
checked="ctrl.model.isDefault" tooltip="Use this notification for all alerts">
tooltip="Use this notification for all alerts"> </gf-form-switch>
</gf-form-switch> <gf-form-switch
</div> class="gf-form"
label="Include image"
label-class="width-12"
checked="ctrl.model.settings.uploadImage"
tooltip="Captures an image and include it in the notification">
</gf-form-switch>
</div> </div>
<div class="gf-form-group" ng-include src="ctrl.notifierTemplateId"> <div class="gf-form-group" ng-include src="ctrl.notifierTemplateId">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment