Skip to content

Commit a007656

Browse files
qinxx108alexweav
authored andcommitted
Add the route ID to uuid (prometheus#3372)
* Add the route ID to uuid Signed-off-by: Yijie Qin <[email protected]> --------- Signed-off-by: Yijie Qin <[email protected]>
1 parent 9a69b8f commit a007656

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

dispatch/route.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,29 @@ func (r *Route) Key() string {
180180
return b.String()
181181
}
182182

183+
// ID returns a unique identifier for the route.
184+
func (r *Route) ID() string {
185+
b := strings.Builder{}
186+
187+
position := -1
188+
if r.parent != nil {
189+
// Find the position in the same level leaf.
190+
for i, cr := range r.parent.Routes {
191+
if cr == r {
192+
position = i
193+
break
194+
}
195+
}
196+
}
197+
b.WriteString(r.Key())
198+
199+
if position > -1 {
200+
b.WriteRune('/')
201+
b.WriteString(fmt.Sprint(position))
202+
}
203+
return b.String()
204+
}
205+
183206
// Walk traverses the route tree in depth-first order.
184207
func (r *Route) Walk(visit func(*Route)) {
185208
visit(r)

dispatch/route_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,3 +853,71 @@ routes:
853853
}
854854
}
855855
}
856+
857+
func TestRouteID(t *testing.T) {
858+
in := `
859+
receiver: 'notify-def'
860+
861+
routes:
862+
- matchers: ['{owner="team-A"}', '{level!="critical"}']
863+
receiver: 'notify-D'
864+
group_by: [...]
865+
continue: true
866+
- matchers: ['{owner="team-A"}', '{level!="critical"}']
867+
receiver: 'notify-A'
868+
routes:
869+
- matchers: ['{env="testing"}', '{baz!~".*quux"}']
870+
receiver: 'notify-testing'
871+
group_by: [...]
872+
- match:
873+
env: "production"
874+
receiver: 'notify-productionA'
875+
group_wait: 1m
876+
continue: true
877+
- matchers: [ env=~"produ.*", job=~".*"]
878+
receiver: 'notify-productionB'
879+
group_wait: 30s
880+
group_interval: 5m
881+
repeat_interval: 1h
882+
group_by: ['job']
883+
- match_re:
884+
owner: 'team-(B|C)'
885+
group_by: ['foo', 'bar']
886+
group_wait: 2m
887+
receiver: 'notify-BC'
888+
- matchers: [group_by="role"]
889+
group_by: ['role']
890+
routes:
891+
- matchers: ['{env="testing"}']
892+
receiver: 'notify-testing'
893+
routes:
894+
- matchers: [wait="long"]
895+
group_wait: 2m
896+
`
897+
898+
var ctree config.Route
899+
if err := yaml.UnmarshalStrict([]byte(in), &ctree); err != nil {
900+
t.Fatal(err)
901+
}
902+
tree := NewRoute(&ctree, nil)
903+
904+
expected := []string{
905+
"{}",
906+
"{}/{level!=\"critical\",owner=\"team-A\"}/0",
907+
"{}/{level!=\"critical\",owner=\"team-A\"}/1",
908+
"{}/{level!=\"critical\",owner=\"team-A\"}/{baz!~\".*quux\",env=\"testing\"}/0",
909+
"{}/{level!=\"critical\",owner=\"team-A\"}/{env=\"production\"}/1",
910+
"{}/{level!=\"critical\",owner=\"team-A\"}/{env=~\"produ.*\",job=~\".*\"}/2",
911+
"{}/{owner=~\"^(?:team-(B|C))$\"}/2",
912+
"{}/{group_by=\"role\"}/3",
913+
"{}/{group_by=\"role\"}/{env=\"testing\"}/0",
914+
"{}/{group_by=\"role\"}/{env=\"testing\"}/{wait=\"long\"}/0",
915+
}
916+
917+
var got []string
918+
tree.Walk(func(r *Route) {
919+
got = append(got, r.ID())
920+
})
921+
922+
require.ElementsMatch(t, got, expected)
923+
}

0 commit comments

Comments
 (0)