forked from grafana/carbon-relay-ng
/
imperatives.go
286 lines (271 loc) · 7.67 KB
/
imperatives.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
import (
"bytes"
"errors"
"fmt"
"github.com/taylorchu/toki"
"strconv"
"strings"
"time"
//"github.com/davecgh/go-spew/spew"
)
const (
addBlack toki.Token = iota
addRouteSendAllMatch
addRouteSendFirstMatch
addDest
modDest
modRoute
opt
str
word
)
// we should make sure we apply changes atomatically. e.g. when changing dest between address A and pickle=false and B with pickle=true,
// we should never half apply the change if one of them fails.
var tokenDefGlobal = []toki.Def{
{Token: addBlack, Pattern: "addBlack [^ ]+"},
{Token: addRouteSendAllMatch, Pattern: "addRoute sendAllMatch [a-z-_]+"},
{Token: addRouteSendFirstMatch, Pattern: "addRoute sendFirstMatch [a-z-_]+"},
{Token: addDest, Pattern: "addDest [a-z-_]+"},
{Token: modDest, Pattern: "modDest .*"},
{Token: modRoute, Pattern: "modRoute .*"},
{Token: opt, Pattern: "[a-z]+="},
{Token: str, Pattern: "\".*\""},
{Token: word, Pattern: "[^ ]+"},
}
var tokenDefDest = []toki.Def{
{Token: opt, Pattern: "[a-z]+="},
{Token: str, Pattern: "\".*\""},
{Token: word, Pattern: "[^ ]+"},
}
// we should read and apply all destinations at once,
// or at least make sure we apply them to the global datastruct at once,
// otherwise we can destabilize things / send wrong traffic, etc
func readDestinations(specs []string, table *Table) (destinations []*Destination, err error) {
s := toki.NewScanner(tokenDefDest)
for _, spec := range specs {
//fmt.Println("spec" + spec)
var prefix, sub, regex, addr, spoolDir string
var spool, pickle bool
flush := 1000
reconn := 10000
spoolDir = table.spoolDir
s.SetInput(spec)
t := s.Next()
//fmt.Println("thisisit")
if len(t.Value) == 0 {
return destinations, errors.New(fmt.Sprintf("addr not set for endpoint"))
}
addr = string(t.Value)
//fmt.Println(t.Token, word)
if t.Token != word {
//fmt.Println("wtf", t.Token, word)
return destinations, errors.New(fmt.Sprintf("expected destination endpoint spec, not '%s'", t.Value))
}
for {
t := s.Next()
if t.Token == opt {
val := string(t.Value)
//fmt.Println("yes got my opt with val", val)
switch val {
case "prefix=":
val := s.Next()
prefix = string(val.Value)
case "sub=":
val := s.Next()
sub = string(val.Value)
case "regex=":
val := s.Next()
regex = string(val.Value)
case "flush=":
val := s.Next()
i, err := strconv.Atoi(string(val.Value))
if err != nil {
return destinations, err
}
flush = i
case "reconn=":
val := s.Next()
i, err := strconv.Atoi(string(val.Value))
if err != nil {
return destinations, err
}
reconn = i
case "pickle=":
t := s.Next()
val := string(t.Value)
if val == "true" {
pickle = true
} else if val == "false" {
} else {
return destinations, errors.New("unrecognized pickle value '" + val + "'")
}
case "spool=":
t := s.Next()
val := string(t.Value)
if val == "true" {
spool = true
} else if val != "false" {
return destinations, errors.New("unrecognized spool value '" + val + "'")
}
default:
return destinations, errors.New("unrecognized option '" + val + "'")
}
} else {
break
//return destinations, errors.New(fmt.Sprintf("expected endpoint option, not token type %v with value '%s'", t.Token, t.Value))
}
}
periodFlush := time.Duration(flush) * time.Millisecond
periodReConn := time.Duration(reconn) * time.Millisecond
dest, err := NewDestination(prefix, sub, regex, addr, spoolDir, spool, pickle, periodFlush, periodReConn)
if err != nil {
return destinations, err
}
destinations = append(destinations, dest)
}
return destinations, nil
}
// note the two spaces between a route and endpoints
//"addBlack filter-out-all-metrics-matching-this-substring",
//"addRoute sendAllMatch carbon-default 127.0.0.1:2005 spool=false pickle=false",
//"addRoute sendFirstMatch demo sub=foo prefix=foo re=foo 127.0.0.1:12345 spool=true"
//addBlack string-without-spaces
//addRoute <type> <key> <match options> <dests> # match options can't have spaces for now. sorry
//dests:
// <tcp addr> <options>
func applyCommand(table *Table, cmd string) error {
inputs := strings.Split(cmd, " ")
s := toki.NewScanner(tokenDefGlobal)
s.SetInput(inputs[0])
t := s.Next()
if t.Token == addBlack {
split := bytes.Split(t.Value, []byte(" "))
if t = s.Next(); t.Token != toki.EOF {
return errors.New(fmt.Sprintf("extraneous input '%s'", string(t.Value)))
}
patt := string(split[1])
//fmt.Println(patt)
m, err := NewMatcher("", patt, "")
if err != nil {
return err
}
table.AddBlacklist(m)
} else if t.Token == addRouteSendAllMatch {
split := strings.Split(string(t.Value), " ")
key := split[2]
if len(inputs) < 2 {
return errors.New("must get at least 1 destination for route " + key)
}
prefix, sub, regex, err := readRouteOpts(s)
if err != nil {
return err
}
destinations, err := readDestinations(inputs[1:], table)
if err != nil {
return err
}
route, err := NewRoute(sendAllMatch(1), key, prefix, sub, regex)
if err != nil {
return err
}
route.Dests = destinations
table.AddRoute(route)
} else if t.Token == addRouteSendFirstMatch {
split := strings.Split(string(t.Value), " ")
key := split[2]
if len(inputs) < 2 {
return errors.New("must get at least 1 destination for route " + key)
}
prefix, sub, regex, err := readRouteOpts(s)
if err != nil {
return err
}
destinations, err := readDestinations(inputs[1:], table)
if err != nil {
return err
}
route, err := NewRoute(sendFirstMatch(2), key, prefix, sub, regex)
if err != nil {
return err
}
route.Dests = destinations
table.AddRoute(route)
} else if t.Token == addDest {
//split := strings.Split(string(t.Value), " ")
//key := split[2]
fmt.Println("val", t.Value)
fmt.Println("inputs", inputs[0])
} else if t.Token == modDest {
split := strings.Split(string(t.Value), " ")
if len(split) < 4 {
return errors.New("need a key, index and at least one option")
}
key := split[1]
index, err := strconv.Atoi(split[2])
if err != nil {
return err
}
opts := make(map[string]string)
for _, str := range split[3:] {
opt := strings.Split(str, "=")
if len(opt) != 2 {
return errors.New("bad option format at " + str)
}
opts[opt[0]] = opt[1]
}
return table.UpdateDestination(key, index, opts)
} else if t.Token == modRoute {
split := strings.Split(string(t.Value), " ")
if len(split) < 3 {
return errors.New("need a key and at least one option")
}
key := split[1]
opts := make(map[string]string)
for _, str := range split[2:] {
opt := strings.Split(str, "=")
if len(opt) != 2 {
return errors.New("bad option format at " + str)
}
opts[opt[0]] = opt[1]
}
return table.UpdateRoute(key, opts)
} else {
return errors.New("unrecognized command '" + string(t.Value) + "'")
}
if t = s.Next(); t.Token != toki.EOF {
//fmt.Println("h")
return errors.New("extraneous input '" + string(t.Value) + "'")
}
return nil
}
func readRouteOpts(s *toki.Scanner) (prefix, sub, regex string, err error) {
for {
t := s.Next()
//spew.Dump(t.Token)
//spew.Dump(t)
if t.Token == toki.EOF {
break
}
if t.Token == toki.Error {
return "", "", "", errors.New("read the error token instead of one i recognize")
}
if t.Token == opt {
//fmt.Println("yet")
switch string(t.Value) {
case "prefix=":
val := s.Next()
prefix = string(val.Value)
case "sub=":
val := s.Next()
sub = string(val.Value)
case "regex=":
val := s.Next()
regex = string(val.Value)
default:
return "", "", "", errors.New("unrecognized option '" + string(t.Value) + "'")
}
}
}
return
}