From 5eb50e369b034d9265147b7d0aa7f94f18c89b0e Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Tue, 18 Jul 2023 21:03:49 +0800 Subject: [PATCH] fix: disable application profiling by default --- pkg/apis/identity/consts.go | 1 + pkg/appsrv/appsrv.go | 10 +++++++++- pkg/appsrv/handlers.go | 2 +- pkg/appsrv/send.go | 3 +-- pkg/cloudcommon/app/app.go | 3 +++ pkg/cloudcommon/options/options.go | 2 ++ 6 files changed, 17 insertions(+), 4 deletions(-) diff --git a/pkg/apis/identity/consts.go b/pkg/apis/identity/consts.go index 4c48bb6bbc..58ae23d121 100644 --- a/pkg/apis/identity/consts.go +++ b/pkg/apis/identity/consts.go @@ -151,6 +151,7 @@ var ( "is_slave_node", "config_sync_period_seconds", + "enable_app_profiling", // ############################ // db blacklist options diff --git a/pkg/appsrv/appsrv.go b/pkg/appsrv/appsrv.go index 536ae93758..50a62257cc 100644 --- a/pkg/appsrv/appsrv.go +++ b/pkg/appsrv/appsrv.go @@ -73,6 +73,8 @@ type Application struct { exception func(method, path string, body jsonutils.JSONObject, err error) isTLS bool + + enableProfiling bool } const ( @@ -553,7 +555,9 @@ func (app *Application) ListenAndServeTLSWithCleanup2(addr string, certFile, key httpSrv := app.initServer(addr) if isMaster { app.addDefaultHandlers() - AddPProfHandler("", app) + if app.enableProfiling { + addPProfHandler("", app) + } app.httpServer = httpSrv app.registerCleanShutdown(app.httpServer, onStop) } else { @@ -665,3 +669,7 @@ func FetchEnv(ctx context.Context, w http.ResponseWriter, r *http.Request) (para func (app *Application) GetContext() context.Context { return app.context } + +func (app *Application) EnableProfiling() { + app.enableProfiling = true +} diff --git a/pkg/appsrv/handlers.go b/pkg/appsrv/handlers.go index 9dec8a034e..889b7e32b1 100644 --- a/pkg/appsrv/handlers.go +++ b/pkg/appsrv/handlers.go @@ -51,7 +51,7 @@ func CORSHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { } }*/ -func AddPProfHandler(prefix string, app *Application) { +func addPProfHandler(prefix string, app *Application) { pp := "/debug/pprof" if prefix != "" { prefix = fmt.Sprintf("%s/%s", prefix, pp) diff --git a/pkg/appsrv/send.go b/pkg/appsrv/send.go index ba301ec813..d0b0c519bf 100644 --- a/pkg/appsrv/send.go +++ b/pkg/appsrv/send.go @@ -22,10 +22,9 @@ import ( "strconv" "time" - "github.com/pkg/errors" - "yunion.io/x/jsonutils" "yunion.io/x/log" + "yunion.io/x/pkg/errors" "yunion.io/x/pkg/gotypes" ) diff --git a/pkg/cloudcommon/app/app.go b/pkg/cloudcommon/app/app.go index 96b97fe6ce..52bb14345b 100644 --- a/pkg/cloudcommon/app/app.go +++ b/pkg/cloudcommon/app/app.go @@ -37,6 +37,9 @@ func InitApp(options *common_options.BaseOptions, dbAccess bool) *appsrv.Applica // if dbConn != nil { // app.SetContext(appsrv.APP_CONTEXT_KEY_DB, dbConn) //} + if options.EnableAppProfiling { + app.EnableProfiling() + } return app } diff --git a/pkg/cloudcommon/options/options.go b/pkg/cloudcommon/options/options.go index 40d4ed3869..a377a1cd9f 100644 --- a/pkg/cloudcommon/options/options.go +++ b/pkg/cloudcommon/options/options.go @@ -116,6 +116,8 @@ type BaseOptions struct { PlatformName string `help:"identity name of this platform" default:"Cloudpods"` PlatformNames map[string]string `help:"identity name of this platform by language"` + + EnableAppProfiling bool `help:"enable profiling API" default:"false"` } const (