Commit bf908424 by Arve Knudsen Committed by GitHub

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

parent 8c2107cd
...@@ -276,22 +276,25 @@ func (sn *SlackNotifier) Notify(evalContext *alerting.EvalContext) error { ...@@ -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 { ...@@ -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)
......
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