forked from tsaikd/gogstash
-
Notifications
You must be signed in to change notification settings - Fork 1
/
filteraddfield.go
48 lines (40 loc) · 1.13 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
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"`
Value string `json:"value"`
}
// 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) (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 {
if _, ok := event.Extra[f.Key]; ok {
return event
}
event.SetValue(f.Key, event.Format(f.Value))
return event
}