Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move walkRoute to dispatch package. #2136

Merged
merged 1 commit into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions cmd/alertmanager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,6 @@ func buildReceiverIntegrations(nc *config.Receiver, tmpl *template.Template, log
return integrations, nil
}

// walkRoute traverses the route tree in depth-first order.
func walkRoute(r *dispatch.Route, visit func(*dispatch.Route)) {
visit(r)
if r.Routes == nil {
return
}
for i := range r.Routes {
walkRoute(r.Routes[i], visit)
}
}

func main() {
os.Exit(run())
}
Expand Down Expand Up @@ -403,7 +392,7 @@ func run() int {
// Build the routing tree and record which receivers are used.
routes := dispatch.NewRoute(conf.Route, nil)
activeReceivers := make(map[string]struct{})
walkRoute(routes, func(r *dispatch.Route) {
routes.Walk(func(r *dispatch.Route) {
activeReceivers[r.RouteOpts.Receiver] = struct{}{}
})

Expand Down Expand Up @@ -447,7 +436,7 @@ func run() int {
})

disp = dispatch.NewDispatcher(alerts, routes, pipeline, marker, timeoutFunc, logger, dispMetrics)
walkRoute(routes, func(r *dispatch.Route) {
routes.Walk(func(r *dispatch.Route) {
if r.RouteOpts.RepeatInterval > *retention {
level.Warn(configLogger).Log(
"msg",
Expand Down
11 changes: 11 additions & 0 deletions dispatch/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ func (r *Route) Key() string {
return b.String()
}

// Walk traverses the route tree in depth-first order.
func (r *Route) Walk(visit func(*Route)) {
visit(r)
if r.Routes == nil {
return
}
for i := range r.Routes {
r.Routes[i].Walk(visit)
}
}

// RouteOpts holds various routing options necessary for processing alerts
// that match a given route.
type RouteOpts struct {
Expand Down
85 changes: 85 additions & 0 deletions dispatch/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,88 @@ routes:
}
}
}

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

routes:
- match:
owner: 'team-A'

receiver: 'notify-A'

routes:
- match:
env: 'testing'

receiver: 'notify-testing'
group_by: [...]

- match:
env: "production"

receiver: 'notify-productionA'
group_wait: 1m

continue: true

- match_re:
env: "produ.*"
job: ".*"

receiver: 'notify-productionB'
group_wait: 30s
group_interval: 5m
repeat_interval: 1h
group_by: ['job']


- match_re:
owner: 'team-(B|C)'

group_by: ['foo', 'bar']
group_wait: 2m
receiver: 'notify-BC'

- match:
group_by: 'role'
group_by: ['role']

routes:
- match:
env: 'testing'
receiver: 'notify-testing'
routes:
- match:
wait: 'long'
group_wait: 2m
`

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

expected := []string{
"notify-def",
"notify-A",
"notify-testing",
"notify-productionA",
"notify-productionB",
"notify-BC",
"notify-def",
"notify-testing",
"notify-testing",
}

var got []string
tree.Walk(func(r *Route) {
got = append(got, r.RouteOpts.Receiver)
})

if !reflect.DeepEqual(got, expected) {
t.Errorf("\nexpected:\n%v\ngot:\n%v", expected, got)
}
}