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"`
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

View File

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

View File

@@ -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": "<!here|here> <!subteam^group1><!subteam^group2> <@user1><@user2>",
"type": "mrkdwn"
},
"type": "section"
"ts": %s,
"mrkdwn_in": ["pretext"],
"pretext": "<!here|here> <!subteam^group1><!subteam^group2> <@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>"
}
]
}`,