Skip to content

Commit

Permalink
feat(controller): enable pprof profiling support (#3769)
Browse files Browse the repository at this point in the history
* feat(controller): enable pprof profiling

Signed-off-by: John Wood <[email protected]>

* wip

Signed-off-by: John Wood <[email protected]>

* Consolidate --enable-pprof and --pprof-port into single config variable

Signed-off-by: John Wood <[email protected]>

---------

Signed-off-by: John Wood <[email protected]>
  • Loading branch information
johnmwood authored Aug 13, 2024
1 parent e619a51 commit 0397210
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cmd/rollouts-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"net/http"
"os"
"strings"
"time"
Expand Down Expand Up @@ -79,6 +80,7 @@ func newCommand() *cobra.Command {
printVersion bool
selfServiceNotificationEnabled bool
controllersEnabled []string
pprofAddress string
)
electOpts := controller.NewLeaderElectionOptions()
var command = cobra.Command{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down
24 changes: 24 additions & 0 deletions controller/profiling.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 0397210

Please sign in to comment.