From e9396f2d6fc9add3917eb0eaf3b656632db43833 Mon Sep 17 00:00:00 2001 From: Serge Zaitsev Date: Tue, 25 Sep 2018 13:52:43 +0200 Subject: [PATCH] add debug http endpoint for expvar and pprof api --- cmd/gomodproxy/main.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/cmd/gomodproxy/main.go b/cmd/gomodproxy/main.go index 0d882bb..7a15174 100644 --- a/cmd/gomodproxy/main.go +++ b/cmd/gomodproxy/main.go @@ -15,6 +15,9 @@ import ( "time" "github.com/sixt/gomodproxy/pkg/api" + + _ "expvar" + _ "net/http/pprof" ) func prettyLog(v ...interface{}) { @@ -53,6 +56,7 @@ func main() { addr := flag.String("addr", ":0", "http server address") verbose := flag.Bool("v", false, "verbose logging") + debug := flag.Bool("debug", false, "enable debug HTTP API (pprof/expvar)") json := flag.Bool("json", false, "json structured logging") dir := flag.String("dir", filepath.Join(os.Getenv("HOME"), ".gomodproxy"), "cache directory") memLimit := flag.Int64("mem", 256, "in-memory cache size in MB") @@ -69,7 +73,7 @@ func main() { fmt.Println("Listening on", ln.Addr()) options := []api.Option{} - var logger func(...interface{}) + logger := func(...interface{}) {} if *verbose || *json { if *json { logger = jsonLog @@ -95,7 +99,17 @@ func main() { sigc := make(chan os.Signal, 1) signal.Notify(sigc, os.Interrupt) - srv := &http.Server{Handler: api.New(options...)} + mux := http.NewServeMux() + mux.Handle("/", api.New(options...)) + if *debug { + mux.Handle("/debug/vars", http.DefaultServeMux) + mux.Handle("/debug/pprof/heap", http.DefaultServeMux) + mux.Handle("/debug/pprof/profile", http.DefaultServeMux) + mux.Handle("/debug/pprof/block", http.DefaultServeMux) + mux.Handle("/debug/pprof/trace", http.DefaultServeMux) + } + + srv := &http.Server{Handler: mux} go func() { if err := srv.Serve(ln); err != nil { log.Fatal(err)