-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
176 lines (160 loc) · 3.68 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"fmt"
"io/ioutil"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
type PatternConfig struct {
Match string
MatchCompiled *regexp.Regexp `yaml:"-"`
Metric string
Type string
Help string
Action string
Value string
Continue bool
Labels map[string]string
CounterVec *prometheus.CounterVec
GaugeVec *prometheus.GaugeVec
}
type LogConfig struct {
Path string
Patterns []*PatternConfig
}
type Config struct {
sync.Mutex
Logs []LogConfig
}
func (c *Config) load(path string) error {
c.Lock()
defer c.Unlock()
content, err := ioutil.ReadFile(path)
if err != nil {
return fmt.Errorf("cannot read file '%s': %s", path, err)
}
log.WithFields(log.Fields{"configFile": path}).Info("Loading config file")
err = yaml.UnmarshalStrict(content, &c)
if err != nil {
return fmt.Errorf("yaml parse error: %s, failed to load config", err)
}
if len(c.Logs) < 1 {
return fmt.Errorf("no Logs configured, cannot do anything")
}
return nil
}
func (pc *PatternConfig) compile() error {
err := pc.validateTypeAction()
if err != nil {
return err
}
err = pc.compileMatch()
if err != nil {
return err
}
err = pc.validateValue()
if err != nil {
return err
}
err = pc.validateLabels()
if err != nil {
return err
}
return nil
}
func (pc *PatternConfig) validateTypeAction() error {
switch pc.Type {
case "counter":
switch pc.Action {
case "inc":
default:
return fmt.Errorf("action '%s' is unsupported for counters", pc.Action)
}
case "gauge":
switch pc.Action {
case "inc":
case "dec":
case "set":
default:
return fmt.Errorf("action '%s' is unsupported for counters", pc.Action)
}
default:
return fmt.Errorf("unsupported type '%s'", pc.Type)
}
return nil
}
func (pc *PatternConfig) validateValue() error {
numGroups := pc.MatchCompiled.NumSubexp()
dummyMatches := []string{}
for x := 0; x < numGroups+1; x++ {
dummyMatches = append(dummyMatches, "0")
}
_, err := pc.eval(dummyMatches)
return err
}
func (pc *PatternConfig) validateLabels() error {
numGroups := pc.MatchCompiled.NumSubexp()
dummyMatches := []string{}
for x := 0; x < numGroups+1; x++ {
dummyMatches = append(dummyMatches, "0")
}
_, err := pc.getEvaluatedLabels(dummyMatches)
return err
}
func (pc *PatternConfig) compileMatch() error {
r, err := regexp.Compile(pc.Match)
if err != nil {
return fmt.Errorf("failed to compile match expression: %s", err)
}
pc.MatchCompiled = r
return nil
}
func (pc *PatternConfig) eval(matches []string) (float64, error) {
var err error
val := pc.Value
if val == "now()" {
return float64(time.Now().Unix()), nil
}
if strings.HasPrefix(val, "$") {
val, err = pc.getGroupMatch(val[1:], matches)
if err != nil {
return 0, err
}
}
return strconv.ParseFloat(val, 64)
}
func (pc *PatternConfig) getEvaluatedLabels(matches []string) (map[string]string, error) {
ret := map[string]string{}
for label, val := range pc.Labels {
if !strings.HasPrefix(val, "$") {
ret[label] = val
continue
}
val, err := pc.getGroupMatch(val[1:], matches)
if err != nil {
return nil, err
}
ret[label] = val
}
return ret, nil
}
func (pc *PatternConfig) getGroupMatch(name string, matches []string) (string, error) {
if len(name) < 1 {
return "", fmt.Errorf("empty group name")
}
for idx, existingName := range pc.MatchCompiled.SubexpNames() {
if existingName == name {
if len(matches) < idx+1 {
return "", fmt.Errorf("missing regexp match data at index %d", idx)
}
return matches[idx], nil
}
}
return "", fmt.Errorf("named group not found")
}