From 48c25fa36f25953bb978a697ebde33baf79d4519 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Tue, 15 Jun 2021 21:48:47 -0700 Subject: [PATCH] tsweb: fold StdHandlerOpts and StdHandler200s with StdHandler. Signed-off-by: David Anderson --- cmd/microproxy/microproxy.go | 5 ++++- tsweb/tsweb.go | 23 +++++------------------ tsweb/tsweb_test.go | 8 +++----- 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/cmd/microproxy/microproxy.go b/cmd/microproxy/microproxy.go index ecfb42046..76ee41b9c 100644 --- a/cmd/microproxy/microproxy.go +++ b/cmd/microproxy/microproxy.go @@ -57,7 +57,10 @@ func main() { mux := tsweb.NewMux(http.HandlerFunc(debugHandler)) mux.Handle("/metrics", tsweb.Protected(proxy)) - mux.Handle("/varz", tsweb.Protected(tsweb.StdHandler(&goVarsHandler{*goVarsURL}, log.Printf))) + mux.Handle("/varz", tsweb.Protected(tsweb.StdHandler(&goVarsHandler{*goVarsURL}, tsweb.HandlerOptions{ + Quiet200s: true, + Logf: log.Printf, + }))) ch := &certHolder{ hostname: *hostname, diff --git a/tsweb/tsweb.go b/tsweb/tsweb.go index 80bc8e0c5..8c8b18e7f 100644 --- a/tsweb/tsweb.go +++ b/tsweb/tsweb.go @@ -186,13 +186,6 @@ type HandlerOptions struct { StatusCodeCounters *expvar.Map } -// StdHandler converts a ReturnHandler into a standard http.Handler. -// Handled requests are logged using logf, as are any errors. Errors -// are handled as specified by the Handler interface. -func StdHandler(h ReturnHandler, logf logger.Logf) http.Handler { - return StdHandlerOpts(h, HandlerOptions{Logf: logf, Now: time.Now}) -} - // ReturnHandlerFunc is an adapter to allow the use of ordinary // functions as ReturnHandlers. If f is a function with the // appropriate signature, ReturnHandlerFunc(f) is a ReturnHandler that @@ -204,22 +197,16 @@ func (f ReturnHandlerFunc) ServeHTTPReturn(w http.ResponseWriter, r *http.Reques return f(w, r) } -// StdHandlerNo200s is like StdHandler, but successfully handled HTTP -// requests don't write an access log entry to logf. -// -// TODO(josharian): eliminate this and StdHandler in favor of StdHandlerOpts, -// rename StdHandlerOpts to StdHandler. Will be a breaking API change. -func StdHandlerNo200s(h ReturnHandler, logf logger.Logf) http.Handler { - return StdHandlerOpts(h, HandlerOptions{Logf: logf, Now: time.Now, Quiet200s: true}) -} - -// StdHandlerOpts converts a ReturnHandler into a standard http.Handler. +// StdHandler converts a ReturnHandler into a standard http.Handler. // Handled requests are logged using opts.Logf, as are any errors. // Errors are handled as specified by the Handler interface. -func StdHandlerOpts(h ReturnHandler, opts HandlerOptions) http.Handler { +func StdHandler(h ReturnHandler, opts HandlerOptions) http.Handler { if opts.Now == nil { opts.Now = time.Now } + if opts.Logf == nil { + opts.Logf = logger.Discard + } return retHandler{h, opts} } diff --git a/tsweb/tsweb_test.go b/tsweb/tsweb_test.go index 4d4651e5c..5e0dfe9c2 100644 --- a/tsweb/tsweb_test.go +++ b/tsweb/tsweb_test.go @@ -248,7 +248,7 @@ func TestStdHandler(t *testing.T) { clock.Reset() rec := noopHijacker{httptest.NewRecorder(), false} - h := StdHandlerOpts(test.rh, HandlerOptions{Logf: logf, Now: clock.Now}) + h := StdHandler(test.rh, HandlerOptions{Logf: logf, Now: clock.Now}) h.ServeHTTP(&rec, test.r) res := rec.Result() if res.StatusCode != test.wantCode { @@ -277,8 +277,7 @@ func BenchmarkLogNot200(b *testing.B) { // Implicit 200 OK. return nil }) - discardLogger := func(string, ...interface{}) {} - h := StdHandlerNo200s(rh, discardLogger) + h := StdHandler(rh, HandlerOptions{Quiet200s: true}) req := httptest.NewRequest("GET", "/", nil) rw := new(httptest.ResponseRecorder) for i := 0; i < b.N; i++ { @@ -293,8 +292,7 @@ func BenchmarkLog(b *testing.B) { // Implicit 200 OK. return nil }) - discardLogger := func(string, ...interface{}) {} - h := StdHandler(rh, discardLogger) + h := StdHandler(rh, HandlerOptions{}) req := httptest.NewRequest("GET", "/", nil) rw := new(httptest.ResponseRecorder) for i := 0; i < b.N; i++ {