-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathfiltermutate.go
119 lines (107 loc) · 3.38 KB
/
filtermutate.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
package filtermutate
import (
"context"
"errors"
"strings"
"github.com/tsaikd/gogstash/config"
"github.com/tsaikd/gogstash/config/goglog"
"github.com/tsaikd/gogstash/config/logevent"
)
const (
// ModuleName is the name used in config file
ModuleName = "mutate"
// ErrorTag tag added to event when process module failed
ErrorTag = "gogstash_filter_mutate_error"
)
// errors
var (
ErrNotConfigured = errors.New("filter mutate not configured")
)
// FilterConfig holds the configuration json fields and internal objects
type FilterConfig struct {
config.FilterConfig
Uppercase string `yaml:"uppercase"`
Lowercase string `yaml:"lowercase"`
Split [2]string `yaml:"split"`
Replace [3]string `yaml:"replace"`
Merge [2]string `yaml:"merge"` // merge string value into existing string slice field
Rename [2]string `yaml:"rename"` // rename field name into new field name
}
// 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()
err := config.ReflectConfig(raw, &conf)
if err != nil {
return nil, err
}
if conf.Uppercase == "" && conf.Lowercase == "" && conf.Split[0] == "" && conf.Replace[0] == "" && conf.Merge[0] == "" && conf.Rename[0] == "" && !conf.IsConfigured() {
return nil, ErrNotConfigured
}
return &conf, nil
}
// Event the main filter event
func (f *FilterConfig) Event(ctx context.Context, event logevent.LogEvent) (logevent.LogEvent, bool) {
if f.Uppercase != "" {
event.SetValue(f.Uppercase, strings.ToUpper(event.GetString(f.Uppercase)))
}
if f.Lowercase != "" {
event.SetValue(f.Lowercase, strings.ToLower(event.GetString(f.Lowercase)))
}
if f.Split[0] != "" {
event.SetValue(f.Split[0], strings.Split(event.GetString(f.Split[0]), f.Split[1]))
}
if f.Replace[0] != "" {
event.SetValue(f.Replace[0], strings.ReplaceAll(event.GetString(f.Replace[0]), f.Replace[1], event.Format(f.Replace[2])))
}
if f.Merge[0] != "" {
event = mergeField(event, f.Merge[0], f.Merge[1])
}
if f.Rename[0] != "" {
value := event.Get(f.Rename[0])
if value != nil {
event.SetValue(f.Rename[1], value)
}
event.Remove(f.Rename[0])
}
// always return true here for configured filter
return event, true
}
func mergeField(event logevent.LogEvent, destinationName, source string) logevent.LogEvent {
destinationValue := event.Get(destinationName)
value := event.Format(source)
if destinationValue == nil {
destinationValue = []string{value}
event.SetValue(destinationName, destinationValue)
return event
}
switch currentDestination := destinationValue.(type) {
case string:
var newDestination []string
if currentDestination != "" {
newDestination = append(newDestination, currentDestination)
}
newDestination = append(newDestination, value)
event.SetValue(destinationName, newDestination)
case []string:
currentDestination = append(currentDestination, value)
event.SetValue(destinationName, currentDestination)
default:
goglog.Logger.Warnf("mutate: destination field %s is not string nor []string", destinationName)
event.AddTag(ErrorTag)
}
return event
}