fix discord notifier so it doesn't crash when there are no image generated
(cherry picked from commit f21c976b27)
This commit is contained in:
committed by
Hugo Häggmark
parent
a5c4dff3b3
commit
3ed4d91aee
@@ -111,57 +111,20 @@ func (this *DiscordNotifier) Notify(evalContext *alerting.EvalContext) error {
|
|||||||
|
|
||||||
json, _ := bodyJSON.MarshalJSON()
|
json, _ := bodyJSON.MarshalJSON()
|
||||||
|
|
||||||
content_type := "application/json"
|
|
||||||
|
|
||||||
var body []byte
|
|
||||||
|
|
||||||
if embeddedImage {
|
|
||||||
|
|
||||||
var b bytes.Buffer
|
|
||||||
|
|
||||||
w := multipart.NewWriter(&b)
|
|
||||||
|
|
||||||
f, err := os.Open(evalContext.ImageOnDiskPath)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
this.log.Error("Can't open graph file", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
fw, err := w.CreateFormField("payload_json")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err = fw.Write([]byte(string(json))); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
fw, err = w.CreateFormFile("file", "graph.png")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err = io.Copy(fw, f); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Close()
|
|
||||||
|
|
||||||
body = b.Bytes()
|
|
||||||
content_type = w.FormDataContentType()
|
|
||||||
|
|
||||||
} else {
|
|
||||||
body = json
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd := &m.SendWebhookSync{
|
cmd := &m.SendWebhookSync{
|
||||||
Url: this.WebhookURL,
|
Url: this.WebhookURL,
|
||||||
Body: string(body),
|
|
||||||
HttpMethod: "POST",
|
HttpMethod: "POST",
|
||||||
ContentType: content_type,
|
ContentType: "application/json",
|
||||||
|
}
|
||||||
|
|
||||||
|
if !embeddedImage {
|
||||||
|
cmd.Body = string(json)
|
||||||
|
} else {
|
||||||
|
err := this.embedImage(cmd, evalContext.ImageOnDiskPath, json)
|
||||||
|
if err != nil {
|
||||||
|
this.log.Error("failed to embed image", "error", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
|
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
|
||||||
@@ -171,3 +134,45 @@ func (this *DiscordNotifier) Notify(evalContext *alerting.EvalContext) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *DiscordNotifier) embedImage(cmd *m.SendWebhookSync, imagePath string, existingJSONBody []byte) error {
|
||||||
|
f, err := os.Open(imagePath)
|
||||||
|
defer f.Close()
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
cmd.Body = string(existingJSONBody)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if !os.IsNotExist(err) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var b bytes.Buffer
|
||||||
|
w := multipart.NewWriter(&b)
|
||||||
|
|
||||||
|
fw, err := w.CreateFormField("payload_json")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err = fw.Write([]byte(string(existingJSONBody))); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fw, err = w.CreateFormFile("file", "graph.png")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err = io.Copy(fw, f); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Close()
|
||||||
|
|
||||||
|
cmd.Body = string(b.Bytes())
|
||||||
|
cmd.ContentType = w.FormDataContentType()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user