Skip to content

Commit

Permalink
Merge pull request #5005 from negz/pprofessor
Browse files Browse the repository at this point in the history
Use controller-runtime's pprof server rather than our own
  • Loading branch information
phisco committed Nov 15, 2023
2 parents 1a1333f + a872c03 commit 50e9ea3
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 62 deletions.
32 changes: 1 addition & 31 deletions cmd/crossplane/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
"context"
"crypto/tls"
"fmt"
"net/http"
"net/http/pprof"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -62,8 +60,6 @@ import (
"github.com/crossplane/crossplane/internal/xpkg"
)

const pprofPath = "/debug/pprof/"

// Command runs the core crossplane controllers
type Command struct {
Start startCommand `cmd:"" help:"Start Crossplane controllers."`
Expand Down Expand Up @@ -126,33 +122,6 @@ type startCommand struct {

// Run core Crossplane controllers.
func (c *startCommand) Run(s *runtime.Scheme, log logging.Logger) error { //nolint:gocyclo // Only slightly over.
if c.Profile != "" {
// NOTE(negz): These log messages attempt to match those emitted by
// controller-runtime's metrics HTTP server when it starts.
log.Debug("Profiling server is starting to listen", "addr", c.Profile)
go func() {

// Registering these explicitly ensures they're only served by the
// HTTP server we start explicitly for profiling.
mux := http.NewServeMux()
mux.HandleFunc(pprofPath, pprof.Index)
mux.HandleFunc(filepath.Join(pprofPath, "cmdline"), pprof.Cmdline)
mux.HandleFunc(filepath.Join(pprofPath, "profile"), pprof.Profile)
mux.HandleFunc(filepath.Join(pprofPath, "symbol"), pprof.Symbol)
mux.HandleFunc(filepath.Join(pprofPath, "trace"), pprof.Trace)

s := &http.Server{
Addr: c.Profile,
ReadTimeout: 2 * time.Minute,
WriteTimeout: 2 * time.Minute,
Handler: mux,
}
log.Debug("Starting server", "type", "pprof", "path", pprofPath, "addr", s.Addr)
err := s.ListenAndServe()
log.Debug("Profiling server has stopped listening", "error", err)
}()
}

cfg, err := ctrl.GetConfig()
if err != nil {
return errors.Wrap(err, "cannot get config")
Expand Down Expand Up @@ -199,6 +168,7 @@ func (c *startCommand) Run(s *runtime.Scheme, log logging.Logger) error { //noli
LeaseDuration: func() *time.Duration { d := 60 * time.Second; return &d }(),
RenewDeadline: func() *time.Duration { d := 50 * time.Second; return &d }(),

PprofBindAddress: c.Profile,
HealthProbeBindAddress: ":8081",
})
if err != nil {
Expand Down
32 changes: 1 addition & 31 deletions cmd/crossplane/rbac/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ limitations under the License.
package rbac

import (
"net/http"
"net/http/pprof"
"path/filepath"
"strings"
"time"

Expand All @@ -46,8 +43,6 @@ const (
ManagementPolicyBasic = string(rbaccontroller.ManagementPolicyBasic)
)

const pprofPath = "/debug/pprof/"

// KongVars represent the kong variables associated with the CLI parser
// required for the RBAC enum interpolation.
var KongVars = kong.Vars{
Expand Down Expand Up @@ -90,32 +85,6 @@ type startCommand struct {
// Run the RBAC manager.
func (c *startCommand) Run(s *runtime.Scheme, log logging.Logger) error {
log.Debug("Starting", "policy", c.ManagementPolicy)
if c.Profile != "" {
// NOTE(negz): These log messages attempt to match those emitted by
// controller-runtime's metrics HTTP server when it starts.
log.Debug("Profiling server is starting to listen", "addr", c.Profile)
go func() {

// Registering these explicitly ensures they're only served by the
// HTTP server we start specifically for profiling.
mux := http.NewServeMux()
mux.HandleFunc(pprofPath, pprof.Index)
mux.HandleFunc(filepath.Join(pprofPath, "cmdline"), pprof.Cmdline)
mux.HandleFunc(filepath.Join(pprofPath, "profile"), pprof.Profile)
mux.HandleFunc(filepath.Join(pprofPath, "symbol"), pprof.Symbol)
mux.HandleFunc(filepath.Join(pprofPath, "trace"), pprof.Trace)

s := &http.Server{
Addr: c.Profile,
ReadTimeout: 2 * time.Minute,
WriteTimeout: 2 * time.Minute,
Handler: mux,
}
log.Debug("Starting server", "type", "pprof", "path", pprofPath, "addr", s.Addr)
err := s.ListenAndServe()
log.Debug("Profiling server has stopped listening", "error", err)
}()
}

cfg, err := ctrl.GetConfig()
if err != nil {
Expand All @@ -130,6 +99,7 @@ func (c *startCommand) Run(s *runtime.Scheme, log logging.Logger) error {
Cache: cache.Options{
SyncPeriod: &c.SyncInterval,
},
PprofBindAddress: c.Profile,
})
if err != nil {
return errors.Wrap(err, "cannot create manager")
Expand Down

0 comments on commit 50e9ea3

Please sign in to comment.