kubectl flush profiling when get a sigterm

Signed-off-by: Ricardo Pchevuzinske Katz <ricardo.katz@gmail.com>
This commit is contained in:
Ricardo Pchevuzinske Katz 2020-10-29 16:49:03 -03:00
parent 6352f01e66
commit 912d9673cb

View File

@ -19,6 +19,7 @@ package cmd
import ( import (
"fmt" "fmt"
"os" "os"
"os/signal"
"runtime" "runtime"
"runtime/pprof" "runtime/pprof"
@ -44,15 +45,16 @@ func initProfiling() error {
if err != nil { if err != nil {
return err return err
} }
return pprof.StartCPUProfile(f) err = pprof.StartCPUProfile(f)
if err != nil {
return err
}
// Block and mutex profiles need a call to Set{Block,Mutex}ProfileRate to // Block and mutex profiles need a call to Set{Block,Mutex}ProfileRate to
// output anything. We choose to sample all events. // output anything. We choose to sample all events.
case "block": case "block":
runtime.SetBlockProfileRate(1) runtime.SetBlockProfileRate(1)
return nil
case "mutex": case "mutex":
runtime.SetMutexProfileFraction(1) runtime.SetMutexProfileFraction(1)
return nil
default: default:
// Check the profile name is valid. // Check the profile name is valid.
if profile := pprof.Lookup(profileName); profile == nil { if profile := pprof.Lookup(profileName); profile == nil {
@ -60,6 +62,16 @@ func initProfiling() error {
} }
} }
// If the command is interrupted before the end (ctrl-c), flush the
// profiling files
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
flushProfiling()
os.Exit(0)
}()
return nil return nil
} }