Skip to content
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

Less verbose logging #617

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 6 additions & 33 deletions internal/logging/klog.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,28 @@ import (
"flag"
"fmt"
"os"
"strings"

"github.com/go-logr/logr"
"k8s.io/klog/v2"
)

type KlogFilter func(msg string, keysAndValues ...interface{}) bool

// SetFilteredKlogLogger sets log as the logger backend of klog, with debugLevel+3 as the
// klog verbosity level. If debugLevel is 0, only request throttling messages are
// logged. Further filters can be added to the logger by passing them as arguments.
// Those filters also only apply for debugLevel 0.
func SetFilteredKlogLogger(debugLevel int, log logr.Logger, preds ...KlogFilter) {
// SetKlogLogger sets log as the logger backend of klog, with debugLevel+3 as the
// klog verbosity level.
func SetKlogLogger(debugLevel int, log logr.Logger) {
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
klog.InitFlags(fs)
_ = fs.Parse([]string{fmt.Sprintf("--v=%d", debugLevel+3)}) //nolint:errcheck // we couldn't do anything here anyway

preds = append(preds, requestThrottlingFilter)
if debugLevel == 0 {
preds = nil
}

klogr := logr.New(&klogFilter{LogSink: log.GetSink(), preds: preds})
klogr := logr.New(&klogFilter{LogSink: log.GetSink()})
klog.SetLogger(klogr)
}

type klogFilter struct {
logr.LogSink
preds []KlogFilter
}

func (l *klogFilter) Info(level int, msg string, keysAndValues ...interface{}) {
if len(l.preds) == 0 {
l.LogSink.Info(klogToLogrLevel(level), msg, keysAndValues...)
return
}
for _, pred := range l.preds {
if pred(msg, keysAndValues...) {
l.LogSink.Info(klogToLogrLevel(level), msg, keysAndValues...)
return
}
}
l.LogSink.Info(klogToLogrLevel(level), msg, keysAndValues...)
}

func (l *klogFilter) Enabled(_ int) bool {
Expand All @@ -64,15 +44,8 @@ func klogToLogrLevel(klogLvl int) int {

func (l *klogFilter) WithCallDepth(depth int) logr.LogSink {
if delegate, ok := l.LogSink.(logr.CallDepthLogSink); ok {
return &klogFilter{LogSink: delegate.WithCallDepth(depth), preds: l.preds}
return &klogFilter{LogSink: delegate.WithCallDepth(depth)}
}

return l
}

// requestThrottlingFilter drops everything that is not a client-go throttling
// message, compare:
// https://github.com/kubernetes/client-go/blob/8c4efe8d079e405329f314fb789a41ac6af101dc/rest/request.go#L621
func requestThrottlingFilter(msg string, _ ...interface{}) bool {
return strings.Contains(msg, "Waited for ") && strings.Contains(msg, " request: ")
}
4 changes: 3 additions & 1 deletion internal/upbound/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ func NewFromFlags(f Flags, opts ...Option) (*Context, error) { //nolint:gocyclo

// SetupLogging sets up the logger in controller-runtime and kube's klog
func (c *Context) SetupLogging() {
logging.SetFilteredKlogLogger(c.DebugLevel, c.zl)
if c.DebugLevel > 1 {
logging.SetKlogLogger(c.DebugLevel, c.zl)
}
ctrl.SetLogger(c.zl)
}

Expand Down
Loading