Skip to content

Commit

Permalink
Fix child routes not inheriting time interval configuration
Browse files Browse the repository at this point in the history
Fixes a bug in the creation of routes which would mistakenly override parent route config with empty time interval configuration if none were specified on the child.

Signed-off-by: Ben Ridley <[email protected]>
  • Loading branch information
benridley committed Sep 28, 2023
1 parent c6be0bc commit 2269bbc
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 8 deletions.
21 changes: 13 additions & 8 deletions dispatch/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ import (
// DefaultRouteOpts are the defaulting routing options which apply
// to the root route of a routing tree.
var DefaultRouteOpts = RouteOpts{
GroupWait: 30 * time.Second,
GroupInterval: 5 * time.Minute,
RepeatInterval: 4 * time.Hour,
GroupBy: map[model.LabelName]struct{}{},
GroupByAll: false,
MuteTimeIntervals: []string{},
GroupWait: 30 * time.Second,
GroupInterval: 5 * time.Minute,
RepeatInterval: 4 * time.Hour,
GroupBy: map[model.LabelName]struct{}{},
GroupByAll: false,
MuteTimeIntervals: []string{},
ActiveTimeIntervals: []string{},
}

// A Route is a node that contains definitions of how to handle alerts.
Expand Down Expand Up @@ -117,8 +118,12 @@ func NewRoute(cr *config.Route, parent *Route) *Route {

sort.Sort(matchers)

opts.MuteTimeIntervals = cr.MuteTimeIntervals
opts.ActiveTimeIntervals = cr.ActiveTimeIntervals
if len(cr.MuteTimeIntervals) != 0 {
opts.MuteTimeIntervals = cr.MuteTimeIntervals
}
if len(cr.ActiveTimeIntervals) != 0 {
opts.ActiveTimeIntervals = cr.ActiveTimeIntervals
}

route := &Route{
parent: parent,
Expand Down
62 changes: 62 additions & 0 deletions dispatch/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,68 @@ routes:
require.Equal(t, child2.RouteOpts.GroupByAll, false)
}

func TestInheritParentMuteTimeIntervals(t *testing.T) {
in := `
routes:
- match:
env: 'parent'
group_by: ['...']
mute_time_intervals: ['weekend_mute']
routes:
- match:
env: 'child1'
- match:
env: 'child2'
mute_time_intervals: ['override_mute']
`

var ctree config.Route
if err := yaml.UnmarshalStrict([]byte(in), &ctree); err != nil {
t.Fatal(err)
}

tree := NewRoute(&ctree, nil)
parent := tree.Routes[0]
child1 := parent.Routes[0]
child2 := parent.Routes[1]
require.Equal(t, []string{"weekend_mute"}, parent.RouteOpts.MuteTimeIntervals)
require.Equal(t, []string{"weekend_mute"}, child1.RouteOpts.MuteTimeIntervals)
require.Equal(t, []string{"override_mute"}, child2.RouteOpts.MuteTimeIntervals)
}

func TestInheritParentActiveTimeIntervals(t *testing.T) {
in := `
routes:
- match:
env: 'parent'
group_by: ['...']
active_time_intervals: ['weekend_active']
routes:
- match:
env: 'child1'
- match:
env: 'child2'
active_time_intervals: ['override_active']
`

var ctree config.Route
if err := yaml.UnmarshalStrict([]byte(in), &ctree); err != nil {
t.Fatal(err)
}

tree := NewRoute(&ctree, nil)
parent := tree.Routes[0]
child1 := parent.Routes[0]
child2 := parent.Routes[1]
require.Equal(t, []string{"weekend_active"}, parent.RouteOpts.ActiveTimeIntervals)
require.Equal(t, []string{"weekend_active"}, child1.RouteOpts.ActiveTimeIntervals)
require.Equal(t, []string{"override_active"}, child2.RouteOpts.ActiveTimeIntervals)
}

func TestRouteMatchers(t *testing.T) {
in := `
receiver: 'notify-def'
Expand Down

0 comments on commit 2269bbc

Please sign in to comment.