forked from Showmax/prometheus-pusher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route_map.go
61 lines (53 loc) · 1.14 KB
/
route_map.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"bufio"
"os"
"strings"
iradix "github.com/hashicorp/go-immutable-radix"
)
// route map type
//
// TODO: store pointers to strings as values in radix tree
//
type routeMap struct {
*iradix.Tree
defaultRoute []string
}
// creates routeMap instance from a route_map config file
//
func newRouteMap(file string, dr string) *routeMap {
m := iradix.New()
if _, err := os.Stat(file); os.IsNotExist(err) {
r := &routeMap{m, strings.Split(dr, ",")}
return r
}
fd, err := os.Open(file)
if err != nil {
logger.Fatalf("Failed to parse route map %s - %s", file, err.Error())
return nil
}
sc := bufio.NewScanner(fd)
for sc.Scan() {
if sc.Text() == "" {
continue
}
if strings.HasPrefix(sc.Text(), "#") {
continue
}
elem := strings.Fields(sc.Text())
rt := strings.Split(elem[1], ",")
m, _, _ = m.Insert([]byte(elem[0]), rt)
}
r := &routeMap{m, strings.Split(dr, ",")}
return r
}
// calculates route for given metric name
//
func (r *routeMap) route(name []byte) []string {
_, value, _ := r.Root().LongestPrefix(name)
rt, _ := value.([]string)
if len(rt) == 0 {
return r.defaultRoute
}
return rt
}