Alerting: Remove duplicate Slack notification title (#58107)

Move mentions to a markdown-formatted pretext field
to prevent issues mixing blocks and legacy-attachment content.
This commit is contained in:
Will Jordan
2022-11-04 09:09:24 -07:00
committed by GitHub
parent 4d2bf41efb
commit d581b368bd
3 changed files with 12 additions and 38 deletions

View File

@@ -132,7 +132,7 @@ type slackMessage struct {
IconEmoji string `json:"icon_emoji,omitempty"` IconEmoji string `json:"icon_emoji,omitempty"`
IconURL string `json:"icon_url,omitempty"` IconURL string `json:"icon_url,omitempty"`
Attachments []attachment `json:"attachments"` 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. // attachment is used to display a richly-formatted message block.
@@ -147,6 +147,8 @@ type attachment struct {
FooterIcon string `json:"footer_icon"` FooterIcon string `json:"footer_icon"`
Color string `json:"color,omitempty"` Color string `json:"color,omitempty"`
Ts int64 `json:"ts,omitempty"` Ts int64 `json:"ts,omitempty"`
Pretext string `json:"pretext,omitempty"`
MrkdwnIn []string `json:"mrkdwn_in,omitempty"`
} }
// Notify sends an alert notification to Slack. // Notify sends an alert notification to Slack.
@@ -261,7 +263,6 @@ func (sn *SlackNotifier) buildSlackMessage(ctx context.Context, alrts []*types.A
req := &slackMessage{ req := &slackMessage{
Channel: tmpl(sn.settings.Recipient), Channel: tmpl(sn.settings.Recipient),
Text: tmpl(sn.settings.Title),
Username: tmpl(sn.settings.Username), Username: tmpl(sn.settings.Username),
IconEmoji: tmpl(sn.settings.IconEmoji), IconEmoji: tmpl(sn.settings.IconEmoji),
IconURL: tmpl(sn.settings.IconURL), IconURL: tmpl(sn.settings.IconURL),
@@ -315,15 +316,9 @@ func (sn *SlackNotifier) buildSlackMessage(ctx context.Context, alrts []*types.A
} }
if mentionsBuilder.Len() > 0 { if mentionsBuilder.Len() > 0 {
req.Blocks = []map[string]interface{}{ // Use markdown-formatted pretext for any mentions.
{ req.Attachments[0].MrkdwnIn = []string{"pretext"}
"type": "section", req.Attachments[0].Pretext = mentionsBuilder.String()
"text": map[string]interface{}{
"type": "mrkdwn",
"text": mentionsBuilder.String(),
},
},
}
} }
return req, nil return req, nil

View File

@@ -64,7 +64,6 @@ func TestSlackNotifier(t *testing.T) {
}, },
expMsg: &slackMessage{ expMsg: &slackMessage{
Channel: "#testchannel", Channel: "#testchannel",
Text: "[FIRING:1] (val1)",
Username: "Grafana", Username: "Grafana",
IconEmoji: ":emoji:", IconEmoji: ":emoji:",
Attachments: []attachment{ Attachments: []attachment{
@@ -100,7 +99,6 @@ func TestSlackNotifier(t *testing.T) {
}, },
expMsg: &slackMessage{ expMsg: &slackMessage{
Channel: "#testchannel", Channel: "#testchannel",
Text: "[FIRING:1] (val1)",
Username: "Grafana", Username: "Grafana",
IconEmoji: ":emoji:", IconEmoji: ":emoji:",
Attachments: []attachment{ Attachments: []attachment{
@@ -136,7 +134,6 @@ func TestSlackNotifier(t *testing.T) {
}, },
expMsg: &slackMessage{ expMsg: &slackMessage{
Channel: "#testchannel", Channel: "#testchannel",
Text: "[FIRING:1] (val1)",
Username: "Grafana", Username: "Grafana",
IconEmoji: ":emoji:", IconEmoji: ":emoji:",
Attachments: []attachment{ Attachments: []attachment{
@@ -180,7 +177,6 @@ func TestSlackNotifier(t *testing.T) {
}, },
expMsg: &slackMessage{ expMsg: &slackMessage{
Channel: "#testchannel", Channel: "#testchannel",
Text: "2 firing, 0 resolved",
Username: "Grafana", Username: "Grafana",
IconEmoji: ":emoji:", IconEmoji: ":emoji:",
Attachments: []attachment{ Attachments: []attachment{
@@ -229,7 +225,6 @@ func TestSlackNotifier(t *testing.T) {
}, },
expMsg: &slackMessage{ expMsg: &slackMessage{
Channel: "#testchannel", Channel: "#testchannel",
Text: "[FIRING:1] (val1)",
Username: "Grafana", Username: "Grafana",
IconEmoji: ":emoji:", IconEmoji: ":emoji:",
Attachments: []attachment{ Attachments: []attachment{

View File

@@ -2312,7 +2312,6 @@ var expNonEmailNotifications = map[string][]string{
"slack_recv1/slack_test_without_token": { "slack_recv1/slack_test_without_token": {
`{ `{
"channel": "#test-channel", "channel": "#test-channel",
"text": "Integration Test [FIRING:1] SlackAlert1 (default)",
"username": "Integration Test", "username": "Integration Test",
"icon_emoji": "🚀", "icon_emoji": "🚀",
"icon_url": "https://awesomeemoji.com/rocket", "icon_url": "https://awesomeemoji.com/rocket",
@@ -2325,16 +2324,9 @@ var expNonEmailNotifications = map[string][]string{
"footer": "Grafana v", "footer": "Grafana v",
"footer_icon": "https://grafana.com/assets/img/fav32.png", "footer_icon": "https://grafana.com/assets/img/fav32.png",
"color": "#D63232", "color": "#D63232",
"ts": %s "ts": %s,
} "mrkdwn_in": ["pretext"],
], "pretext": "<!here|here> <!subteam^group1><!subteam^group2> <@user1><@user2>"
"blocks": [
{
"text": {
"text": "<!here|here> <!subteam^group1><!subteam^group2> <@user1><@user2>",
"type": "mrkdwn"
},
"type": "section"
} }
] ]
}`, }`,
@@ -2342,7 +2334,6 @@ var expNonEmailNotifications = map[string][]string{
"slack_recvX/slack_testX": { "slack_recvX/slack_testX": {
`{ `{
"channel": "#test-channel", "channel": "#test-channel",
"text": "[FIRING:1] SlackAlert2 (default)",
"username": "Integration Test", "username": "Integration Test",
"attachments": [ "attachments": [
{ {
@@ -2353,16 +2344,9 @@ var expNonEmailNotifications = map[string][]string{
"footer": "Grafana v", "footer": "Grafana v",
"footer_icon": "https://grafana.com/assets/img/fav32.png", "footer_icon": "https://grafana.com/assets/img/fav32.png",
"color": "#D63232", "color": "#D63232",
"ts": %s "ts": %s,
} "mrkdwn_in": ["pretext"],
], "pretext": "<@user1><@user2>"
"blocks": [
{
"text": {
"text": "<@user1><@user2>",
"type": "mrkdwn"
},
"type": "section"
} }
] ]
}`, }`,