tsweb: restore CPU profiling handler

It was accidentally deleted in the earlier 0022c3d2e (#2143) refactor.
Lock it in with a test.

Fixes tailscale/corp#2503

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2021-09-02 08:57:14 -07:00 committed by Brad Fitzpatrick
parent daf54d1253
commit 722942dd46
2 changed files with 25 additions and 0 deletions

View File

@ -47,6 +47,11 @@ func Debugger(mux *http.ServeMux) *DebugHandler {
} }
mux.Handle("/debug/", ret) mux.Handle("/debug/", ret)
// Register this one directly on mux, rather than using
// ret.URL/etc, as we don't need another line of output on the
// index page. The /pprof/ index already covers it.
mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
ret.KVFunc("Uptime", func() interface{} { return Uptime() }) ret.KVFunc("Uptime", func() interface{} { return Uptime() })
ret.KV("Version", version.Long) ret.KV("Version", version.Long)
ret.Handle("vars", "Metrics (Go)", expvar.Handler()) ret.Handle("vars", "Metrics (Go)", expvar.Handler())

View File

@ -9,6 +9,7 @@
"io" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"runtime"
"strings" "strings"
"testing" "testing"
) )
@ -25,6 +26,25 @@ func TestDebugger(t *testing.T) {
if dbg2 != dbg1 { if dbg2 != dbg1 {
t.Fatal("Debugger returned different debuggers for the same mux") t.Fatal("Debugger returned different debuggers for the same mux")
} }
t.Run("cpu_pprof", func(t *testing.T) {
if testing.Short() {
t.Skip("skipping second long test")
}
switch runtime.GOOS {
case "linux", "darwin":
default:
t.Skipf("skipping test on %v", runtime.GOOS)
}
req := httptest.NewRequest("GET", "/debug/pprof/profile?seconds=1", nil)
req.RemoteAddr = "100.101.102.103:1234"
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != 200 {
t.Errorf("unexpected %v", res.Status)
}
})
} }
func get(m http.Handler, path, srcIP string) (int, string) { func get(m http.Handler, path, srcIP string) (int, string) {