Merge pull request #3132 from tomwilkie/fix-debug-handlers

Get pprof handlers working again
This commit is contained in:
Tom Wilkie 2017-09-18 14:24:08 +01:00 committed by GitHub
commit fae3bd17b9
2 changed files with 41 additions and 0 deletions

View file

@ -246,6 +246,11 @@ func serveDebug(w http.ResponseWriter, req *http.Request) {
ctx := req.Context() ctx := req.Context()
subpath := route.Param(ctx, "subpath") subpath := route.Param(ctx, "subpath")
if subpath == "/pprof" {
http.Redirect(w, req, req.URL.Path+"/", http.StatusMovedPermanently)
return
}
if !strings.HasPrefix(subpath, "/pprof/") { if !strings.HasPrefix(subpath, "/pprof/") {
http.NotFound(w, req) http.NotFound(w, req)
return return
@ -262,6 +267,7 @@ func serveDebug(w http.ResponseWriter, req *http.Request) {
case "trace": case "trace":
pprof.Trace(w, req) pprof.Trace(w, req)
default: default:
req.URL.Path = "/debug/pprof/" + subpath
pprof.Index(w, req) pprof.Index(w, req)
} }
} }

View file

@ -15,6 +15,7 @@ package web
import ( import (
"net/http" "net/http"
"net/http/httptest"
"net/url" "net/url"
"testing" "testing"
"time" "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())
}
}
}