forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.go
369 lines (303 loc) · 6.99 KB
/
select.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package outil
import (
"fmt"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/fmtstr"
"github.com/elastic/beats/libbeat/processors"
)
type Selector struct {
sel SelectorExpr
}
type Settings struct {
// single selector key and default option keyword
Key string
// multi-selector key in config
MultiKey string
// if enabled a selector `key` in config will be generated, if `key` is present
EnableSingleOnly bool
// Fail building selector if `key` and `multiKey` are missing
FailEmpty bool
}
type SelectorExpr interface {
sel(evt common.MapStr) (string, error)
}
type emptySelector struct{}
type listSelector struct {
selectors []SelectorExpr
}
type condSelector struct {
s SelectorExpr
cond *processors.Condition
}
type constSelector struct {
s string
}
type fmtSelector struct {
f fmtstr.EventFormatString
otherwise string
}
type mapSelector struct {
from SelectorExpr
otherwise string
to map[string]string
}
var nilSelector SelectorExpr = &emptySelector{}
func MakeSelector(es ...SelectorExpr) Selector {
switch len(es) {
case 0:
return Selector{nilSelector}
case 1:
return Selector{es[0]}
default:
return Selector{ConcatSelectorExpr(es...)}
}
}
// Select runs configured selector against the current event.
// If no matching selector is found, an empty string is returned.
// It's up to the caller to decide if an empty string is an error
// or an expected result.
func (s Selector) Select(evt common.MapStr) (string, error) {
return s.sel.sel(evt)
}
func (s Selector) IsEmpty() bool {
return s.sel == nilSelector || s.sel == nil
}
func (s Selector) IsConst() bool {
if s.sel == nilSelector {
return true
}
_, ok := s.sel.(*constSelector)
return ok
}
func BuildSelectorFromConfig(
cfg *common.Config,
settings Settings,
) (Selector, error) {
var sel []SelectorExpr
key := settings.Key
multiKey := settings.MultiKey
found := false
if cfg.HasField(multiKey) {
found = true
sub, err := cfg.Child(multiKey, -1)
if err != nil {
return Selector{}, err
}
var table []*common.Config
if err := sub.Unpack(&table); err != nil {
return Selector{}, err
}
for _, config := range table {
action, err := buildSingle(config, key)
if err != nil {
return Selector{}, err
}
if action != nilSelector {
sel = append(sel, action)
}
}
}
if settings.EnableSingleOnly && cfg.HasField(key) {
found = true
// expect event-format-string
str, err := cfg.String(key, -1)
if err != nil {
return Selector{}, err
}
fmtstr, err := fmtstr.CompileEvent(str)
if err != nil {
return Selector{}, fmt.Errorf("%v in %v", err, cfg.PathOf(key))
}
if fmtstr.IsConst() {
str, err := fmtstr.Run(common.MapStr{})
if err != nil {
return Selector{}, err
}
if str != "" {
sel = append(sel, ConstSelectorExpr(str))
}
} else {
sel = append(sel, FmtSelectorExpr(fmtstr, ""))
}
}
if settings.FailEmpty && !found {
if settings.EnableSingleOnly {
return Selector{}, fmt.Errorf("missing required '%v' or '%v' in %v",
key, multiKey, cfg.Path())
}
return Selector{}, fmt.Errorf("missing required '%v' in %v",
multiKey, cfg.Path())
}
return MakeSelector(sel...), nil
}
func EmptySelectorExpr() SelectorExpr {
return nilSelector
}
func ConstSelectorExpr(s string) SelectorExpr {
return &constSelector{s}
}
func FmtSelectorExpr(fmt *fmtstr.EventFormatString, fallback string) SelectorExpr {
return &fmtSelector{*fmt, fallback}
}
func ConcatSelectorExpr(s ...SelectorExpr) SelectorExpr {
return &listSelector{s}
}
func ConditionalSelectorExpr(
s SelectorExpr,
cond *processors.Condition,
) SelectorExpr {
return &condSelector{s, cond}
}
func LookupSelectorExpr(
s SelectorExpr,
table map[string]string,
fallback string,
) SelectorExpr {
return &mapSelector{s, fallback, table}
}
func buildSingle(cfg *common.Config, key string) (SelectorExpr, error) {
// TODO: check for unknown fields
// 1. extract required key-word handler
if !cfg.HasField(key) {
return nil, fmt.Errorf("missing %v", cfg.PathOf(key))
}
str, err := cfg.String(key, -1)
if err != nil {
return nil, err
}
evtfmt, err := fmtstr.CompileEvent(str)
if err != nil {
return nil, fmt.Errorf("%v in %v", err, cfg.PathOf(key))
}
// 2. extract optional `default` value
var otherwise string
if cfg.HasField("default") {
tmp, err := cfg.String("default", -1)
if err != nil {
return nil, err
}
otherwise = tmp
}
// 3. extract optional `mapping`
mapping := struct {
Table map[string]string `config:"mappings"`
}{nil}
if cfg.HasField("mappings") {
if err := cfg.Unpack(&mapping); err != nil {
return nil, err
}
}
// 4. extract conditional
var cond *processors.Condition
if cfg.HasField("when") {
sub, err := cfg.Child("when", -1)
if err != nil {
return nil, err
}
condConfig := processors.ConditionConfig{}
if err := sub.Unpack(&condConfig); err != nil {
return nil, err
}
tmp, err := processors.NewCondition(&condConfig)
if err != nil {
return nil, err
}
cond = tmp
}
// 5. build selector from available fields
var sel SelectorExpr
if len(mapping.Table) > 0 {
if evtfmt.IsConst() {
str, err := evtfmt.Run(common.MapStr{})
if err != nil {
return nil, err
}
str = mapping.Table[str]
if str == "" {
str = otherwise
}
if str == "" {
sel = nilSelector
} else {
sel = ConstSelectorExpr(str)
}
} else {
sel = &mapSelector{
from: FmtSelectorExpr(evtfmt, ""),
to: mapping.Table,
otherwise: otherwise,
}
}
} else {
if evtfmt.IsConst() {
str, err := evtfmt.Run(common.MapStr{})
if err != nil {
return nil, err
}
if str == "" {
sel = nilSelector
} else {
sel = ConstSelectorExpr(str)
}
} else {
sel = FmtSelectorExpr(evtfmt, otherwise)
}
}
if cond != nil && sel != nilSelector {
sel = ConditionalSelectorExpr(sel, cond)
}
return sel, nil
}
func (s *emptySelector) sel(evt common.MapStr) (string, error) {
return "", nil
}
func (s *listSelector) sel(evt common.MapStr) (string, error) {
for _, sub := range s.selectors {
n, err := sub.sel(evt)
if err != nil { // TODO: try
return n, err
}
if n != "" {
return n, nil
}
}
return "", nil
}
func (s *condSelector) sel(evt common.MapStr) (string, error) {
if !s.cond.Check(evt) {
return "", nil
}
return s.s.sel(evt)
}
func (s *constSelector) sel(_ common.MapStr) (string, error) {
return s.s, nil
}
func (s *fmtSelector) sel(evt common.MapStr) (string, error) {
n, err := s.f.Run(evt)
if err != nil {
// err will be set if not all keys present in event ->
// return empty selector result and ignore error
return s.otherwise, nil
}
if n == "" {
return s.otherwise, nil
}
return n, nil
}
func (s *mapSelector) sel(evt common.MapStr) (string, error) {
n, err := s.from.sel(evt)
if err != nil {
if s.otherwise == "" {
return "", err
}
return s.otherwise, nil
}
if n == "" {
return s.otherwise, nil
}
n = s.to[n]
if n == "" {
return s.otherwise, nil
}
return n, nil
}