From d581b368bdeeaf8870e495a15dcfdf5aef293be9 Mon Sep 17 00:00:00 2001 From: Will Jordan Date: Fri, 4 Nov 2022 09:09:24 -0700 Subject: [PATCH] Alerting: Remove duplicate Slack notification title (#58107) Move mentions to a markdown-formatted pretext field to prevent issues mixing blocks and legacy-attachment content. --- .../ngalert/notifier/channels/slack.go | 17 ++++------- .../ngalert/notifier/channels/slack_test.go | 5 ---- .../alerting/api_notification_channel_test.go | 28 ++++--------------- 3 files changed, 12 insertions(+), 38 deletions(-) diff --git a/pkg/services/ngalert/notifier/channels/slack.go b/pkg/services/ngalert/notifier/channels/slack.go index 199725a1c37..4b424a0f40a 100644 --- a/pkg/services/ngalert/notifier/channels/slack.go +++ b/pkg/services/ngalert/notifier/channels/slack.go @@ -132,7 +132,7 @@ type slackMessage struct { IconEmoji string `json:"icon_emoji,omitempty"` IconURL string `json:"icon_url,omitempty"` Attachments []attachment `json:"attachments"` - Blocks []map[string]interface{} `json:"blocks"` + Blocks []map[string]interface{} `json:"blocks,omitempty"` } // attachment is used to display a richly-formatted message block. @@ -147,6 +147,8 @@ type attachment struct { FooterIcon string `json:"footer_icon"` Color string `json:"color,omitempty"` Ts int64 `json:"ts,omitempty"` + Pretext string `json:"pretext,omitempty"` + MrkdwnIn []string `json:"mrkdwn_in,omitempty"` } // Notify sends an alert notification to Slack. @@ -261,7 +263,6 @@ func (sn *SlackNotifier) buildSlackMessage(ctx context.Context, alrts []*types.A req := &slackMessage{ Channel: tmpl(sn.settings.Recipient), - Text: tmpl(sn.settings.Title), Username: tmpl(sn.settings.Username), IconEmoji: tmpl(sn.settings.IconEmoji), IconURL: tmpl(sn.settings.IconURL), @@ -315,15 +316,9 @@ func (sn *SlackNotifier) buildSlackMessage(ctx context.Context, alrts []*types.A } if mentionsBuilder.Len() > 0 { - req.Blocks = []map[string]interface{}{ - { - "type": "section", - "text": map[string]interface{}{ - "type": "mrkdwn", - "text": mentionsBuilder.String(), - }, - }, - } + // Use markdown-formatted pretext for any mentions. + req.Attachments[0].MrkdwnIn = []string{"pretext"} + req.Attachments[0].Pretext = mentionsBuilder.String() } return req, nil diff --git a/pkg/services/ngalert/notifier/channels/slack_test.go b/pkg/services/ngalert/notifier/channels/slack_test.go index f8405c7b646..e57a3e95e72 100644 --- a/pkg/services/ngalert/notifier/channels/slack_test.go +++ b/pkg/services/ngalert/notifier/channels/slack_test.go @@ -64,7 +64,6 @@ func TestSlackNotifier(t *testing.T) { }, expMsg: &slackMessage{ Channel: "#testchannel", - Text: "[FIRING:1] (val1)", Username: "Grafana", IconEmoji: ":emoji:", Attachments: []attachment{ @@ -100,7 +99,6 @@ func TestSlackNotifier(t *testing.T) { }, expMsg: &slackMessage{ Channel: "#testchannel", - Text: "[FIRING:1] (val1)", Username: "Grafana", IconEmoji: ":emoji:", Attachments: []attachment{ @@ -136,7 +134,6 @@ func TestSlackNotifier(t *testing.T) { }, expMsg: &slackMessage{ Channel: "#testchannel", - Text: "[FIRING:1] (val1)", Username: "Grafana", IconEmoji: ":emoji:", Attachments: []attachment{ @@ -180,7 +177,6 @@ func TestSlackNotifier(t *testing.T) { }, expMsg: &slackMessage{ Channel: "#testchannel", - Text: "2 firing, 0 resolved", Username: "Grafana", IconEmoji: ":emoji:", Attachments: []attachment{ @@ -229,7 +225,6 @@ func TestSlackNotifier(t *testing.T) { }, expMsg: &slackMessage{ Channel: "#testchannel", - Text: "[FIRING:1] (val1)", Username: "Grafana", IconEmoji: ":emoji:", Attachments: []attachment{ diff --git a/pkg/tests/api/alerting/api_notification_channel_test.go b/pkg/tests/api/alerting/api_notification_channel_test.go index 1d6f9c3dbed..c520fa07bf4 100644 --- a/pkg/tests/api/alerting/api_notification_channel_test.go +++ b/pkg/tests/api/alerting/api_notification_channel_test.go @@ -2312,7 +2312,6 @@ var expNonEmailNotifications = map[string][]string{ "slack_recv1/slack_test_without_token": { `{ "channel": "#test-channel", - "text": "Integration Test [FIRING:1] SlackAlert1 (default)", "username": "Integration Test", "icon_emoji": "🚀", "icon_url": "https://awesomeemoji.com/rocket", @@ -2325,16 +2324,9 @@ var expNonEmailNotifications = map[string][]string{ "footer": "Grafana v", "footer_icon": "https://grafana.com/assets/img/fav32.png", "color": "#D63232", - "ts": %s - } - ], - "blocks": [ - { - "text": { - "text": " <@user1><@user2>", - "type": "mrkdwn" - }, - "type": "section" + "ts": %s, + "mrkdwn_in": ["pretext"], + "pretext": " <@user1><@user2>" } ] }`, @@ -2342,7 +2334,6 @@ var expNonEmailNotifications = map[string][]string{ "slack_recvX/slack_testX": { `{ "channel": "#test-channel", - "text": "[FIRING:1] SlackAlert2 (default)", "username": "Integration Test", "attachments": [ { @@ -2353,16 +2344,9 @@ var expNonEmailNotifications = map[string][]string{ "footer": "Grafana v", "footer_icon": "https://grafana.com/assets/img/fav32.png", "color": "#D63232", - "ts": %s - } - ], - "blocks": [ - { - "text": { - "text": "<@user1><@user2>", - "type": "mrkdwn" - }, - "type": "section" + "ts": %s, + "mrkdwn_in": ["pretext"], + "pretext": "<@user1><@user2>" } ] }`,