Created
July 30, 2025 16:55
-
-
Save algermissen/a59fcde44224733cdafd90d62b011f1e to your computer and use it in GitHub Desktop.
Print Golang stack trace every 15 minutes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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