Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save algermissen/a59fcde44224733cdafd90d62b011f1e to your computer and use it in GitHub Desktop.
Save algermissen/a59fcde44224733cdafd90d62b011f1e to your computer and use it in GitHub Desktop.
Print Golang stack trace every 15 minutes
go func() {
ticker := time.NewTicker(15 * time.Minute)
defer ticker.Stop()
for {
<-ticker.C
printGoroutines()
}
}()
func printGoroutines() {
const initSize = 64 * 1024 // Initial buffer size (64KB)
buf := make([]byte, initSize)
for {
n := runtime.Stack(buf, true)
if n < len(buf) {
os.Stdout.Write([]byte("Goroutine list:\n"))
os.Stdout.Write(buf[:n])
os.Stdout.Write([]byte("\n"))
return
}
// Buffer was too small; double it and try again
buf = make([]byte, 2*len(buf))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment