-
Notifications
You must be signed in to change notification settings - Fork 2
/
metric.go
68 lines (59 loc) · 1.31 KB
/
metric.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
package snitch
import (
"strconv"
"strings"
"github.com/xjewer/snitch/lib/config"
)
type keyPath struct {
val string
match int
isVar bool
}
type metric struct {
keyPaths []keyPath
count bool
timing bool
timingData int
delimiter string
}
// makeMetrics makes metrics, that have to send to statsd with specific keys
func makeMetrics(keys []config.Key, prefix string) ([]*metric, error) {
metrics := make([]*metric, 0)
for _, k := range keys {
m := &metric{keyPaths: make([]keyPath, 0), count: k.Count}
if k.Timing != "" {
td, err := getVarName(k.Timing)
if err != nil {
return metrics, err
}
m.timing = true
m.timingData = td
}
m.keyPaths = append(m.keyPaths, keyPath{val: prefix})
for _, p := range strings.Split(k.Key, ".") {
if string(p[0]) == "$" {
match, err := getVarName(p)
if err != nil {
return metrics, err
}
m.keyPaths = append(m.keyPaths, keyPath{isVar: true, match: match})
} else {
m.keyPaths = append(m.keyPaths, keyPath{val: p})
}
}
m.delimiter = k.Delimiter
metrics = append(metrics, m)
}
return metrics, nil
}
// getVarName returns a var name
func getVarName(v string) (int, error) {
if len(v) <= 1 {
return 0, ErrEmptyVarName
}
n, err := strconv.Atoi(v[1:])
if err != nil {
return 0, err
}
return n, nil
}