Skip to content

Commit ff2401d

Browse files
Use routeID in aggrGroup and mute stages
Signed-off-by: George Robinson <george.robinson@grafana.com>
1 parent 6a4aff1 commit ff2401d

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

dispatch/dispatch_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -725,14 +725,14 @@ func TestDispatcher_DoMaintenance(t *testing.T) {
725725
go aggrGroup1.run(func(context.Context, ...*types.Alert) bool { return true })
726726

727727
// Insert a marker for the aggregation group's group key.
728-
marker.SetMuted(aggrGroup1.GroupKey(), []string{"weekends"})
729-
mutedBy, isMuted := marker.Muted(aggrGroup1.GroupKey())
728+
marker.SetMuted(route.ID(), aggrGroup1.GroupKey(), []string{"weekends"})
729+
mutedBy, isMuted := marker.Muted(route.ID(), aggrGroup1.GroupKey())
730730
require.True(t, isMuted)
731731
require.Equal(t, []string{"weekends"}, mutedBy)
732732

733733
// Run the maintenance and the marker should be removed.
734734
dispatcher.doMaintenance()
735-
mutedBy, isMuted = marker.Muted(aggrGroup1.GroupKey())
735+
mutedBy, isMuted = marker.Muted(route.ID(), aggrGroup1.GroupKey())
736736
require.False(t, isMuted)
737737
require.Empty(t, mutedBy)
738738
}

notify/notify.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ const (
119119
keyNow
120120
keyMuteTimeIntervals
121121
keyActiveTimeIntervals
122+
keyRouteID
122123
)
123124

124125
// WithReceiverName populates a context with a receiver name.
@@ -165,6 +166,10 @@ func WithActiveTimeIntervals(ctx context.Context, at []string) context.Context {
165166
return context.WithValue(ctx, keyActiveTimeIntervals, at)
166167
}
167168

169+
func WithRouteID(ctx context.Context, routeID string) context.Context {
170+
return context.WithValue(ctx, keyRouteID, routeID)
171+
}
172+
168173
// RepeatInterval extracts a repeat interval from the context. Iff none exists, the
169174
// second argument is false.
170175
func RepeatInterval(ctx context.Context) (time.Duration, bool) {
@@ -228,6 +233,13 @@ func ActiveTimeIntervalNames(ctx context.Context) ([]string, bool) {
228233
return v, ok
229234
}
230235

236+
// RouteID extracts a RouteID from the context. Iff none exists, the
237+
// // second argument is false.
238+
func RouteID(ctx context.Context) (string, bool) {
239+
v, ok := ctx.Value(keyRouteID).(string)
240+
return v, ok
241+
}
242+
231243
// A Stage processes alerts under the constraints of the given context.
232244
type Stage interface {
233245
Exec(ctx context.Context, l log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error)
@@ -937,6 +949,11 @@ func NewTimeMuteStage(muter types.TimeMuter, marker types.GroupMarker, metrics *
937949
// Exec implements the stage interface for TimeMuteStage.
938950
// TimeMuteStage is responsible for muting alerts whose route is not in an active time.
939951
func (tms TimeMuteStage) Exec(ctx context.Context, l log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error) {
952+
routeID, ok := RouteID(ctx)
953+
if !ok {
954+
return ctx, nil, errors.New("route ID missing")
955+
}
956+
940957
gkey, ok := GroupKey(ctx)
941958
if !ok {
942959
return ctx, nil, errors.New("group key missing")
@@ -961,7 +978,7 @@ func (tms TimeMuteStage) Exec(ctx context.Context, l log.Logger, alerts ...*type
961978
return ctx, alerts, err
962979
}
963980
// If muted is false then mutedBy is nil and the muted marker is removed.
964-
tms.marker.SetMuted(gkey, mutedBy)
981+
tms.marker.SetMuted(routeID, gkey, mutedBy)
965982

966983
// If the current time is inside a mute time, all alerts are removed from the pipeline.
967984
if muted {
@@ -982,6 +999,11 @@ func NewTimeActiveStage(muter types.TimeMuter, marker types.GroupMarker, metrics
982999
// Exec implements the stage interface for TimeActiveStage.
9831000
// TimeActiveStage is responsible for muting alerts whose route is not in an active time.
9841001
func (tas TimeActiveStage) Exec(ctx context.Context, l log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error) {
1002+
routeID, ok := RouteID(ctx)
1003+
if !ok {
1004+
return ctx, nil, errors.New("route ID missing")
1005+
}
1006+
9851007
gkey, ok := GroupKey(ctx)
9861008
if !ok {
9871009
return ctx, nil, errors.New("group key missing")
@@ -1014,7 +1036,7 @@ func (tas TimeActiveStage) Exec(ctx context.Context, l log.Logger, alerts ...*ty
10141036
// to be active.
10151037
mutedBy = activeTimeIntervalNames
10161038
}
1017-
tas.marker.SetMuted(gkey, mutedBy)
1039+
tas.marker.SetMuted(routeID, gkey, mutedBy)
10181040

10191041
// If the current time is not inside an active time, all alerts are removed from the pipeline
10201042
if !active {

notify/notify_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,7 @@ func TestTimeMuteStage(t *testing.T) {
906906
ctx = WithGroupKey(ctx, "group1")
907907
ctx = WithActiveTimeIntervals(ctx, nil)
908908
ctx = WithMuteTimeIntervals(ctx, muteTimeIntervalNames)
909+
ctx = WithRouteID(ctx, "route1")
909910

910911
_, active, err := st.Exec(ctx, log.NewNopLogger(), test.alerts...)
911912
require.NoError(t, err)
@@ -914,7 +915,7 @@ func TestTimeMuteStage(t *testing.T) {
914915
// All alerts should be active.
915916
require.Equal(t, len(test.alerts), len(active))
916917
// The group should not be marked.
917-
mutedBy, isMuted := marker.Muted("group1")
918+
mutedBy, isMuted := marker.Muted("route1", "group1")
918919
require.False(t, isMuted)
919920
require.Empty(t, mutedBy)
920921
// The metric for total suppressed notifications should not
@@ -930,7 +931,7 @@ alertmanager_marked_alerts{state="unprocessed"} 0
930931
// All alerts should be muted.
931932
require.Empty(t, active)
932933
// The group should be marked as muted.
933-
mutedBy, isMuted := marker.Muted("group1")
934+
mutedBy, isMuted := marker.Muted("route1", "group1")
934935
require.True(t, isMuted)
935936
require.Equal(t, test.mutedBy, mutedBy)
936937
// Gets the metric for total suppressed notifications.
@@ -1023,6 +1024,7 @@ func TestTimeActiveStage(t *testing.T) {
10231024
ctx = WithGroupKey(ctx, "group1")
10241025
ctx = WithActiveTimeIntervals(ctx, activeTimeIntervalNames)
10251026
ctx = WithMuteTimeIntervals(ctx, nil)
1027+
ctx = WithRouteID(ctx, "route1")
10261028

10271029
_, active, err := st.Exec(ctx, log.NewNopLogger(), test.alerts...)
10281030
require.NoError(t, err)
@@ -1031,7 +1033,7 @@ func TestTimeActiveStage(t *testing.T) {
10311033
// All alerts should be active.
10321034
require.Equal(t, len(test.alerts), len(active))
10331035
// The group should not be marked.
1034-
mutedBy, isMuted := marker.Muted("group1")
1036+
mutedBy, isMuted := marker.Muted("route1", "group1")
10351037
require.False(t, isMuted)
10361038
require.Empty(t, mutedBy)
10371039
// The metric for total suppressed notifications should not
@@ -1047,7 +1049,7 @@ alertmanager_marked_alerts{state="unprocessed"} 0
10471049
// All alerts should be muted.
10481050
require.Empty(t, active)
10491051
// The group should be marked as muted.
1050-
mutedBy, isMuted := marker.Muted("group1")
1052+
mutedBy, isMuted := marker.Muted("route1", "group1")
10511053
require.True(t, isMuted)
10521054
require.Equal(t, test.mutedBy, mutedBy)
10531055
// Gets the metric for total suppressed notifications.

0 commit comments

Comments
 (0)