forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drop_event.go
73 lines (55 loc) · 1.35 KB
/
drop_event.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
package rules
import (
"fmt"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/filter"
)
type DropEvent struct {
Cond *filter.Condition
}
type DropEventConfig struct {
filter.ConditionConfig `config:",inline"`
}
func init() {
if err := filter.RegisterPlugin("drop_event", newDropEvent); err != nil {
panic(err)
}
}
func newDropEvent(c common.Config) (filter.FilterRule, error) {
f := DropEvent{}
if err := f.CheckConfig(c); err != nil {
return nil, err
}
config := DropEventConfig{}
err := c.Unpack(&config)
if err != nil {
return nil, fmt.Errorf("fail to unpack the drop_event configuration: %s", err)
}
cond, err := filter.NewCondition(config.ConditionConfig)
if err != nil {
return nil, err
}
f.Cond = cond
return &f, nil
}
func (f *DropEvent) CheckConfig(c common.Config) error {
for _, field := range c.GetFields() {
if !filter.AvailableCondition(field) {
return fmt.Errorf("unexpected %s option in the drop_event configuration", field)
}
}
return nil
}
func (f *DropEvent) Filter(event common.MapStr) (common.MapStr, error) {
if f.Cond != nil && !f.Cond.Check(event) {
return event, nil
}
// return event=nil to delete the entire event
return nil, nil
}
func (f DropEvent) String() string {
if f.Cond != nil {
return "drop_event, condition=" + f.Cond.String()
}
return "drop_event"
}