-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathfilteraddfield.go
56 lines (48 loc) · 1.28 KB
/
filteraddfield.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
package filteraddfield
import (
"context"
"github.com/tsaikd/gogstash/config"
"github.com/tsaikd/gogstash/config/logevent"
)
// ModuleName is the name used in config file
const ModuleName = "add_field"
// FilterConfig holds the configuration json fields and internal objects
type FilterConfig struct {
config.FilterConfig
Key string `json:"key" yaml:"key"`
Value string `json:"value" yaml:"value"`
Overwrite bool `json:"overwrite" yaml:"overwrite"`
}
// DefaultFilterConfig returns an FilterConfig struct with default values
func DefaultFilterConfig() FilterConfig {
return FilterConfig{
FilterConfig: config.FilterConfig{
CommonConfig: config.CommonConfig{
Type: ModuleName,
},
},
}
}
// InitHandler initialize the filter plugin
func InitHandler(
ctx context.Context,
raw config.ConfigRaw,
control config.Control,
) (config.TypeFilterConfig, error) {
conf := DefaultFilterConfig()
if err := config.ReflectConfig(raw, &conf); err != nil {
return nil, err
}
return &conf, nil
}
// Event the main filter event
func (f *FilterConfig) Event(
ctx context.Context,
event logevent.LogEvent,
) (logevent.LogEvent, bool) {
if _, ok := event.Extra[f.Key]; ok && !f.Overwrite {
return event, false
}
event.SetValue(f.Key, event.Format(f.Value))
return event, true
}