Alerting: Fix swallowing of errors when attaching images to notifications (#59432)

* Break out image logic and add logging

* Attach alert log context to image attachment

* Fix capitalization
This commit is contained in:
Alexander Weaver 2022-11-29 13:18:47 -06:00 committed by GitHub
parent a77d95807c
commit 1481ace528
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 31 deletions

View File

@ -248,6 +248,40 @@ func (pn *PushoverNotifier) genPushoverBody(ctx context.Context, as ...*types.Al
return nil, b, fmt.Errorf("failed write the message: %w", err)
}
pn.writeImageParts(ctx, w, as...)
var sound string
if status == model.AlertResolved {
sound = tmpl(pn.settings.okSound)
} else {
sound = tmpl(pn.settings.alertingSound)
}
if sound != "default" {
if err := w.WriteField("sound", sound); err != nil {
return nil, b, fmt.Errorf("failed to write the sound: %w", err)
}
}
// Mark the message as HTML
if err := w.WriteField("html", "1"); err != nil {
return nil, b, fmt.Errorf("failed to mark the message as HTML: %w", err)
}
if err := w.Close(); err != nil {
return nil, b, fmt.Errorf("failed to close the multipart request: %w", err)
}
if tmplErr != nil {
pn.log.Warn("failed to template pushover message", "error", tmplErr.Error())
}
headers := map[string]string{
"Content-Type": w.FormDataContentType(),
}
return headers, b, nil
}
func (pn *PushoverNotifier) writeImageParts(ctx context.Context, w *multipart.Writer, as ...*types.Alert) {
// Pushover supports at most one image attachment with a maximum size of pushoverMaxFileSize.
// If the image is larger than pushoverMaxFileSize then return an error.
_ = withStoredImages(ctx, pn.log, pn.images, func(index int, image ngmodels.Image) error {
@ -281,34 +315,4 @@ func (pn *PushoverNotifier) genPushoverBody(ctx context.Context, as ...*types.Al
return ErrImagesDone
}, as...)
var sound string
if status == model.AlertResolved {
sound = tmpl(pn.settings.okSound)
} else {
sound = tmpl(pn.settings.alertingSound)
}
if sound != "default" {
if err := w.WriteField("sound", sound); err != nil {
return nil, b, fmt.Errorf("failed to write the sound: %w", err)
}
}
// Mark the message as HTML
if err := w.WriteField("html", "1"); err != nil {
return nil, b, fmt.Errorf("failed to mark the message as HTML: %w", err)
}
if err := w.Close(); err != nil {
return nil, b, fmt.Errorf("failed to close the multipart request: %w", err)
}
if tmplErr != nil {
pn.log.Warn("failed to template pushover message", "error", tmplErr.Error())
}
headers := map[string]string{
"Content-Type": w.FormDataContentType(),
}
return headers, b, nil
}

View File

@ -82,7 +82,8 @@ func getImage(ctx context.Context, l log.Logger, imageStore ImageStore, alert ty
// images have been found.
func withStoredImages(ctx context.Context, l log.Logger, imageStore ImageStore, forEachFunc forEachImageFunc, alerts ...*types.Alert) error {
for index, alert := range alerts {
img, err := getImage(ctx, l, imageStore, *alert)
logger := l.New("alert", alert.String())
img, err := getImage(ctx, logger, imageStore, *alert)
if err != nil {
return err
} else if img != nil {
@ -90,6 +91,7 @@ func withStoredImages(ctx context.Context, l log.Logger, imageStore ImageStore,
if errors.Is(err, ErrImagesDone) {
return nil
}
logger.Error("Failed to attach image to notification", "error", err)
return err
}
}