Skip to content

Commit 8aea874

Browse files
committed
fixup! ✨ util: Warning handler that discards messages that match a regular expression
Remove deduplication. As a side-effect, simplify the implementation.
1 parent a27eb82 commit 8aea874

File tree

2 files changed

+38
-70
lines changed

2 files changed

+38
-70
lines changed

util/apiwarnings/handler.go

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,68 +18,34 @@ package apiwarnings
1818

1919
import (
2020
"regexp"
21-
"sync"
2221

2322
"github.com/go-logr/logr"
2423
)
2524

26-
// DiscardMatchingHandlerOptions configures the handler created with
27-
// NewDiscardMatchingHandler.
28-
type DiscardMatchingHandlerOptions struct {
29-
// Deduplicate indicates a given warning message should only be written once.
30-
// Setting this to true in a long-running process handling many warnings can
31-
// result in increased memory use.
32-
Deduplicate bool
25+
// DiscardMatchingHandler is a handler that discards API server warnings
26+
// whose message matches any user-defined regular expressions, but otherwise
27+
// logs warnings to the user-defined logger.
28+
type DiscardMatchingHandler struct {
29+
// Logger is used to log responses with the warning header.
30+
Logger logr.Logger
3331

3432
// Expressions is a slice of regular expressions used to discard warnings.
3533
// If the warning message matches any expression, it is not logged.
3634
Expressions []regexp.Regexp
3735
}
3836

39-
// NewDiscardMatchingHandler initializes and returns a new DiscardMatchingHandler.
40-
func NewDiscardMatchingHandler(l logr.Logger, opts DiscardMatchingHandlerOptions) *DiscardMatchingHandler {
41-
h := &DiscardMatchingHandler{logger: l, opts: opts}
42-
if opts.Deduplicate {
43-
h.logged = map[string]struct{}{}
44-
}
45-
return h
46-
}
47-
48-
// DiscardMatchingHandler is a handler that discards API server warnings
49-
// whose message matches any user-defined regular expressions.
50-
type DiscardMatchingHandler struct {
51-
// logger is used to log responses with the warning header
52-
logger logr.Logger
53-
// opts contain options controlling warning output
54-
opts DiscardMatchingHandlerOptions
55-
// loggedLock guards logged
56-
loggedLock sync.Mutex
57-
// used to keep track of already logged messages
58-
// and help in de-duplication.
59-
logged map[string]struct{}
60-
}
61-
6237
// HandleWarningHeader handles logging for responses from API server that are
6338
// warnings with code being 299 and uses a logr.Logger for its logging purposes.
6439
func (h *DiscardMatchingHandler) HandleWarningHeader(code int, _, message string) {
6540
if code != 299 || message == "" {
6641
return
6742
}
6843

69-
for _, exp := range h.opts.Expressions {
44+
for _, exp := range h.Expressions {
7045
if exp.MatchString(message) {
7146
return
7247
}
7348
}
7449

75-
if h.opts.Deduplicate {
76-
h.loggedLock.Lock()
77-
defer h.loggedLock.Unlock()
78-
79-
if _, alreadyLogged := h.logged[message]; alreadyLogged {
80-
return
81-
}
82-
h.logged[message] = struct{}{}
83-
}
84-
h.logger.Info(message)
50+
h.Logger.Info(message)
8551
}

util/apiwarnings/handler_test.go

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,63 +26,65 @@ import (
2626

2727
func TestDiscardMatchingHandler(t *testing.T) {
2828
tests := []struct {
29-
name string
30-
opts DiscardMatchingHandlerOptions
31-
code int
32-
message string
33-
wantLogged bool
29+
name string
30+
code int
31+
message string
32+
expressions []regexp.Regexp
33+
wantLogged bool
3434
}{
3535
{
36-
name: "log, if warning does not match any expression",
37-
code: 299,
38-
message: "non-matching warning",
39-
opts: DiscardMatchingHandlerOptions{
40-
Expressions: []regexp.Regexp{},
41-
},
36+
name: "log, if no expressions are defined",
37+
code: 299,
38+
message: "non-matching warning",
4239
wantLogged: true,
4340
},
41+
{
42+
name: "log, if warning does not match any expression",
43+
code: 299,
44+
message: "non-matching warning",
45+
expressions: []regexp.Regexp{},
46+
wantLogged: true,
47+
},
4448
{
4549
name: "do not log, if warning matches at least one expression",
4650
code: 299,
4751
message: "matching warning",
48-
opts: DiscardMatchingHandlerOptions{
49-
Expressions: []regexp.Regexp{
50-
*regexp.MustCompile("^matching.*"),
51-
},
52+
expressions: []regexp.Regexp{
53+
*regexp.MustCompile("^matching.*"),
5254
},
5355
wantLogged: false,
5456
},
5557
{
56-
name: "do not log, if code is not 299",
57-
code: 0,
58-
message: "",
59-
opts: DiscardMatchingHandlerOptions{
60-
Expressions: []regexp.Regexp{},
61-
},
62-
wantLogged: false,
58+
name: "do not log, if code is not 299",
59+
code: 0,
60+
message: "",
61+
expressions: []regexp.Regexp{},
62+
wantLogged: false,
6363
},
6464
}
6565
for _, tt := range tests {
6666
t.Run(tt.name, func(t *testing.T) {
6767
g := NewWithT(t)
6868
logged := false
69-
h := NewDiscardMatchingHandler(
70-
funcr.New(func(_, _ string) {
69+
h := DiscardMatchingHandler{
70+
Logger: funcr.New(func(_, _ string) {
7171
logged = true
7272
},
7373
funcr.Options{},
7474
),
75-
tt.opts,
76-
)
75+
Expressions: tt.expressions,
76+
}
7777
h.HandleWarningHeader(tt.code, "", tt.message)
7878
g.Expect(logged).To(Equal(tt.wantLogged))
7979
})
8080
}
8181
}
8282

83-
func TestDiscardMatchingHandler_uninitialized(t *testing.T) {
83+
func TestDiscardMatchingHandler_NilLogger(t *testing.T) {
8484
g := NewWithT(t)
85-
h := DiscardMatchingHandler{}
85+
h := DiscardMatchingHandler{
86+
// Logger is nil
87+
}
8688
g.Expect(func() {
8789
h.HandleWarningHeader(0, "", "")
8890
}).ToNot(Panic())

0 commit comments

Comments
 (0)