Skip to content

Commit

Permalink
opm: always serve pprof endpoints
Browse files Browse the repository at this point in the history
There's no substantial runtime cost to serving the endpoints by default,
and when things hit the fan you always need to query them in situ.

Signed-off-by: Steve Kuznetsov <skuznets@redhat.com>
  • Loading branch information
stevekuznetsov committed Jul 27, 2023
1 parent bca2bfb commit 3ec0ea3
Showing 1 changed file with 4 additions and 22 deletions.
26 changes: 4 additions & 22 deletions cmd/opm/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,17 @@ will not be reflected in the served content.
cmd.Flags().BoolVar(&s.debug, "debug", false, "enable debug logging")
cmd.Flags().StringVarP(&s.terminationLog, "termination-log", "t", "/dev/termination-log", "path to a container termination log file")
cmd.Flags().StringVarP(&s.port, "port", "p", "50051", "port number to serve on")
cmd.Flags().StringVar(&s.pprofAddr, "pprof-addr", "", "address of startup profiling endpoint (addr:port format)")
cmd.Flags().StringVar(&s.pprofAddr, "pprof-addr", "localhost:6060", "address of startup profiling endpoint (addr:port format)")
cmd.Flags().StringVar(&s.cacheDir, "cache-dir", "", "if set, sync and persist server cache directory")
cmd.Flags().BoolVar(&s.cacheOnly, "cache-only", false, "sync the serve cache and exit without serving")
cmd.Flags().BoolVar(&s.cacheEnforceIntegrity, "cache-enforce-integrity", false, "exit with error if cache is not present or has been invalidated. (default: true when --cache-dir is set and --cache-only is false, false otherwise), ")
return cmd
}

func (s *serve) run(ctx context.Context) error {
if s.pprofAddr == "" {
s.logger.Fatal("--pprof-addr cannot be empty")
}
p := newProfilerInterface(s.pprofAddr, s.logger)
if err := p.startEndpoint(); err != nil {
return fmt.Errorf("could not start pprof endpoint: %v", err)
Expand Down Expand Up @@ -193,16 +196,7 @@ func newProfilerInterface(a string, log *logrus.Entry) *profilerInterface {
}
}

func (p *profilerInterface) isEnabled() bool {
return p.addr != ""
}

func (p *profilerInterface) startEndpoint() error {
// short-circuit if not enabled
if !p.isEnabled() {
return nil
}

mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", endpoint.Index)
mux.HandleFunc("/debug/pprof/cmdline", endpoint.Cmdline)
Expand Down Expand Up @@ -235,11 +229,6 @@ func (p *profilerInterface) startEndpoint() error {
}

func (p *profilerInterface) startCpuProfileCache() error {
// short-circuit if not enabled
if !p.isEnabled() {
return nil
}

p.logger.Infof("start caching cpu profile data at %q", defaultCpuStartupPath)
if err := pprof.StartCPUProfile(&p.cache); err != nil {
return err
Expand All @@ -249,10 +238,6 @@ func (p *profilerInterface) startCpuProfileCache() error {
}

func (p *profilerInterface) stopCpuProfileCache() {
// short-circuit if not enabled
if !p.isEnabled() {
return
}
pprof.StopCPUProfile()
p.setCacheReady()
p.logger.Info("stopped caching cpu profile data")
Expand All @@ -266,9 +251,6 @@ func (p *profilerInterface) httpHandler(w http.ResponseWriter, r *http.Request)
}

func (p *profilerInterface) stopEndpoint(ctx context.Context) error {
if !p.isEnabled() {
return nil
}
if err := p.server.Shutdown(ctx); err != nil {
return err
}
Expand Down

0 comments on commit 3ec0ea3

Please sign in to comment.