/
filter.go
288 lines (264 loc) · 7.49 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package log
import (
"bytes"
"encoding/json"
"expvar"
"github.com/signalfx/golib/errors"
"net/http"
"regexp"
"strings"
"sync"
"sync/atomic"
"text/template"
)
// A Filter controls if logging should happen.
type Filter interface {
WouldLog(keyvals ...interface{}) bool
Disableable
}
// MultiFilter logs to the first filter that passes.
type MultiFilter struct {
Filters []Filter
PassTo Logger
}
// Disabled returns true if all inner filters are disabled
func (f *MultiFilter) Disabled() bool {
if len(f.Filters) == 0 {
return true
}
for _, filter := range f.Filters {
if !filter.Disabled() {
return false
}
}
return true
}
// Log will log out to the first filter
func (f *MultiFilter) Log(keyvals ...interface{}) {
if f.Disabled() {
return
}
for _, filter := range f.Filters {
if filter.WouldLog(keyvals...) {
f.PassTo.Log(keyvals...)
return
}
}
}
// RegexFilter controls logging to only happen to logs that have a dimension that matches all the regex
type RegexFilter struct {
MissingValueKey Key
Log Logger
ErrCallback ErrorHandler
filterChangeIndex int64
currentFilters map[string]*regexp.Regexp
mu sync.RWMutex
}
// Disabled returns true if there are no current filters enabled.
func (f *RegexFilter) Disabled() bool {
f.mu.RLock()
if len(f.currentFilters) == 0 {
f.mu.RUnlock()
return true
}
f.mu.RUnlock()
return false
}
// WouldLog returns true if the regex matches keyvals dimensions
func (f *RegexFilter) WouldLog(keyvals ...interface{}) bool {
if f.Disabled() {
return false
}
m := mapFromKeyvals(f.MissingValueKey, keyvals...)
shouldPass := false
f.mu.RLock()
if matches(f.currentFilters, m, f.ErrCallback) {
shouldPass = true
}
f.mu.RUnlock()
if shouldPass {
return true
}
return false
}
func (f *RegexFilter) varStats() interface{} {
filters := f.GetFilters()
ret := struct {
Stats Stats
Filters map[string]string
}{
Filters: make(map[string]string, len(filters)),
}
for k, v := range filters {
ret.Filters[k] = v.String()
}
ret.Stats = f.Stats()
return ret
}
// Var is an expvar that exports various internal stats and the regex filters
func (f *RegexFilter) Var() expvar.Var {
return expvar.Func(f.varStats)
}
// Stats describes the filter
type Stats struct {
ChangeIndex int64
FilterLen int
}
// Stats returns a Stats object that describes the filter
func (f *RegexFilter) Stats() Stats {
return Stats{
ChangeIndex: atomic.LoadInt64(&f.filterChangeIndex),
FilterLen: len(f.GetFilters()),
}
}
// GetFilters returns the currently used regex filters. Do not modify this map.
func (f *RegexFilter) GetFilters() map[string]*regexp.Regexp {
f.mu.RLock()
ret := f.currentFilters
f.mu.RUnlock()
return ret
}
// FilterCount is a log key that is the count of current filters
var FilterCount = Key("filter_count")
// SetFilters changes the internal regex map
func (f *RegexFilter) SetFilters(filters map[string]*regexp.Regexp) {
f.Log.Log(FilterCount, len(filters), "Changing filters")
f.mu.Lock()
f.currentFilters = filters
atomic.AddInt64(&f.filterChangeIndex, 1)
f.mu.Unlock()
}
func matches(filters map[string]*regexp.Regexp, m map[string]interface{}, onErr ErrorHandler) bool {
if len(filters) == 0 {
return false
}
buf := &bytes.Buffer{}
for k, rgx := range filters {
currentVal, exists := m[k]
if !exists {
return false
}
buf.Reset()
if err := json.NewEncoder(buf).Encode(currentVal); err != nil {
onErr.ErrorLogger(err).Log("Unable to encode a JSON value")
return false
}
if !rgx.MatchString(strings.TrimSpace(buf.String())) {
return false
}
}
return true
}
// FilterChangeHandler allows changing filters via HTTP calls
type FilterChangeHandler struct {
Filter *RegexFilter
Log Logger
}
var _ http.Handler = &FilterChangeHandler{}
// ServeHTTP will process the POST/GET request and change the current filters
func (f *FilterChangeHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
var postErr error
if req.Method == "POST" {
postErr = f.servePost(rw, req)
}
if req.Method == "GET" || req.Method == "POST" {
f.serveGet(rw, req, postErr)
return
}
http.NotFound(rw, req)
}
var getTemplate = template.Must(template.New("get request").Parse(
`<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Current Filters</title>
<style type="text/css">
textarea
{
font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace;
}
</style>
</head>
<body>
{{ if .PostErr }}<p>ERROR SETTING POST: {{ .PostErr}} </p>{{end}}
<h1>Existing filters</h1>
<p> There are currently {{ .FilterCount }} filters. </p>
<p> The filters have been changed {{ .ChangeIndex }} times. </p>
<form>
<p>
<h2>Put new filter here</h2>
<textarea name="newregex" rows="20" cols="120">{{ .CurrentRegex }}</textarea>
<h2>New filter duration</h2>
</p>
<p>
<button type="submit" formmethod="post">Change filters</button>
</p>
</form>
<h1>Usage</h1>
<p>Input 1 or more filters into the box above where each filter is one line long.
Filters are some key name, followed by a :, then the rest of the line which becomes
a regex. See examples below.</p>
<h2>Find id matching 123</h2>
<textarea rows="3" cols="120" readonly="readonly">id:123</textarea>
<h2>Find names matching bob and ids matching 123 with anything after</h2>
<textarea rows="3" cols="120" readonly="readonly">name:bob
id:123.*</textarea>
</body>
</html>`))
type getData struct {
CurrentRegex string
PostErr error
FilterCount int
ChangeIndex int64
}
func (f *FilterChangeHandler) servePost(rw http.ResponseWriter, req *http.Request) error {
if err := req.ParseForm(); err != nil {
return errors.Annotate(err, "cannot parse POST form")
}
newRegex, err := FiltersFromString(req.Form.Get("newregex"))
if err != nil {
return err
}
f.Filter.SetFilters(newRegex)
return nil
}
// FiltersFromString is a helper that creates regex style filters from a \n separated string of
// : separated id -> regex pairs.
func FiltersFromString(newRegex string) (map[string]*regexp.Regexp, error) {
if newRegex == "" {
return nil, nil
}
lines := strings.Split(newRegex, "\n")
newFilters := make(map[string]*regexp.Regexp, len(lines))
for _, line := range lines {
lineParts := strings.SplitN(line, ":", 2)
if len(lineParts) != 2 || lineParts[0] == "" || lineParts[1] == "" {
return nil, errors.Errorf("Cannot parse line %s", line)
}
reg, err := regexp.Compile(lineParts[1])
if err != nil {
return nil, errors.Annotatef(err, "Cannot parse regex line %s", line)
}
newFilters[lineParts[0]] = reg
}
return newFilters, nil
}
func (f *FilterChangeHandler) serveGet(rw http.ResponseWriter, req *http.Request, postErr error) {
allFilters := f.Filter.GetFilters()
currentData := make([]string, 0, len(allFilters))
for key, filter := range allFilters {
currentData = append(currentData, key+":"+filter.String())
}
templateData := getData{
CurrentRegex: strings.Join(currentData, "\n"),
PostErr: postErr,
FilterCount: len(allFilters),
ChangeIndex: atomic.LoadInt64(&f.Filter.filterChangeIndex),
}
rw.Header().Add("Content-Type", "application/xhtml+xml")
if err := getTemplate.Execute(rw, templateData); err != nil {
rw.WriteHeader(http.StatusInternalServerError)
f.Log.Log(Err, err, "cannot execute template")
}
}