Skip to content

Commit 3ec0ea3

Browse files
opm: always serve pprof endpoints
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 <[email protected]>
1 parent bca2bfb commit 3ec0ea3

File tree

1 file changed

+4
-22
lines changed

1 file changed

+4
-22
lines changed

cmd/opm/serve/serve.go

+4-22
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,17 @@ will not be reflected in the served content.
8080
cmd.Flags().BoolVar(&s.debug, "debug", false, "enable debug logging")
8181
cmd.Flags().StringVarP(&s.terminationLog, "termination-log", "t", "/dev/termination-log", "path to a container termination log file")
8282
cmd.Flags().StringVarP(&s.port, "port", "p", "50051", "port number to serve on")
83-
cmd.Flags().StringVar(&s.pprofAddr, "pprof-addr", "", "address of startup profiling endpoint (addr:port format)")
83+
cmd.Flags().StringVar(&s.pprofAddr, "pprof-addr", "localhost:6060", "address of startup profiling endpoint (addr:port format)")
8484
cmd.Flags().StringVar(&s.cacheDir, "cache-dir", "", "if set, sync and persist server cache directory")
8585
cmd.Flags().BoolVar(&s.cacheOnly, "cache-only", false, "sync the serve cache and exit without serving")
8686
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), ")
8787
return cmd
8888
}
8989

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

196-
func (p *profilerInterface) isEnabled() bool {
197-
return p.addr != ""
198-
}
199-
200199
func (p *profilerInterface) startEndpoint() error {
201-
// short-circuit if not enabled
202-
if !p.isEnabled() {
203-
return nil
204-
}
205-
206200
mux := http.NewServeMux()
207201
mux.HandleFunc("/debug/pprof/", endpoint.Index)
208202
mux.HandleFunc("/debug/pprof/cmdline", endpoint.Cmdline)
@@ -235,11 +229,6 @@ func (p *profilerInterface) startEndpoint() error {
235229
}
236230

237231
func (p *profilerInterface) startCpuProfileCache() error {
238-
// short-circuit if not enabled
239-
if !p.isEnabled() {
240-
return nil
241-
}
242-
243232
p.logger.Infof("start caching cpu profile data at %q", defaultCpuStartupPath)
244233
if err := pprof.StartCPUProfile(&p.cache); err != nil {
245234
return err
@@ -249,10 +238,6 @@ func (p *profilerInterface) startCpuProfileCache() error {
249238
}
250239

251240
func (p *profilerInterface) stopCpuProfileCache() {
252-
// short-circuit if not enabled
253-
if !p.isEnabled() {
254-
return
255-
}
256241
pprof.StopCPUProfile()
257242
p.setCacheReady()
258243
p.logger.Info("stopped caching cpu profile data")
@@ -266,9 +251,6 @@ func (p *profilerInterface) httpHandler(w http.ResponseWriter, r *http.Request)
266251
}
267252

268253
func (p *profilerInterface) stopEndpoint(ctx context.Context) error {
269-
if !p.isEnabled() {
270-
return nil
271-
}
272254
if err := p.server.Shutdown(ctx); err != nil {
273255
return err
274256
}

0 commit comments

Comments
 (0)