-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
79 lines (67 loc) · 1.45 KB
/
config.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package mscraper
import (
"encoding/json"
"io/ioutil"
"regexp"
"time"
)
type Resource struct {
Type string `json:"type"`
Labels map[string]string `json:"labels"`
}
type Metric struct {
Prefix []string `json:"prefix"`
Labels map[string]string `json:"labels"`
Filter string `json:"filter"`
}
type ConfigExpr struct {
Target string `json:"target"`
Resource Resource `json:"resource"`
Metric Metric `json:"metric"`
Interval string `json:"interval"`
}
type Config struct {
Target string
Resource Resource
Metric Metric
Prefix []string
Interval time.Duration
Project string
Filter *regexp.Regexp
}
func ReadConfig(path string) ([]Config, error) {
bytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
var cfgs []ConfigExpr
if err := json.Unmarshal(bytes, &cfgs); err != nil {
return nil, err
}
ret := make([]Config, 0, len(cfgs))
for _, cfg := range cfgs {
duration, err := time.ParseDuration(cfg.Interval)
if err != nil {
return nil, err
}
project := cfg.Resource.Labels["project_id"]
filterStr := cfg.Metric.Filter
if filterStr == "" {
filterStr = "^.+$"
}
filter, err := regexp.Compile(filterStr)
if err != nil {
return nil, err
}
cfg := Config{
Target: cfg.Target,
Resource: cfg.Resource,
Metric: cfg.Metric,
Interval: duration,
Project: project,
Filter: filter,
}
ret = append(ret, cfg)
}
return ret, nil
}