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
This commit is contained in:
Marcus Efraimsson 2020-03-31 00:46:01 +02:00 committed by GitHub
parent ea08c148df
commit 6bd7411f04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 55 additions and 44 deletions

View File

@ -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
}, },
} }
} else { } else {
bodyMsg = map[string]interface{}{ link := map[string]string{
"msgtype": "link",
"link": map[string]string{
"text": message, "text": message,
"title": title, "title": title,
"picUrl": picURL,
"messageUrl": messageURL, "messageUrl": messageURL,
}, }
if dd.NeedsImage() {
link["picUrl"] = picURL
}
bodyMsg = map[string]interface{}{
"msgtype": "link",
"link": link,
} }
} }
return json.Marshal(bodyMsg) return json.Marshal(bodyMsg)

View File

@ -115,6 +115,7 @@ 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 dn.NeedsImage() {
if evalContext.ImagePublicURL != "" { if evalContext.ImagePublicURL != "" {
image = map[string]interface{}{ image = map[string]interface{}{
"url": evalContext.ImagePublicURL, "url": evalContext.ImagePublicURL,
@ -127,6 +128,7 @@ func (dn *DiscordNotifier) Notify(evalContext *alerting.EvalContext) error {
embed.Set("image", image) embed.Set("image", image)
embeddedImage = true embeddedImage = true
} }
}
bodyJSON.Set("embeds", []interface{}{embed}) bodyJSON.Set("embeds", []interface{}{embed})

View File

@ -110,6 +110,7 @@ func (en *EmailNotifier) Notify(evalContext *alerting.EvalContext) error {
}, },
} }
if en.NeedsImage() {
if evalContext.ImagePublicURL != "" { if evalContext.ImagePublicURL != "" {
cmd.Data["ImageLink"] = evalContext.ImagePublicURL cmd.Data["ImageLink"] = evalContext.ImagePublicURL
} else { } else {
@ -119,6 +120,7 @@ func (en *EmailNotifier) Notify(evalContext *alerting.EvalContext) error {
cmd.Data["EmbeddedImage"] = file.Name() cmd.Data["EmbeddedImage"] = file.Name()
} }
} }
}
err = bus.DispatchCtx(evalContext.Ctx, cmd) err = bus.DispatchCtx(evalContext.Ctx, cmd)

View File

@ -152,6 +152,7 @@ func (gcn *GoogleChatNotifier) Notify(evalContext *alerting.EvalContext) error {
} }
widgets = append(widgets, fields) widgets = append(widgets, fields)
if gcn.NeedsImage() {
// if an image exists, add it as an image widget // if an image exists, add it as an image widget
if evalContext.ImagePublicURL != "" { if evalContext.ImagePublicURL != "" {
widgets = append(widgets, imageWidget{ widgets = append(widgets, imageWidget{
@ -162,6 +163,7 @@ func (gcn *GoogleChatNotifier) Notify(evalContext *alerting.EvalContext) error {
} else { } else {
gcn.log.Info("Could not retrieve a public image URL.") gcn.log.Info("Could not retrieve a public image URL.")
} }
}
// add a button widget (link to Grafana) // add a button widget (link to Grafana)
widgets = append(widgets, buttonWidget{ widgets = append(widgets, buttonWidget{

View File

@ -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,

View File

@ -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")

View File

@ -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)
} }

View File

@ -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)
} }

View File

@ -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)

View File

@ -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)
} }

View File

@ -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{}{

View File

@ -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,
}) })

View File

@ -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)

View File

@ -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)
} }

View File

@ -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)
} }