2015-10-11 15:24:49 +00:00
|
|
|
// Copyright 2015 Prometheus Team
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-08-09 09:04:01 +00:00
|
|
|
package dispatch
|
2015-07-01 15:56:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2015-07-01 16:24:08 +00:00
|
|
|
"time"
|
2015-07-01 15:56:53 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
"gopkg.in/yaml.v2"
|
2015-09-25 16:14:46 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/alertmanager/config"
|
2015-07-01 15:56:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRouteMatch(t *testing.T) {
|
|
|
|
in := `
|
2015-11-10 13:08:20 +00:00
|
|
|
receiver: 'notify-def'
|
2015-07-01 15:56:53 +00:00
|
|
|
|
|
|
|
routes:
|
|
|
|
- match:
|
|
|
|
owner: 'team-A'
|
|
|
|
|
2015-11-10 13:08:20 +00:00
|
|
|
receiver: 'notify-A'
|
2015-07-01 15:56:53 +00:00
|
|
|
|
2015-07-01 16:24:08 +00:00
|
|
|
routes:
|
|
|
|
- match:
|
|
|
|
env: 'testing'
|
|
|
|
|
2015-11-10 13:08:20 +00:00
|
|
|
receiver: 'notify-testing'
|
2018-11-29 11:31:14 +00:00
|
|
|
group_by: [...]
|
2015-07-01 16:24:08 +00:00
|
|
|
|
|
|
|
- match:
|
|
|
|
env: "production"
|
|
|
|
|
2015-11-10 13:08:20 +00:00
|
|
|
receiver: 'notify-productionA'
|
2015-07-01 16:24:08 +00:00
|
|
|
group_wait: 1m
|
|
|
|
|
|
|
|
continue: true
|
|
|
|
|
|
|
|
- match_re:
|
2015-11-20 14:10:38 +00:00
|
|
|
env: "produ.*"
|
2017-06-23 13:07:50 +00:00
|
|
|
job: ".*"
|
2015-07-01 16:24:08 +00:00
|
|
|
|
2015-11-10 13:08:20 +00:00
|
|
|
receiver: 'notify-productionB'
|
2015-10-08 08:50:37 +00:00
|
|
|
group_wait: 30s
|
|
|
|
group_interval: 5m
|
|
|
|
repeat_interval: 1h
|
2015-07-01 16:24:08 +00:00
|
|
|
group_by: ['job']
|
|
|
|
|
|
|
|
|
2015-07-01 15:56:53 +00:00
|
|
|
- match_re:
|
2015-11-20 14:10:38 +00:00
|
|
|
owner: 'team-(B|C)'
|
2015-07-01 15:56:53 +00:00
|
|
|
|
2015-07-01 16:24:08 +00:00
|
|
|
group_by: ['foo', 'bar']
|
|
|
|
group_wait: 2m
|
2015-11-10 13:08:20 +00:00
|
|
|
receiver: 'notify-BC'
|
2016-06-04 09:08:08 +00:00
|
|
|
|
|
|
|
- match:
|
|
|
|
group_by: 'role'
|
|
|
|
group_by: ['role']
|
|
|
|
|
|
|
|
routes:
|
|
|
|
- match:
|
|
|
|
env: 'testing'
|
|
|
|
receiver: 'notify-testing'
|
|
|
|
routes:
|
|
|
|
- match:
|
|
|
|
wait: 'long'
|
|
|
|
group_wait: 2m
|
2015-07-01 15:56:53 +00:00
|
|
|
`
|
|
|
|
|
2015-09-25 16:14:46 +00:00
|
|
|
var ctree config.Route
|
2018-04-17 14:22:46 +00:00
|
|
|
if err := yaml.UnmarshalStrict([]byte(in), &ctree); err != nil {
|
2015-07-01 15:56:53 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-10-08 08:50:37 +00:00
|
|
|
var (
|
2015-10-19 15:35:59 +00:00
|
|
|
def = DefaultRouteOpts
|
2015-10-21 11:08:53 +00:00
|
|
|
tree = NewRoute(&ctree, nil)
|
2015-10-08 08:50:37 +00:00
|
|
|
)
|
2015-07-01 16:24:08 +00:00
|
|
|
lset := func(labels ...string) map[model.LabelName]struct{} {
|
|
|
|
s := map[model.LabelName]struct{}{}
|
|
|
|
for _, ls := range labels {
|
|
|
|
s[model.LabelName(ls)] = struct{}{}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2015-07-01 15:56:53 +00:00
|
|
|
tests := []struct {
|
|
|
|
input model.LabelSet
|
|
|
|
result []*RouteOpts
|
2017-06-23 13:07:50 +00:00
|
|
|
keys []string
|
2015-07-01 15:56:53 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: model.LabelSet{
|
|
|
|
"owner": "team-A",
|
|
|
|
},
|
|
|
|
result: []*RouteOpts{
|
|
|
|
{
|
2015-11-10 12:47:04 +00:00
|
|
|
Receiver: "notify-A",
|
2015-10-19 15:35:59 +00:00
|
|
|
GroupBy: def.GroupBy,
|
2018-11-29 11:31:14 +00:00
|
|
|
GroupByAll: false,
|
2015-10-08 08:50:37 +00:00
|
|
|
GroupWait: def.GroupWait,
|
|
|
|
GroupInterval: def.GroupInterval,
|
|
|
|
RepeatInterval: def.RepeatInterval,
|
2015-07-01 16:24:08 +00:00
|
|
|
},
|
|
|
|
},
|
2017-06-23 13:07:50 +00:00
|
|
|
keys: []string{"{}/{owner=\"team-A\"}"},
|
2015-07-01 16:24:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: model.LabelSet{
|
|
|
|
"owner": "team-A",
|
|
|
|
"env": "unset",
|
|
|
|
},
|
|
|
|
result: []*RouteOpts{
|
|
|
|
{
|
2015-11-10 12:47:04 +00:00
|
|
|
Receiver: "notify-A",
|
2015-10-19 15:35:59 +00:00
|
|
|
GroupBy: def.GroupBy,
|
2018-11-29 11:31:14 +00:00
|
|
|
GroupByAll: false,
|
2015-10-08 08:50:37 +00:00
|
|
|
GroupWait: def.GroupWait,
|
|
|
|
GroupInterval: def.GroupInterval,
|
|
|
|
RepeatInterval: def.RepeatInterval,
|
2015-07-01 16:24:08 +00:00
|
|
|
},
|
|
|
|
},
|
2017-06-23 13:07:50 +00:00
|
|
|
keys: []string{"{}/{owner=\"team-A\"}"},
|
2015-07-01 16:24:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: model.LabelSet{
|
|
|
|
"owner": "team-C",
|
|
|
|
},
|
|
|
|
result: []*RouteOpts{
|
|
|
|
{
|
2015-11-10 12:47:04 +00:00
|
|
|
Receiver: "notify-BC",
|
2015-10-08 08:50:37 +00:00
|
|
|
GroupBy: lset("foo", "bar"),
|
2018-11-29 11:31:14 +00:00
|
|
|
GroupByAll: false,
|
2015-10-08 08:50:37 +00:00
|
|
|
GroupWait: 2 * time.Minute,
|
|
|
|
GroupInterval: def.GroupInterval,
|
|
|
|
RepeatInterval: def.RepeatInterval,
|
2015-07-01 16:24:08 +00:00
|
|
|
},
|
|
|
|
},
|
2017-06-23 13:07:50 +00:00
|
|
|
keys: []string{"{}/{owner=~\"^(?:team-(B|C))$\"}"},
|
2015-07-01 16:24:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: model.LabelSet{
|
|
|
|
"owner": "team-A",
|
|
|
|
"env": "testing",
|
|
|
|
},
|
|
|
|
result: []*RouteOpts{
|
|
|
|
{
|
2015-11-10 12:47:04 +00:00
|
|
|
Receiver: "notify-testing",
|
2015-10-08 08:50:37 +00:00
|
|
|
GroupBy: lset(),
|
2018-11-29 11:31:14 +00:00
|
|
|
GroupByAll: true,
|
2015-10-08 08:50:37 +00:00
|
|
|
GroupWait: def.GroupWait,
|
|
|
|
GroupInterval: def.GroupInterval,
|
|
|
|
RepeatInterval: def.RepeatInterval,
|
2015-07-01 16:24:08 +00:00
|
|
|
},
|
|
|
|
},
|
2017-06-23 13:07:50 +00:00
|
|
|
keys: []string{"{}/{owner=\"team-A\"}/{env=\"testing\"}"},
|
2015-07-01 16:24:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: model.LabelSet{
|
|
|
|
"owner": "team-A",
|
|
|
|
"env": "production",
|
|
|
|
},
|
|
|
|
result: []*RouteOpts{
|
|
|
|
{
|
2015-11-10 12:47:04 +00:00
|
|
|
Receiver: "notify-productionA",
|
2015-10-19 15:35:59 +00:00
|
|
|
GroupBy: def.GroupBy,
|
2018-11-29 11:31:14 +00:00
|
|
|
GroupByAll: false,
|
2015-10-08 08:50:37 +00:00
|
|
|
GroupWait: 1 * time.Minute,
|
|
|
|
GroupInterval: def.GroupInterval,
|
|
|
|
RepeatInterval: def.RepeatInterval,
|
2015-07-01 16:24:08 +00:00
|
|
|
},
|
|
|
|
{
|
2015-11-10 12:47:04 +00:00
|
|
|
Receiver: "notify-productionB",
|
2015-10-08 08:50:37 +00:00
|
|
|
GroupBy: lset("job"),
|
2018-11-29 11:31:14 +00:00
|
|
|
GroupByAll: false,
|
2015-10-08 08:50:37 +00:00
|
|
|
GroupWait: 30 * time.Second,
|
|
|
|
GroupInterval: 5 * time.Minute,
|
|
|
|
RepeatInterval: 1 * time.Hour,
|
2015-07-01 15:56:53 +00:00
|
|
|
},
|
|
|
|
},
|
2017-06-23 13:07:50 +00:00
|
|
|
keys: []string{
|
|
|
|
"{}/{owner=\"team-A\"}/{env=\"production\"}",
|
|
|
|
"{}/{owner=\"team-A\"}/{env=~\"^(?:produ.*)$\",job=~\"^(?:.*)$\"}",
|
|
|
|
},
|
2015-07-01 15:56:53 +00:00
|
|
|
},
|
2016-06-04 09:08:08 +00:00
|
|
|
{
|
|
|
|
input: model.LabelSet{
|
|
|
|
"group_by": "role",
|
|
|
|
},
|
|
|
|
result: []*RouteOpts{
|
|
|
|
{
|
|
|
|
Receiver: "notify-def",
|
|
|
|
GroupBy: lset("role"),
|
2018-11-29 11:31:14 +00:00
|
|
|
GroupByAll: false,
|
2016-06-04 09:08:08 +00:00
|
|
|
GroupWait: def.GroupWait,
|
|
|
|
GroupInterval: def.GroupInterval,
|
|
|
|
RepeatInterval: def.RepeatInterval,
|
|
|
|
},
|
|
|
|
},
|
2017-06-23 13:07:50 +00:00
|
|
|
keys: []string{"{}/{group_by=\"role\"}"},
|
2016-06-04 09:08:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: model.LabelSet{
|
|
|
|
"env": "testing",
|
|
|
|
"group_by": "role",
|
|
|
|
},
|
|
|
|
result: []*RouteOpts{
|
|
|
|
{
|
|
|
|
Receiver: "notify-testing",
|
|
|
|
GroupBy: lset("role"),
|
2018-11-29 11:31:14 +00:00
|
|
|
GroupByAll: false,
|
2016-06-04 09:08:08 +00:00
|
|
|
GroupWait: def.GroupWait,
|
|
|
|
GroupInterval: def.GroupInterval,
|
|
|
|
RepeatInterval: def.RepeatInterval,
|
|
|
|
},
|
|
|
|
},
|
2017-06-23 13:07:50 +00:00
|
|
|
keys: []string{"{}/{group_by=\"role\"}/{env=\"testing\"}"},
|
2016-06-04 09:08:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: model.LabelSet{
|
|
|
|
"env": "testing",
|
|
|
|
"group_by": "role",
|
|
|
|
"wait": "long",
|
|
|
|
},
|
|
|
|
result: []*RouteOpts{
|
|
|
|
{
|
|
|
|
Receiver: "notify-testing",
|
|
|
|
GroupBy: lset("role"),
|
2018-11-29 11:31:14 +00:00
|
|
|
GroupByAll: false,
|
2016-06-04 09:08:08 +00:00
|
|
|
GroupWait: 2 * time.Minute,
|
|
|
|
GroupInterval: def.GroupInterval,
|
|
|
|
RepeatInterval: def.RepeatInterval,
|
|
|
|
},
|
|
|
|
},
|
2017-06-23 13:07:50 +00:00
|
|
|
keys: []string{"{}/{group_by=\"role\"}/{env=\"testing\"}/{wait=\"long\"}"},
|
2016-06-04 09:08:08 +00:00
|
|
|
},
|
2015-07-01 15:56:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2015-11-07 13:30:21 +00:00
|
|
|
var matches []*RouteOpts
|
2017-06-23 13:07:50 +00:00
|
|
|
var keys []string
|
|
|
|
|
2015-11-07 13:30:21 +00:00
|
|
|
for _, r := range tree.Match(test.input) {
|
|
|
|
matches = append(matches, &r.RouteOpts)
|
2017-06-23 13:07:50 +00:00
|
|
|
keys = append(keys, r.Key())
|
2015-11-07 13:30:21 +00:00
|
|
|
}
|
2015-07-01 15:56:53 +00:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(matches, test.result) {
|
2015-09-25 16:14:46 +00:00
|
|
|
t.Errorf("\nexpected:\n%v\ngot:\n%v", test.result, matches)
|
2015-07-01 15:56:53 +00:00
|
|
|
}
|
2017-06-23 13:07:50 +00:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(keys, test.keys) {
|
|
|
|
t.Errorf("\nexpected:\n%v\ngot:\n%v", test.keys, keys)
|
|
|
|
}
|
2015-07-01 15:56:53 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-20 14:27:58 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|