-
Notifications
You must be signed in to change notification settings - Fork 113
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
logging: add SetFilteredKlogLogger to show client-go throttling logs #673
Merged
Merged
Changes from all commits
Commits
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 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
This file contains 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,61 @@ | ||
// Copyright 2024 Upbound Inc. | ||
// All rights reserved | ||
|
||
package logging | ||
|
||
import ( | ||
"flag" | ||
"os" | ||
"strings" | ||
|
||
"github.com/go-logr/logr" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
// SetFilteredKlogLogger sets log as the logger backend of klog, but filtering | ||
// aggressively to avoid noise. | ||
func SetFilteredKlogLogger(log logr.Logger) { | ||
// initialize klog at verbosity level 3, dropping everything higher. | ||
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError) | ||
klog.InitFlags(fs) | ||
fs.Parse([]string{"--v=3"}) //nolint:errcheck // we couldn't do anything here anyway | ||
|
||
klogr := logr.New(&requestThrottlingFilter{log.GetSink()}) | ||
klog.SetLogger(klogr) | ||
} | ||
|
||
// requestThrottlingFilter drops everything that is not a client-go throttling | ||
// message, compare: | ||
// https://github.com/kubernetes/client-go/blob/8c4efe8d079e405329f314fb789a41ac6af101dc/rest/request.go#L621 | ||
type requestThrottlingFilter struct { | ||
logr.LogSink | ||
} | ||
|
||
func (l *requestThrottlingFilter) Info(level int, msg string, keysAndValues ...interface{}) { | ||
if !strings.Contains(msg, "Waited for ") || !strings.Contains(msg, " request: ") { | ||
return | ||
} | ||
|
||
l.LogSink.Info(l.klogToLogrLevel(level), msg, keysAndValues...) | ||
} | ||
|
||
func (l *requestThrottlingFilter) Enabled(level int) bool { | ||
return l.LogSink.Enabled(l.klogToLogrLevel(level)) | ||
} | ||
|
||
func (l *requestThrottlingFilter) klogToLogrLevel(klogLvl int) int { | ||
// we want a default klog level of 3 for info, 4 for debug, corresponding to | ||
// logr levels of 0 and 1. | ||
if klogLvl >= 3 { | ||
return klogLvl - 3 | ||
} | ||
return 0 | ||
} | ||
|
||
func (l *requestThrottlingFilter) WithCallDepth(depth int) logr.LogSink { | ||
if delegate, ok := l.LogSink.(logr.CallDepthLogSink); ok { | ||
return &requestThrottlingFilter{LogSink: delegate.WithCallDepth(depth)} | ||
} | ||
|
||
return l | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this should have something WRT "request throttling" in the name, to make it obvious why it's filtering. Unless you think we might include more in future?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was thinking that maybe we will add more some day, i.e. this is more like the standard klog setup for crossplane components.