Dump runtime stacks on USR1 reception (#292)

Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
This commit is contained in:
Kenfe-Mickaël Laventure 2016-08-30 09:47:21 -07:00 committed by Michael Crosby
parent 79c7673858
commit 4f0a33ac3d

View file

@ -6,6 +6,7 @@ import (
"net" "net"
"os" "os"
"os/signal" "os/signal"
"runtime"
"strings" "strings"
"sync" "sync"
"syscall" "syscall"
@ -87,6 +88,32 @@ var daemonFlags = []cli.Flag{
}, },
} }
// DumpStacks dumps the runtime stack.
func dumpStacks() {
var (
buf []byte
stackSize int
)
bufferLen := 16384
for stackSize == len(buf) {
buf = make([]byte, bufferLen)
stackSize = runtime.Stack(buf, true)
bufferLen *= 2
}
buf = buf[:stackSize]
logrus.Infof("=== BEGIN goroutine stack dump ===\n%s\n=== END goroutine stack dump ===", buf)
}
func setupDumpStacksTrap() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGUSR1)
go func() {
for range c {
dumpStacks()
}
}()
}
func main() { func main() {
logrus.SetFormatter(&logrus.TextFormatter{TimestampFormat: time.RFC3339Nano}) logrus.SetFormatter(&logrus.TextFormatter{TimestampFormat: time.RFC3339Nano})
app := cli.NewApp() app := cli.NewApp()
@ -99,6 +126,7 @@ func main() {
app.Usage = usage app.Usage = usage
app.Flags = daemonFlags app.Flags = daemonFlags
app.Before = func(context *cli.Context) error { app.Before = func(context *cli.Context) error {
setupDumpStacksTrap()
if context.GlobalBool("debug") { if context.GlobalBool("debug") {
logrus.SetLevel(logrus.DebugLevel) logrus.SetLevel(logrus.DebugLevel)
if context.GlobalDuration("metrics-interval") > 0 { if context.GlobalDuration("metrics-interval") > 0 {
@ -106,7 +134,6 @@ func main() {
return err return err
} }
} }
} }
if p := context.GlobalString("pprof-address"); len(p) > 0 { if p := context.GlobalString("pprof-address"); len(p) > 0 {
pprof.Enable(p) pprof.Enable(p)