From 0397210a79ff985a87f4a72bf8ec29bf29230fb9 Mon Sep 17 00:00:00 2001 From: John Wood Date: Tue, 13 Aug 2024 11:27:33 -0500 Subject: [PATCH] feat(controller): enable pprof profiling support (#3769) * feat(controller): enable pprof profiling Signed-off-by: John Wood * wip Signed-off-by: John Wood * Consolidate --enable-pprof and --pprof-port into single config variable Signed-off-by: John Wood --------- Signed-off-by: John Wood --- cmd/rollouts-controller/main.go | 8 ++++++++ controller/profiling.go | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 controller/profiling.go diff --git a/cmd/rollouts-controller/main.go b/cmd/rollouts-controller/main.go index c8deab3ea0..89e611ab95 100644 --- a/cmd/rollouts-controller/main.go +++ b/cmd/rollouts-controller/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "net/http" "os" "strings" "time" @@ -79,6 +80,7 @@ func newCommand() *cobra.Command { printVersion bool selfServiceNotificationEnabled bool controllersEnabled []string + pprofAddress string ) electOpts := controller.NewLeaderElectionOptions() var command = cobra.Command{ @@ -204,6 +206,11 @@ func newCommand() *cobra.Command { ingressWrapper, err := ingressutil.NewIngressWrapper(mode, kubeClient, kubeInformerFactory) checkError(err) + if pprofAddress != "" { + mux := controller.NewPProfServer() + go func() { log.Println(http.ListenAndServe(pprofAddress, mux)) }() + } + var cm *controller.Manager enabledControllers, err := getEnabledControllers(controllersEnabled) @@ -310,6 +317,7 @@ func newCommand() *cobra.Command { command.Flags().DurationVar(&electOpts.LeaderElectionRetryPeriod, "leader-election-retry-period", controller.DefaultLeaderElectionRetryPeriod, "The duration the clients should wait between attempting acquisition and renewal of a leadership. This is only applicable if leader election is enabled.") command.Flags().BoolVar(&selfServiceNotificationEnabled, "self-service-notification-enabled", false, "Allows rollouts controller to pull notification config from the namespace that the rollout resource is in. This is useful for self-service notification.") command.Flags().StringSliceVar(&controllersEnabled, "controllers", nil, "Explicitly specify the list of controllers to run, currently only supports 'analysis', eg. --controller=analysis. Default: all controllers are enabled") + command.Flags().StringVar(&pprofAddress, "enable-pprof-address", "", "Enable pprof profiling on controller by providing a server address.") return &command } diff --git a/controller/profiling.go b/controller/profiling.go new file mode 100644 index 0000000000..03920906ac --- /dev/null +++ b/controller/profiling.go @@ -0,0 +1,24 @@ +package controller + +import ( + "fmt" + "net/http" + "net/http/pprof" +) + +const ( + ProfilingPath = "/debug/pprof" +) + +// NewPProfServer returns a new pprof server to gather runtime profiling data +func NewPProfServer() *http.ServeMux { + mux := http.NewServeMux() + + mux.HandleFunc(ProfilingPath, pprof.Index) + mux.HandleFunc(fmt.Sprintf("%s/cmdline", ProfilingPath), pprof.Cmdline) + mux.HandleFunc(fmt.Sprintf("%s/profile", ProfilingPath), pprof.Profile) + mux.HandleFunc(fmt.Sprintf("%s/symbol", ProfilingPath), pprof.Symbol) + mux.HandleFunc(fmt.Sprintf("%s/trace", ProfilingPath), pprof.Trace) + + return mux +}