From ff97f7314f31934e3953fe5ad76eaf1b55628dbe Mon Sep 17 00:00:00 2001 From: George Robinson Date: Wed, 3 Apr 2024 11:18:06 +0100 Subject: [PATCH] Update tests to check markers Signed-off-by: George Robinson --- notify/notify.go | 10 +++------- notify/notify_test.go | 44 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/notify/notify.go b/notify/notify.go index a46f92517b..c8ea921577 100644 --- a/notify/notify.go +++ b/notify/notify.go @@ -960,10 +960,8 @@ func (tms TimeMuteStage) Exec(ctx context.Context, l log.Logger, alerts ...*type if err != nil { return ctx, alerts, err } - for _, a := range alerts { - // If muted is false then mutedBy is nil and the muted marker is removed. - tms.marker.SetMuted(gkey, a.Fingerprint(), mutedBy) - } + // If muted is false then mutedBy is nil and the muted marker is removed. + tms.marker.SetMuted(gkey, mutedBy) // If the current time is inside a mute time, all alerts are removed from the pipeline. if muted { @@ -1016,9 +1014,7 @@ func (tas TimeActiveStage) Exec(ctx context.Context, l log.Logger, alerts ...*ty // to be active. mutedBy = activeTimeIntervalNames } - for _, a := range alerts { - tas.marker.SetMuted(gkey, a.Fingerprint(), mutedBy) - } + tas.marker.SetMuted(gkey, mutedBy) // If the current time is not inside an active time, all alerts are removed from the pipeline if !active { diff --git a/notify/notify_test.go b/notify/notify_test.go index 7a760d82e4..a62cc3a155 100644 --- a/notify/notify_test.go +++ b/notify/notify_test.go @@ -899,6 +899,7 @@ func TestTimeMuteStage(t *testing.T) { ctx := context.Background() ctx = WithNow(ctx, test.now) + ctx = WithGroupKey(ctx, "group1") ctx = WithActiveTimeIntervals(ctx, nil) ctx = WithMuteTimeIntervals(ctx, muteTimeIntervalNames) @@ -908,14 +909,33 @@ func TestTimeMuteStage(t *testing.T) { if len(test.mutedBy) == 0 { // All alerts should be active. require.Equal(t, len(test.alerts), len(active)) + // The group should not be marked. + mutedBy, isMuted := marker.Muted("group1") + require.False(t, isMuted) + require.Empty(t, mutedBy) // The metric for total suppressed notifications should not // have been incremented, which means it will not be collected. - require.NoError(t, prom_testutil.GatherAndCompare(r, strings.NewReader(""))) + require.NoError(t, prom_testutil.GatherAndCompare(r, strings.NewReader(` +# HELP alertmanager_marked_alerts How many alerts by state are currently marked in the Alertmanager regardless of their expiry. +# TYPE alertmanager_marked_alerts gauge +alertmanager_marked_alerts{state="active"} 0 +alertmanager_marked_alerts{state="suppressed"} 0 +alertmanager_marked_alerts{state="unprocessed"} 0 +`))) } else { // All alerts should be muted. require.Empty(t, active) + // The group should be marked as muted. + mutedBy, isMuted := marker.Muted("group1") + require.True(t, isMuted) + require.Equal(t, test.mutedBy, mutedBy) // Gets the metric for total suppressed notifications. require.NoError(t, prom_testutil.GatherAndCompare(r, strings.NewReader(fmt.Sprintf(` +# HELP alertmanager_marked_alerts How many alerts by state are currently marked in the Alertmanager regardless of their expiry. +# TYPE alertmanager_marked_alerts gauge +alertmanager_marked_alerts{state="active"} 0 +alertmanager_marked_alerts{state="suppressed"} 0 +alertmanager_marked_alerts{state="unprocessed"} 0 # HELP alertmanager_notifications_suppressed_total The total number of notifications suppressed for being silenced, inhibited, outside of active time intervals or within muted time intervals. # TYPE alertmanager_notifications_suppressed_total counter alertmanager_notifications_suppressed_total{reason="mute_time_interval"} %d @@ -994,6 +1014,7 @@ func TestTimeActiveStage(t *testing.T) { ctx := context.Background() ctx = WithNow(ctx, test.now) + ctx = WithGroupKey(ctx, "group1") ctx = WithActiveTimeIntervals(ctx, activeTimeIntervalNames) ctx = WithMuteTimeIntervals(ctx, nil) @@ -1003,14 +1024,33 @@ func TestTimeActiveStage(t *testing.T) { if len(test.mutedBy) == 0 { // All alerts should be active. require.Equal(t, len(test.alerts), len(active)) + // The group should not be marked. + mutedBy, isMuted := marker.Muted("group1") + require.False(t, isMuted) + require.Empty(t, mutedBy) // The metric for total suppressed notifications should not // have been incremented, which means it will not be collected. - require.NoError(t, prom_testutil.GatherAndCompare(r, strings.NewReader(""))) + require.NoError(t, prom_testutil.GatherAndCompare(r, strings.NewReader(` +# HELP alertmanager_marked_alerts How many alerts by state are currently marked in the Alertmanager regardless of their expiry. +# TYPE alertmanager_marked_alerts gauge +alertmanager_marked_alerts{state="active"} 0 +alertmanager_marked_alerts{state="suppressed"} 0 +alertmanager_marked_alerts{state="unprocessed"} 0 +`))) } else { // All alerts should be muted. require.Empty(t, active) + // The group should be marked as muted. + mutedBy, isMuted := marker.Muted("group1") + require.True(t, isMuted) + require.Equal(t, test.mutedBy, mutedBy) // Gets the metric for total suppressed notifications. require.NoError(t, prom_testutil.GatherAndCompare(r, strings.NewReader(fmt.Sprintf(` +# HELP alertmanager_marked_alerts How many alerts by state are currently marked in the Alertmanager regardless of their expiry. +# TYPE alertmanager_marked_alerts gauge +alertmanager_marked_alerts{state="active"} 0 +alertmanager_marked_alerts{state="suppressed"} 0 +alertmanager_marked_alerts{state="unprocessed"} 0 # HELP alertmanager_notifications_suppressed_total The total number of notifications suppressed for being silenced, inhibited, outside of active time intervals or within muted time intervals. # TYPE alertmanager_notifications_suppressed_total counter alertmanager_notifications_suppressed_total{reason="active_time_interval"} %d