From 7d98c793ab321aee796ca9980ca786322caff453 Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Tue, 25 Mar 2025 10:10:21 +0800 Subject: [PATCH] Revert "Automated cherry pick of #22262: Automated cherry pick of #22259: fix(webconsole,monitor): pprof handlers of gorilla mux" --- pkg/appsrv/handlers.go | 25 ------------------------- pkg/monitor/service/handlers.go | 12 ++++++++++-- pkg/webconsole/service/service.go | 21 ++++++++++++++++++++- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/pkg/appsrv/handlers.go b/pkg/appsrv/handlers.go index c79821b602..9dec8a034e 100644 --- a/pkg/appsrv/handlers.go +++ b/pkg/appsrv/handlers.go @@ -20,8 +20,6 @@ import ( "net/http" "net/http/pprof" - "github.com/gorilla/mux" - "yunion.io/x/pkg/util/version" ) @@ -87,26 +85,3 @@ func profSymbol(_ context.Context, w http.ResponseWriter, r *http.Request) { func profTrace(_ context.Context, w http.ResponseWriter, r *http.Request) { pprof.Trace(w, r) } - -func AddMiscHandlersToMuxRouter(app *Application, root *mux.Router, enableProfiling bool) { - adapterF := func(appHandleFunc func(ctx context.Context, w http.ResponseWriter, r *http.Request)) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - appHandleFunc(app.GetContext(), w, r) - } - } - root.HandleFunc("/version", adapterF(VersionHandler)) - root.HandleFunc("/stats", adapterF(StatisticHandler)) - root.HandleFunc("/ping", adapterF(PingHandler)) - root.HandleFunc("/worker_stats", adapterF(WorkerStatsHandler)) - if enableProfiling { - pp := "/debug/pprof" - ppPath := func(sufix string) string { - return fmt.Sprintf("%s/%s", pp, sufix) - } - root.HandleFunc(ppPath(""), pprof.Index) - root.HandleFunc(ppPath("cmdline"), pprof.Cmdline) - root.HandleFunc(ppPath("profile"), pprof.Profile) - root.HandleFunc(ppPath("symbol"), pprof.Symbol) - root.HandleFunc(ppPath("trace"), pprof.Trace) - } -} diff --git a/pkg/monitor/service/handlers.go b/pkg/monitor/service/handlers.go index 1379908dcb..28bb5519a1 100644 --- a/pkg/monitor/service/handlers.go +++ b/pkg/monitor/service/handlers.go @@ -18,6 +18,7 @@ import ( "context" "net" "net/http" + _ "net/http/pprof" "strconv" "github.com/gorilla/mux" @@ -30,7 +31,6 @@ import ( "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman" common_options "yunion.io/x/onecloud/pkg/cloudcommon/options" "yunion.io/x/onecloud/pkg/monitor/models" - "yunion.io/x/onecloud/pkg/monitor/options" ) func InitHandlers(app *appsrv.Application) { @@ -126,5 +126,13 @@ func addMiscHandlers(app *appsrv.Application, root *mux.Router) { } } root.HandleFunc("/subscriptions/write", adapterF(performHandler)) - appsrv.AddMiscHandlersToMuxRouter(app, root, options.Options.EnableAppProfiling) + + // ref: pkg/appsrv/appsrv:addDefaultHandlers + root.HandleFunc("/version", adapterF(appsrv.VersionHandler)) + root.HandleFunc("/stats", adapterF(appsrv.StatisticHandler)) + root.HandleFunc("/ping", adapterF(appsrv.PingHandler)) + root.HandleFunc("/worker_stats", adapterF(appsrv.WorkerStatsHandler)) + + // pprof handler + root.PathPrefix("/debug/pprof/").Handler(http.DefaultServeMux) } diff --git a/pkg/webconsole/service/service.go b/pkg/webconsole/service/service.go index 0023891868..30c1f82ba4 100644 --- a/pkg/webconsole/service/service.go +++ b/pkg/webconsole/service/service.go @@ -15,8 +15,10 @@ package service import ( + "context" "net" "net/http" + _ "net/http/pprof" "net/url" "os" "strconv" @@ -109,7 +111,7 @@ func start() { root.Handle(webconsole.WebsocketProxyPathPrefix, srv) // misc handler - appsrv.AddMiscHandlersToMuxRouter(app, root, o.Options.EnableAppProfiling) + addMiscHandlers(app, root) cron := cronman.InitCronJobManager(true, o.Options.CronJobWorkerCount) @@ -135,3 +137,20 @@ func start() { } } } + +func addMiscHandlers(app *appsrv.Application, root *mux.Router) { + adapterF := func(appHandleFunc func(ctx context.Context, w http.ResponseWriter, r *http.Request)) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + appHandleFunc(app.GetContext(), w, r) + } + } + + // ref: pkg/appsrv/appsrv:addDefaultHandlers + root.HandleFunc("/version", adapterF(appsrv.VersionHandler)) + root.HandleFunc("/stats", adapterF(appsrv.StatisticHandler)) + root.HandleFunc("/ping", adapterF(appsrv.PingHandler)) + root.HandleFunc("/worker_stats", adapterF(appsrv.WorkerStatsHandler)) + + // pprof handler + root.PathPrefix("/debug/pprof/").Handler(http.DefaultServeMux) +}