diff --git a/pkg/services/ngalert/notifier/channels/pushover.go b/pkg/services/ngalert/notifier/channels/pushover.go index b795f7c6d18..293d537e783 100644 --- a/pkg/services/ngalert/notifier/channels/pushover.go +++ b/pkg/services/ngalert/notifier/channels/pushover.go @@ -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 } diff --git a/pkg/services/ngalert/notifier/channels/util.go b/pkg/services/ngalert/notifier/channels/util.go index 409fa98901a..244418df1c9 100644 --- a/pkg/services/ngalert/notifier/channels/util.go +++ b/pkg/services/ngalert/notifier/channels/util.go @@ -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 } }