forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.go
84 lines (65 loc) · 1.69 KB
/
processor.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
package processors
import (
"fmt"
"strings"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
)
type Processors struct {
list []Processor
}
func New(config PluginConfig) (*Processors, error) {
procs := Processors{}
for _, processor := range config {
if len(processor) != 1 {
return nil, fmt.Errorf("each processor needs to have exactly one action, but found %d actions",
len(processor))
}
for processorName, cfg := range processor {
gen, exists := registry.reg[processorName]
if !exists {
return nil, fmt.Errorf("the processor %s doesn't exist", processorName)
}
cfg.PrintDebugf("Configure processor '%v' with:", processorName)
constructor := gen.Plugin()
plugin, err := constructor(cfg)
if err != nil {
return nil, err
}
procs.add(plugin)
}
}
logp.Debug("processors", "Processors: %v", procs)
return &procs, nil
}
func (procs *Processors) add(p Processor) {
procs.list = append(procs.list, p)
}
// Applies a sequence of processing rules and returns the filtered event
func (procs *Processors) Run(event common.MapStr) common.MapStr {
// Check if processors are set, just return event if not
if len(procs.list) == 0 {
return event
}
// clone the event at first, before starting filtering
filtered := event.Clone()
var err error
for _, p := range procs.list {
filtered, err = p.Run(filtered)
if err != nil {
logp.Debug("filter", "fail to apply processor %s: %s", p, err)
}
if filtered == nil {
// drop event
return nil
}
}
return filtered
}
func (procs Processors) String() string {
var s []string
for _, p := range procs.list {
s = append(s, p.String())
}
return strings.Join(s, ", ")
}