From 9ad8250a786bfe8814e3372da089f70213742386 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sun, 26 Jul 2020 18:54:41 +0200 Subject: [PATCH 1/2] Reduce overhead of debug calls if no log is enabled In case no debug log is configured, then calls to debug.Log only incur the costs to check a single boolean flag making the call really cheap. --- internal/debug/debug.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/internal/debug/debug.go b/internal/debug/debug.go index 543755e25..3df2c2ef4 100644 --- a/internal/debug/debug.go +++ b/internal/debug/debug.go @@ -17,9 +17,10 @@ import ( ) var opts struct { - logger *log.Logger - funcs map[string]bool - files map[string]bool + isEnabled bool + logger *log.Logger + funcs map[string]bool + files map[string]bool } // make sure that all the initialization happens before the init() functions @@ -30,6 +31,12 @@ func initDebug() bool { initDebugLogger() initDebugTags() + if opts.logger == nil && len(opts.funcs) == 0 && len(opts.files) == 0 { + opts.isEnabled = false + return false + } + + opts.isEnabled = true fmt.Fprintf(os.Stderr, "debug enabled\n") return true @@ -173,6 +180,10 @@ func checkFilter(filter map[string]bool, key string) bool { // Log prints a message to the debug log (if debug is enabled). func Log(f string, args ...interface{}) { + if !opts.isEnabled { + return + } + fn, dir, file, line := getPosition() goroutine := goroutineNum() From 07f4e7d10b1562a84beec3180babd43916654770 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Thu, 17 Sep 2020 21:40:17 +0200 Subject: [PATCH 2/2] Only log HTTP requests when a debug log is configured The logged HTTP requests are only visible when a debug log is configured. --- internal/debug/round_tripper_debug.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/debug/round_tripper_debug.go b/internal/debug/round_tripper_debug.go index 7b29267c9..5dfbb64c6 100644 --- a/internal/debug/round_tripper_debug.go +++ b/internal/debug/round_tripper_debug.go @@ -66,7 +66,12 @@ type loggingRoundTripper struct { // RoundTripper returns a new http.RoundTripper which logs all requests (if // debug is enabled). When debug is not enabled, upstream is returned. func RoundTripper(upstream http.RoundTripper) http.RoundTripper { - return loggingRoundTripper{eofDetectRoundTripper{upstream}} + eofRoundTripper := eofDetectRoundTripper{upstream} + if opts.isEnabled { + // only use loggingRoundTripper if the debug log is configured + return loggingRoundTripper{eofRoundTripper} + } + return eofRoundTripper } func (tr loggingRoundTripper) RoundTrip(req *http.Request) (res *http.Response, err error) {