/
filter.go
160 lines (131 loc) · 4 KB
/
filter.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"bytes"
fjson "github.com/valyala/fastjson"
"github.com/novln/soba"
)
// DefaultFilterOptions is the default options for the filter handler.
var DefaultFilterOptions = &FilterOptions{
Loggers: FilterAction{
Include: []string{},
Exclude: []string{},
},
Messages: FilterAction{
Include: []string{},
Exclude: []string{},
},
Levels: FilterAction{
Include: []string{},
Exclude: []string{},
},
}
// FilterOptions is the configuration for the filter handler.
type FilterOptions struct {
// Loggers are the filters used for the logger name.
// This filter type will checks that the name start (or don't) with the given prefix.
Loggers FilterAction
// Levels are the filters used for the entry priority.
// This filter type will checks that the level is equals (or not) to the given value.
Levels FilterAction
// Messages are the filters used for the entry message.
// This filter type will checks that the message contains (or not) the given value.
Messages FilterAction
}
// HasFilters returns true is this options has filters.
func (opts FilterOptions) HasFilters() bool {
return opts.Loggers.HasFilters() || opts.Levels.HasFilters() || opts.Messages.HasFilters()
}
// FilterAction is a list of inclusion or exclusion values.
type FilterAction struct {
// Include is a list of values used to retain a json line.
Include []string
// Exclude is a list of values used to discard a json line.
Exclude []string
}
// HasFilters returns true is this action has a inclusion or exclusion list.
func (action FilterAction) HasFilters() bool {
return len(action.Include) > 0 || len(action.Exclude) > 0
}
// Filter inspect the input json to establish if it's required using a list of custom rules.
// This function return the json if it's desired, nil otherwise.
func Filter(json []byte, opts *FilterOptions) []byte {
return NewFilterHandler(opts).Filter(json)
}
// FilterHandler will inspects the input json to establish if it's required using a list of custom rules.
// It will discard the other ones.
type FilterHandler struct {
parser *fjson.Parser
filters FilterOptions
}
// NewFilterHandler creates a new filter instance.
func NewFilterHandler(opts *FilterOptions) *FilterHandler {
if opts == nil {
opts = DefaultFilterOptions
}
handler := &FilterHandler{
parser: &fjson.Parser{},
filters: *opts,
}
return handler
}
// Filter inspect the input json to establish if it's required using a list of custom rules.
// This function return the json if it's desired, nil otherwise.
func (handler *FilterHandler) Filter(json []byte) []byte {
err := fjson.ValidateBytes(json)
if err != nil {
return nil
}
if !handler.filters.HasFilters() {
return json
}
v, err := handler.parser.ParseBytes(json)
if err != nil {
return json
}
level := v.GetStringBytes(soba.LevelKey)
if filter(bytes.Equal, handler.filters.Levels, level) {
return nil
}
logger := v.GetStringBytes(soba.LoggerKey)
if filter(bytes.HasPrefix, handler.filters.Loggers, logger) {
return nil
}
message := v.GetStringBytes(soba.MessageKey)
if filter(bytes.Contains, handler.filters.Messages, message) {
return nil
}
return json
}
func filter(operation func([]byte, []byte) bool, filters FilterAction, value []byte) bool {
// If there is no filter, no need to excute the exclusion/inclusion.
if !filters.HasFilters() {
return false
}
// By default, accept everything.
filter := false
// If there is no exclusion filter, only do inclusion.
if len(filters.Exclude) == 0 {
filter = true
}
// If there is no inclusion filter, only do exclusion.
if len(filters.Include) == 0 {
filter = false
}
// Finally, start with the exclusion pattern to finish by the inclusion pattern.
// This allows to have global exclusion and specific inclusion.
for _, exclude := range filters.Exclude {
v := operation(value, []byte(exclude))
if v {
filter = true
break
}
}
for _, include := range filters.Include {
v := operation(value, []byte(include))
if v {
filter = false
break
}
}
return filter
}