Skip to content

Commit c2e61ee

Browse files
notify/slack: skip the thread copy on repeat_interval notifications
When update_message and post_updates_to_thread are combined, a notification triggered only by repeat_interval elapsing edited the initial message and also posted a full copy of it in the thread. The edited channel message already carries the current state, so the thread reply only added noise on every repeat. Such notifications now only edit the initial message. The notification reason is already available on the context from the dedup stage. When post_updates_to_thread is used without update_message, repeats still post a thread reply, otherwise Slack would receive nothing at all. Behaviour suggested by @cxdy in #5577. Signed-off-by: Sébastien GLON <[email protected]> Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent ef81297 commit c2e61ee

3 files changed

Lines changed: 68 additions & 4 deletions

File tree

‎docs/configuration.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,8 +1750,12 @@ fields:
17501750
17511751
# Posts subsequent notifications for an alert group as replies in the thread of the
17521752
# initial message instead of new channel messages. When combined with update_message,
1753-
# the initial message is updated in place and a reply is also posted to its thread.
1754-
# Webhook URLs do not support threads.
1753+
# the initial message is updated in place and a reply is also posted to its thread,
1754+
# except for notifications triggered only by repeat_interval: the updated message
1755+
# already carries the current state, so it is not copied into the thread again.
1756+
# Requires a Slack app with a bot token (chat:write scope) and api_url set to
1757+
# https://slack.com/api/chat.postMessage. Incoming webhooks cannot be used, they do
1758+
# not return the message identifiers a thread needs.
17551759
[ post_updates_to_thread: <boolean> | default = false ]
17561760
```
17571761

‎notify/slack/slack.go‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,10 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
217217
}
218218

219219
// When update_message and post_updates_to_thread are combined, the initial
220-
// message was just updated in place; additionally post a reply to its thread.
221-
if initialMessageSent && n.conf.UpdateMessage && n.conf.PostUpdatesToThread {
220+
// message was just updated in place; additionally post a reply to its thread,
221+
// unless nothing changed in the alert group: a notification triggered only by
222+
// repeat_interval would add a copy of the message that was just updated.
223+
if initialMessageSent && n.conf.UpdateMessage && n.conf.PostUpdatesToThread && !repeatIntervalOnly(ctx) {
222224
threadReq := *req
223225
threadReq.Timestamp = ""
224226
threadReq.ThreadTimestamp = threadTs
@@ -229,6 +231,13 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
229231
return retry, nil
230232
}
231233

234+
// repeatIntervalOnly reports whether the notification was triggered solely by
235+
// repeat_interval elapsing, meaning the state of the alert group is unchanged.
236+
func repeatIntervalOnly(ctx context.Context) bool {
237+
reason, ok := notify.NotificationReason(ctx)
238+
return ok && reason == notify.ReasonRepeatIntervalElapsed
239+
}
240+
232241
// postRequest encodes and sends a single request to the Slack API, classifies
233242
// errors as retriable or not, and hands the response to slackResponseHandler.
234243
func (n *Notifier) postRequest(ctx context.Context, u string, req *request, store *nflog.Store) (bool, error) {

‎notify/slack/slack_test.go‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,57 @@ func TestSlackPostUpdatesToThread(t *testing.T) {
552552
require.Equal(t, "111.222", threadTs)
553553
})
554554

555+
t.Run("repeat_interval notification only updates the message", func(t *testing.T) {
556+
var captured []capturedRequest
557+
notifier := newTestNotifier(t, &config.SlackConfig{UpdateMessage: true, PostUpdatesToThread: true}, &captured, "999.999")
558+
store := nflog.NewStore(nil)
559+
store.SetStr("threadTs", "111.222")
560+
store.SetStr("channelId", "C123")
561+
562+
ctx := notify.WithNotificationReason(newCtx(store), notify.ReasonRepeatIntervalElapsed)
563+
_, err := notifier.Notify(ctx)
564+
require.NoError(t, err)
565+
566+
// The updated channel message already carries the current state, so no
567+
// copy of it is added to the thread.
568+
require.Len(t, captured, 1)
569+
require.Equal(t, "https://slack.com/api/chat.update", captured[0].url)
570+
require.Equal(t, "111.222", captured[0].body["ts"])
571+
})
572+
573+
t.Run("repeat_interval notification still posts to thread without update_message", func(t *testing.T) {
574+
var captured []capturedRequest
575+
notifier := newTestNotifier(t, &config.SlackConfig{PostUpdatesToThread: true}, &captured, "999.999")
576+
store := nflog.NewStore(nil)
577+
store.SetStr("threadTs", "111.222")
578+
store.SetStr("channelId", "C123")
579+
580+
ctx := notify.WithNotificationReason(newCtx(store), notify.ReasonRepeatIntervalElapsed)
581+
_, err := notifier.Notify(ctx)
582+
require.NoError(t, err)
583+
584+
// Without update_message nothing else carries the notification.
585+
require.Len(t, captured, 1)
586+
require.Equal(t, "https://slack.com/api/chat.postMessage", captured[0].url)
587+
require.Equal(t, "111.222", captured[0].body["thread_ts"])
588+
})
589+
590+
t.Run("state change notification posts to thread after repeat_interval", func(t *testing.T) {
591+
var captured []capturedRequest
592+
notifier := newTestNotifier(t, &config.SlackConfig{UpdateMessage: true, PostUpdatesToThread: true}, &captured, "999.999")
593+
store := nflog.NewStore(nil)
594+
store.SetStr("threadTs", "111.222")
595+
store.SetStr("channelId", "C123")
596+
597+
ctx := notify.WithNotificationReason(newCtx(store), notify.ReasonNewAlertsInGroup)
598+
_, err := notifier.Notify(ctx)
599+
require.NoError(t, err)
600+
601+
require.Len(t, captured, 2)
602+
require.Equal(t, "https://slack.com/api/chat.update", captured[0].url)
603+
require.Equal(t, "111.222", captured[1].body["thread_ts"])
604+
})
605+
555606
t.Run("subsequent notification posts only to thread without update_message", func(t *testing.T) {
556607
var captured []capturedRequest
557608
notifier := newTestNotifier(t, &config.SlackConfig{PostUpdatesToThread: true}, &captured, "999.999")

0 commit comments

Comments
 (0)