Alerting: Don't include image_url field with Slack message if empty (#22372)

This commit is contained in:
Arve Knudsen 2020-02-21 15:43:54 +01:00 committed by GitHub
parent 8c2107cd35
commit bf908424a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -276,22 +276,25 @@ func (sn *SlackNotifier) Notify(evalContext *alerting.EvalContext) error {
}, },
} }
} }
attachment := map[string]interface{}{
"color": evalContext.GetStateModel().Color,
"title": evalContext.GetNotificationTitle(),
"title_link": ruleURL,
"text": msg,
"fallback": evalContext.GetNotificationTitle(),
"fields": fields,
"footer": "Grafana v" + setting.BuildVersion,
"footer_icon": "https://grafana.com/assets/img/fav32.png",
"ts": time.Now().Unix(),
}
if imageURL != "" {
attachment["image_url"] = imageURL
}
body := map[string]interface{}{ body := map[string]interface{}{
"text": evalContext.GetNotificationTitle(), "text": evalContext.GetNotificationTitle(),
"blocks": blocks, "blocks": blocks,
"attachments": []map[string]interface{}{ "attachments": []map[string]interface{}{
{ attachment,
"color": evalContext.GetStateModel().Color,
"title": evalContext.GetNotificationTitle(),
"title_link": ruleURL,
"text": msg,
"fallback": evalContext.GetNotificationTitle(),
"fields": fields,
"image_url": imageURL,
"footer": "Grafana v" + setting.BuildVersion,
"footer_icon": "https://grafana.com/assets/img/fav32.png",
"ts": time.Now().Unix(),
},
}, },
"parse": "full", // to linkify urls, users and channels in alert message. "parse": "full", // to linkify urls, users and channels in alert message.
} }
@ -313,6 +316,7 @@ func (sn *SlackNotifier) Notify(evalContext *alerting.EvalContext) error {
if err != nil { if err != nil {
return err return err
} }
cmd := &models.SendWebhookSync{Url: sn.URL, Body: string(data)} cmd := &models.SendWebhookSync{Url: sn.URL, Body: string(data)}
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil { if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
sn.log.Error("Failed to send slack notification", "error", err, "webhook", sn.Name) sn.log.Error("Failed to send slack notification", "error", err, "webhook", sn.Name)