Skip to content

Commit

Permalink
Update tests to check markers
Browse files Browse the repository at this point in the history
Signed-off-by: George Robinson <[email protected]>
  • Loading branch information
grobinson-grafana committed Apr 3, 2024
1 parent bd96c0b commit ff97f73
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
10 changes: 3 additions & 7 deletions notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
44 changes: 42 additions & 2 deletions notify/notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down

0 comments on commit ff97f73

Please sign in to comment.