-
Notifications
You must be signed in to change notification settings - Fork 1.4k
✨ util: Warning handler that discards messages that match a regular expression #11179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
k8s-ci-robot
merged 4 commits into
kubernetes-sigs:main
from
dlipovetsky:api-warnings-handler
Sep 24, 2024
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a27eb82
✨ util: Warning handler that discards messages that match a regular e…
dlipovetsky 8aea874
fixup! ✨ util: Warning handler that discards messages that match a re…
dlipovetsky e1fd486
fixup! ✨ util: Warning handler that discards messages that match a re…
dlipovetsky ba608cb
fixup! ✨ util: Warning handler that discards messages that match a re…
dlipovetsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// Package apiwarnings defines warning handlers used with API clients. | ||
package apiwarnings |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package apiwarnings | ||
|
||
import ( | ||
"regexp" | ||
"sync" | ||
|
||
"github.com/go-logr/logr" | ||
) | ||
|
||
// DiscardMatchingHandlerOptions configures the handler created with | ||
// NewDiscardMatchingHandler. | ||
type DiscardMatchingHandlerOptions struct { | ||
// Deduplicate indicates a given warning message should only be written once. | ||
// Setting this to true in a long-running process handling many warnings can | ||
// result in increased memory use. | ||
Deduplicate bool | ||
|
||
// Expressions is a slice of regular expressions used to discard warnings. | ||
// If the warning message matches any expression, it is not logged. | ||
Expressions []regexp.Regexp | ||
} | ||
|
||
// NewDiscardMatchingHandler initializes and returns a new DiscardMatchingHandler. | ||
func NewDiscardMatchingHandler(l logr.Logger, opts DiscardMatchingHandlerOptions) *DiscardMatchingHandler { | ||
h := &DiscardMatchingHandler{logger: l, opts: opts} | ||
if opts.Deduplicate { | ||
h.logged = map[string]struct{}{} | ||
} | ||
return h | ||
} | ||
|
||
// DiscardMatchingHandler is a handler that discards API server warnings | ||
// whose message matches any user-defined regular expressions. | ||
type DiscardMatchingHandler struct { | ||
// logger is used to log responses with the warning header | ||
logger logr.Logger | ||
// opts contain options controlling warning output | ||
opts DiscardMatchingHandlerOptions | ||
// loggedLock guards logged | ||
loggedLock sync.Mutex | ||
// used to keep track of already logged messages | ||
// and help in de-duplication. | ||
logged map[string]struct{} | ||
} | ||
|
||
// HandleWarningHeader handles logging for responses from API server that are | ||
// warnings with code being 299 and uses a logr.Logger for its logging purposes. | ||
func (h *DiscardMatchingHandler) HandleWarningHeader(code int, _, message string) { | ||
if code != 299 || message == "" { | ||
return | ||
} | ||
|
||
for _, exp := range h.opts.Expressions { | ||
if exp.MatchString(message) { | ||
return | ||
} | ||
} | ||
|
||
if h.opts.Deduplicate { | ||
h.loggedLock.Lock() | ||
defer h.loggedLock.Unlock() | ||
|
||
if _, alreadyLogged := h.logged[message]; alreadyLogged { | ||
return | ||
} | ||
h.logged[message] = struct{}{} | ||
} | ||
h.logger.Info(message) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package apiwarnings | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/go-logr/logr/funcr" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestDiscardMatchingHandler(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
opts DiscardMatchingHandlerOptions | ||
code int | ||
message string | ||
wantLogged bool | ||
}{ | ||
{ | ||
name: "log, if warning does not match any expression", | ||
code: 299, | ||
message: "non-matching warning", | ||
opts: DiscardMatchingHandlerOptions{ | ||
Expressions: []regexp.Regexp{}, | ||
}, | ||
wantLogged: true, | ||
}, | ||
{ | ||
name: "do not log, if warning matches at least one expression", | ||
code: 299, | ||
message: "matching warning", | ||
opts: DiscardMatchingHandlerOptions{ | ||
Expressions: []regexp.Regexp{ | ||
*regexp.MustCompile("^matching.*"), | ||
}, | ||
}, | ||
wantLogged: false, | ||
}, | ||
{ | ||
name: "do not log, if code is not 299", | ||
code: 0, | ||
message: "", | ||
opts: DiscardMatchingHandlerOptions{ | ||
Expressions: []regexp.Regexp{}, | ||
}, | ||
wantLogged: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := NewWithT(t) | ||
logged := false | ||
h := NewDiscardMatchingHandler( | ||
funcr.New(func(_, _ string) { | ||
logged = true | ||
}, | ||
funcr.Options{}, | ||
), | ||
tt.opts, | ||
) | ||
h.HandleWarningHeader(tt.code, "", tt.message) | ||
g.Expect(logged).To(Equal(tt.wantLogged)) | ||
}) | ||
} | ||
} | ||
|
||
func TestDiscardMatchingHandler_uninitialized(t *testing.T) { | ||
sbueringer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
g := NewWithT(t) | ||
h := DiscardMatchingHandler{} | ||
g.Expect(func() { | ||
h.HandleWarningHeader(0, "", "") | ||
}).ToNot(Panic()) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.