mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
parent
a77d95807c
commit
1481ace528
@ -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)
|
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.
|
// Pushover supports at most one image attachment with a maximum size of pushoverMaxFileSize.
|
||||||
// If the image is larger than pushoverMaxFileSize then return an error.
|
// If the image is larger than pushoverMaxFileSize then return an error.
|
||||||
_ = withStoredImages(ctx, pn.log, pn.images, func(index int, image ngmodels.Image) 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
|
return ErrImagesDone
|
||||||
}, as...)
|
}, 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
|
|
||||||
}
|
}
|
||||||
|
@ -82,7 +82,8 @@ func getImage(ctx context.Context, l log.Logger, imageStore ImageStore, alert ty
|
|||||||
// images have been found.
|
// images have been found.
|
||||||
func withStoredImages(ctx context.Context, l log.Logger, imageStore ImageStore, forEachFunc forEachImageFunc, alerts ...*types.Alert) error {
|
func withStoredImages(ctx context.Context, l log.Logger, imageStore ImageStore, forEachFunc forEachImageFunc, alerts ...*types.Alert) error {
|
||||||
for index, alert := range alerts {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else if img != nil {
|
} else if img != nil {
|
||||||
@ -90,6 +91,7 @@ func withStoredImages(ctx context.Context, l log.Logger, imageStore ImageStore,
|
|||||||
if errors.Is(err, ErrImagesDone) {
|
if errors.Is(err, ErrImagesDone) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
logger.Error("Failed to attach image to notification", "error", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user