Commit 6bd7411f by Marcus Efraimsson Committed by GitHub

Alerting: Only include image in notifier when enabled (#23194)

Fixes a bug when you had multiple notifiers for a rule, but not all 
had enabled to include image.

Fixes #22883
parent ea08c148
...@@ -114,7 +114,7 @@ func (dd *DingDingNotifier) genBody(evalContext *alerting.EvalContext, messageUR ...@@ -114,7 +114,7 @@ func (dd *DingDingNotifier) genBody(evalContext *alerting.EvalContext, messageUR
var bodyMsg map[string]interface{} var bodyMsg map[string]interface{}
if dd.MsgType == "actionCard" { if dd.MsgType == "actionCard" {
// Embed the pic into the markdown directly because actionCard doesn't have a picUrl field // Embed the pic into the markdown directly because actionCard doesn't have a picUrl field
if picURL != "" { if dd.NeedsImage() && picURL != "" {
message = "![](" + picURL + ")\\n\\n" + message message = "![](" + picURL + ")\\n\\n" + message
} }
...@@ -128,14 +128,19 @@ func (dd *DingDingNotifier) genBody(evalContext *alerting.EvalContext, messageUR ...@@ -128,14 +128,19 @@ func (dd *DingDingNotifier) genBody(evalContext *alerting.EvalContext, messageUR
}, },
} }
} else { } else {
link := map[string]string{
"text": message,
"title": title,
"messageUrl": messageURL,
}
if dd.NeedsImage() {
link["picUrl"] = picURL
}
bodyMsg = map[string]interface{}{ bodyMsg = map[string]interface{}{
"msgtype": "link", "msgtype": "link",
"link": map[string]string{ "link": link,
"text": message,
"title": title,
"picUrl": picURL,
"messageUrl": messageURL,
},
} }
} }
return json.Marshal(bodyMsg) return json.Marshal(bodyMsg)
......
...@@ -115,17 +115,19 @@ func (dn *DiscordNotifier) Notify(evalContext *alerting.EvalContext) error { ...@@ -115,17 +115,19 @@ func (dn *DiscordNotifier) Notify(evalContext *alerting.EvalContext) error {
var image map[string]interface{} var image map[string]interface{}
var embeddedImage = false var embeddedImage = false
if evalContext.ImagePublicURL != "" { if dn.NeedsImage() {
image = map[string]interface{}{ if evalContext.ImagePublicURL != "" {
"url": evalContext.ImagePublicURL, image = map[string]interface{}{
"url": evalContext.ImagePublicURL,
}
embed.Set("image", image)
} else {
image = map[string]interface{}{
"url": "attachment://graph.png",
}
embed.Set("image", image)
embeddedImage = true
} }
embed.Set("image", image)
} else {
image = map[string]interface{}{
"url": "attachment://graph.png",
}
embed.Set("image", image)
embeddedImage = true
} }
bodyJSON.Set("embeds", []interface{}{embed}) bodyJSON.Set("embeds", []interface{}{embed})
......
...@@ -110,13 +110,15 @@ func (en *EmailNotifier) Notify(evalContext *alerting.EvalContext) error { ...@@ -110,13 +110,15 @@ func (en *EmailNotifier) Notify(evalContext *alerting.EvalContext) error {
}, },
} }
if evalContext.ImagePublicURL != "" { if en.NeedsImage() {
cmd.Data["ImageLink"] = evalContext.ImagePublicURL if evalContext.ImagePublicURL != "" {
} else { cmd.Data["ImageLink"] = evalContext.ImagePublicURL
file, err := os.Stat(evalContext.ImageOnDiskPath) } else {
if err == nil { file, err := os.Stat(evalContext.ImageOnDiskPath)
cmd.EmbededFiles = []string{evalContext.ImageOnDiskPath} if err == nil {
cmd.Data["EmbeddedImage"] = file.Name() cmd.EmbededFiles = []string{evalContext.ImageOnDiskPath}
cmd.Data["EmbeddedImage"] = file.Name()
}
} }
} }
......
...@@ -152,15 +152,17 @@ func (gcn *GoogleChatNotifier) Notify(evalContext *alerting.EvalContext) error { ...@@ -152,15 +152,17 @@ func (gcn *GoogleChatNotifier) Notify(evalContext *alerting.EvalContext) error {
} }
widgets = append(widgets, fields) widgets = append(widgets, fields)
// if an image exists, add it as an image widget if gcn.NeedsImage() {
if evalContext.ImagePublicURL != "" { // if an image exists, add it as an image widget
widgets = append(widgets, imageWidget{ if evalContext.ImagePublicURL != "" {
Image: image{ widgets = append(widgets, imageWidget{
ImageURL: evalContext.ImagePublicURL, Image: image{
}, ImageURL: evalContext.ImagePublicURL,
}) },
} else { })
gcn.log.Info("Could not retrieve a public image URL.") } else {
gcn.log.Info("Could not retrieve a public image URL.")
}
} }
// add a button widget (link to Grafana) // add a button widget (link to Grafana)
......
...@@ -148,7 +148,7 @@ func (hc *HipChatNotifier) Notify(evalContext *alerting.EvalContext) error { ...@@ -148,7 +148,7 @@ func (hc *HipChatNotifier) Notify(evalContext *alerting.EvalContext) error {
"date": evalContext.EndTime.Unix(), "date": evalContext.EndTime.Unix(),
"attributes": attributes, "attributes": attributes,
} }
if evalContext.ImagePublicURL != "" { if hc.NeedsImage() && evalContext.ImagePublicURL != "" {
card["thumbnail"] = map[string]interface{}{ card["thumbnail"] = map[string]interface{}{
"url": evalContext.ImagePublicURL, "url": evalContext.ImagePublicURL,
"url@2x": evalContext.ImagePublicURL, "url@2x": evalContext.ImagePublicURL,
......
...@@ -89,7 +89,7 @@ func (kn *KafkaNotifier) Notify(evalContext *alerting.EvalContext) error { ...@@ -89,7 +89,7 @@ func (kn *KafkaNotifier) Notify(evalContext *alerting.EvalContext) error {
} }
bodyJSON.Set("client_url", ruleURL) bodyJSON.Set("client_url", ruleURL)
if evalContext.ImagePublicURL != "" { if kn.NeedsImage() && evalContext.ImagePublicURL != "" {
contexts := make([]interface{}, 1) contexts := make([]interface{}, 1)
imageJSON := simplejson.New() imageJSON := simplejson.New()
imageJSON.Set("type", "image") imageJSON.Set("type", "image")
......
...@@ -78,7 +78,7 @@ func (ln *LineNotifier) createAlert(evalContext *alerting.EvalContext) error { ...@@ -78,7 +78,7 @@ func (ln *LineNotifier) createAlert(evalContext *alerting.EvalContext) error {
body := fmt.Sprintf("%s - %s\n%s", evalContext.Rule.Name, ruleURL, evalContext.Rule.Message) body := fmt.Sprintf("%s - %s\n%s", evalContext.Rule.Name, ruleURL, evalContext.Rule.Message)
form.Add("message", body) form.Add("message", body)
if evalContext.ImagePublicURL != "" { if ln.NeedsImage() && evalContext.ImagePublicURL != "" {
form.Add("imageThumbnail", evalContext.ImagePublicURL) form.Add("imageThumbnail", evalContext.ImagePublicURL)
form.Add("imageFullsize", evalContext.ImagePublicURL) form.Add("imageFullsize", evalContext.ImagePublicURL)
} }
......
...@@ -123,7 +123,7 @@ func (on *OpsGenieNotifier) createAlert(evalContext *alerting.EvalContext) error ...@@ -123,7 +123,7 @@ func (on *OpsGenieNotifier) createAlert(evalContext *alerting.EvalContext) error
details := simplejson.New() details := simplejson.New()
details.Set("url", ruleURL) details.Set("url", ruleURL)
if evalContext.ImagePublicURL != "" { if on.NeedsImage() && evalContext.ImagePublicURL != "" {
details.Set("image", evalContext.ImagePublicURL) details.Set("image", evalContext.ImagePublicURL)
} }
......
...@@ -160,7 +160,7 @@ func (pn *PagerdutyNotifier) buildEventPayload(evalContext *alerting.EvalContext ...@@ -160,7 +160,7 @@ func (pn *PagerdutyNotifier) buildEventPayload(evalContext *alerting.EvalContext
links[0] = linkJSON links[0] = linkJSON
bodyJSON.Set("links", links) bodyJSON.Set("links", links)
if evalContext.ImagePublicURL != "" { if pn.NeedsImage() && evalContext.ImagePublicURL != "" {
contexts := make([]interface{}, 1) contexts := make([]interface{}, 1)
imageJSON := simplejson.New() imageJSON := simplejson.New()
imageJSON.Set("src", evalContext.ImagePublicURL) imageJSON.Set("src", evalContext.ImagePublicURL)
......
...@@ -111,7 +111,7 @@ func (sn *SensuNotifier) Notify(evalContext *alerting.EvalContext) error { ...@@ -111,7 +111,7 @@ func (sn *SensuNotifier) Notify(evalContext *alerting.EvalContext) error {
bodyJSON.Set("ruleUrl", ruleURL) bodyJSON.Set("ruleUrl", ruleURL)
} }
if evalContext.ImagePublicURL != "" { if sn.NeedsImage() && evalContext.ImagePublicURL != "" {
bodyJSON.Set("imageUrl", evalContext.ImagePublicURL) bodyJSON.Set("imageUrl", evalContext.ImagePublicURL)
} }
......
...@@ -287,7 +287,7 @@ func (sn *SlackNotifier) Notify(evalContext *alerting.EvalContext) error { ...@@ -287,7 +287,7 @@ func (sn *SlackNotifier) Notify(evalContext *alerting.EvalContext) error {
"footer_icon": "https://grafana.com/assets/img/fav32.png", "footer_icon": "https://grafana.com/assets/img/fav32.png",
"ts": time.Now().Unix(), "ts": time.Now().Unix(),
} }
if imageURL != "" { if sn.NeedsImage() && imageURL != "" {
attachment["image_url"] = imageURL attachment["image_url"] = imageURL
} }
body := map[string]interface{}{ body := map[string]interface{}{
......
...@@ -83,7 +83,7 @@ func (tn *TeamsNotifier) Notify(evalContext *alerting.EvalContext) error { ...@@ -83,7 +83,7 @@ func (tn *TeamsNotifier) Notify(evalContext *alerting.EvalContext) error {
} }
images := make([]map[string]interface{}, 0) images := make([]map[string]interface{}, 0)
if evalContext.ImagePublicURL != "" { if tn.NeedsImage() && evalContext.ImagePublicURL != "" {
images = append(images, map[string]interface{}{ images = append(images, map[string]interface{}{
"image": evalContext.ImagePublicURL, "image": evalContext.ImagePublicURL,
}) })
......
...@@ -147,7 +147,7 @@ func (notifier *ThreemaNotifier) Notify(evalContext *alerting.EvalContext) error ...@@ -147,7 +147,7 @@ func (notifier *ThreemaNotifier) Notify(evalContext *alerting.EvalContext) error
if err == nil { if err == nil {
message = message + fmt.Sprintf("*URL:* %s\n", ruleURL) message = message + fmt.Sprintf("*URL:* %s\n", ruleURL)
} }
if evalContext.ImagePublicURL != "" { if notifier.NeedsImage() && evalContext.ImagePublicURL != "" {
message = message + fmt.Sprintf("*Image:* %s\n", evalContext.ImagePublicURL) message = message + fmt.Sprintf("*Image:* %s\n", evalContext.ImagePublicURL)
} }
data.Set("text", message) data.Set("text", message)
......
...@@ -116,7 +116,7 @@ func (vn *VictoropsNotifier) Notify(evalContext *alerting.EvalContext) error { ...@@ -116,7 +116,7 @@ func (vn *VictoropsNotifier) Notify(evalContext *alerting.EvalContext) error {
bodyJSON.Set("error_message", evalContext.Error.Error()) bodyJSON.Set("error_message", evalContext.Error.Error())
} }
if evalContext.ImagePublicURL != "" { if vn.NeedsImage() && evalContext.ImagePublicURL != "" {
bodyJSON.Set("image_url", evalContext.ImagePublicURL) bodyJSON.Set("image_url", evalContext.ImagePublicURL)
} }
......
...@@ -97,7 +97,7 @@ func (wn *WebhookNotifier) Notify(evalContext *alerting.EvalContext) error { ...@@ -97,7 +97,7 @@ func (wn *WebhookNotifier) Notify(evalContext *alerting.EvalContext) error {
bodyJSON.Set("ruleUrl", ruleURL) bodyJSON.Set("ruleUrl", ruleURL)
} }
if evalContext.ImagePublicURL != "" { if wn.NeedsImage() && evalContext.ImagePublicURL != "" {
bodyJSON.Set("imageUrl", evalContext.ImagePublicURL) bodyJSON.Set("imageUrl", evalContext.ImagePublicURL)
} }
......
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