From 2269bbc03b75c4734dddb97197e415ec11e309bf Mon Sep 17 00:00:00 2001 From: Ben Ridley Date: Thu, 28 Sep 2023 15:25:00 +1000 Subject: [PATCH] Fix child routes not inheriting time interval configuration 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 --- dispatch/route.go | 21 ++++++++------ dispatch/route_test.go | 62 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 8 deletions(-) diff --git a/dispatch/route.go b/dispatch/route.go index 4b3673c531..bb0f322e79 100644 --- a/dispatch/route.go +++ b/dispatch/route.go @@ -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. @@ -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, diff --git a/dispatch/route_test.go b/dispatch/route_test.go index e633ae5f6f..c0a036238c 100644 --- a/dispatch/route_test.go +++ b/dispatch/route_test.go @@ -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'