From bbc9671d50eed2223a05bb2af204ac2dacbf38e2 Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Fri, 1 Sep 2017 12:12:51 +0100 Subject: [PATCH] Get profile handlers working again after #3054 and #3146. Ensures the pprod endpoints deal with path-prefixes correctly; adds a test so we don't break it again. --- web/web.go | 6 ++++++ web/web_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/web/web.go b/web/web.go index 1c6d7fb95..0f87f6b94 100644 --- a/web/web.go +++ b/web/web.go @@ -246,6 +246,11 @@ func serveDebug(w http.ResponseWriter, req *http.Request) { ctx := req.Context() subpath := route.Param(ctx, "subpath") + if subpath == "/pprof" { + http.Redirect(w, req, req.URL.Path+"/", http.StatusMovedPermanently) + return + } + if !strings.HasPrefix(subpath, "/pprof/") { http.NotFound(w, req) return @@ -262,6 +267,7 @@ func serveDebug(w http.ResponseWriter, req *http.Request) { case "trace": pprof.Trace(w, req) default: + req.URL.Path = "/debug/pprof/" + subpath pprof.Index(w, req) } } diff --git a/web/web_test.go b/web/web_test.go index 9784a17b9..ef54ac04a 100644 --- a/web/web_test.go +++ b/web/web_test.go @@ -15,6 +15,7 @@ package web import ( "net/http" + "net/http/httptest" "net/url" "testing" "time" @@ -146,3 +147,37 @@ func TestReadyAndHealthy(t *testing.T) { } } + +func TestDebugHandler(t *testing.T) { + for _, tc := range []struct { + prefix, url string + code int + }{ + {"/", "/debug/pprof/cmdline", 200}, + {"/foo", "/foo/debug/pprof/cmdline", 200}, + + {"/", "/debug/pprof/goroutine", 200}, + {"/foo", "/foo/debug/pprof/goroutine", 200}, + + {"/", "/debug/pprof/foo", 404}, + {"/foo", "/bar/debug/pprof/goroutine", 404}, + } { + opts := &Options{ + RoutePrefix: tc.prefix, + MetricsPath: "/metrics", + } + handler := New(opts) + handler.Ready() + + w := httptest.NewRecorder() + req, err := http.NewRequest("GET", tc.url, nil) + if err != nil { + t.Fatalf("Unexpected error %s", err) + } + + handler.router.ServeHTTP(w, req) + if w.Code != tc.code { + t.Fatalf("Unexpected status code %d: %s", w.Code, w.Body.String()) + } + } +}