Skip to content

Commit

Permalink
Add routeID to GroupMarker interface
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 29, 2024
1 parent c02de3c commit 0d47e10
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
20 changes: 13 additions & 7 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,22 @@ type AlertMarker interface {
Inhibited(model.Fingerprint) ([]string, bool)
}

// GroupMarker helps to mark groups as active or muted.
// All methods are goroutine-safe.
//
// TODO(grobinson): routeID is used in Muted and SetMuted because groupKey
// is not unique (see #3817). Once groupKey uniqueness is fixed routeID can
// be removed from the GroupMarker interface.
type GroupMarker interface {
// Muted returns true if the group is muted, otherwise false. If the group
// is muted then it also returns the names of the time intervals that muted
// it.
Muted(groupKey string) ([]string, bool)
Muted(routeID, groupKey string) ([]string, bool)

// SetMuted marks the group as muted, and sets the names of the time
// intervals that mute it. If the list of names is nil or the empty slice
// then the muted marker is removed.
SetMuted(groupKey string, timeIntervalNames []string)
SetMuted(routeID, groupKey string, timeIntervalNames []string)
}

// NewMarker returns an instance of a AlertMarker implementation.
Expand All @@ -130,24 +136,24 @@ type MemMarker struct {
}

// Muted implements GroupMarker.
func (m *MemMarker) Muted(groupKey string) ([]string, bool) {
func (m *MemMarker) Muted(routeID, groupKey string) ([]string, bool) {
m.mtx.Lock()
defer m.mtx.Unlock()
status, ok := m.groups[groupKey]
status, ok := m.groups[routeID+groupKey]
if !ok {
return nil, false
}
return status.mutedBy, len(status.mutedBy) > 0
}

// SetMuted implements GroupMarker.
func (m *MemMarker) SetMuted(groupKey string, timeIntervalNames []string) {
func (m *MemMarker) SetMuted(routeID, groupKey string, timeIntervalNames []string) {
m.mtx.Lock()
defer m.mtx.Unlock()
status, ok := m.groups[groupKey]
status, ok := m.groups[routeID+groupKey]
if !ok {
status = &groupStatus{}
m.groups[groupKey] = status
m.groups[routeID+groupKey] = status
}
status.mutedBy = timeIntervalNames
}
Expand Down
17 changes: 11 additions & 6 deletions types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,29 @@ func TestMemMarker_Muted(t *testing.T) {
marker := NewMarker(r)

// No groups should be muted.
timeIntervalNames, isMuted := marker.Muted("group1")
timeIntervalNames, isMuted := marker.Muted("route1", "group1")
require.False(t, isMuted)
require.Empty(t, timeIntervalNames)

// Mark the group as muted because it's the weekend.
marker.SetMuted("group1", []string{"weekends"})
timeIntervalNames, isMuted = marker.Muted("group1")
marker.SetMuted("route1", "group1", []string{"weekends"})
timeIntervalNames, isMuted = marker.Muted("route1", "group1")
require.True(t, isMuted)
require.Equal(t, []string{"weekends"}, timeIntervalNames)

// Other groups should not be marked as muted.
timeIntervalNames, isMuted = marker.Muted("group2")
timeIntervalNames, isMuted = marker.Muted("route1", "group2")
require.False(t, isMuted)
require.Empty(t, timeIntervalNames)

// Other routes should not be marked as muted either.
timeIntervalNames, isMuted = marker.Muted("route2", "group1")
require.False(t, isMuted)
require.Empty(t, timeIntervalNames)

// The group is no longer muted.
marker.SetMuted("group1", nil)
timeIntervalNames, isMuted = marker.Muted("group1")
marker.SetMuted("route1", "group1", nil)
timeIntervalNames, isMuted = marker.Muted("route1", "group1")
require.False(t, isMuted)
require.Empty(t, timeIntervalNames)
}
Expand Down

0 comments on commit 0d47e10

Please sign in to comment.