Periodically collect runtime info when requested

This commit is contained in:
Frederik Ring 2024-02-10 12:10:16 +01:00
parent 68ba6ce5a1
commit 01e41f45d1

View File

@ -9,6 +9,7 @@ import (
"log/slog" "log/slog"
"os" "os"
"os/signal" "os/signal"
"runtime"
"syscall" "syscall"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
@ -122,7 +123,7 @@ func runScript(c *Config) (err error) {
return runErr return runErr
} }
func (c *command) runInForeground() error { func (c *command) runInForeground(profileCronExpression string) error {
cr := cron.New( cr := cron.New(
cron.WithParser( cron.WithParser(
cron.NewParser( cron.NewParser(
@ -189,6 +190,24 @@ func (c *command) runInForeground() error {
} }
} }
if profileCronExpression != "" {
cr.AddFunc(profileCronExpression, func() {
memStats := runtime.MemStats{}
runtime.ReadMemStats(&memStats)
c.logger.Info(
"Collecting runtime information",
"num_goroutines",
runtime.NumGoroutine(),
"memory_heap_alloc",
formatBytes(memStats.HeapAlloc, false),
"memory_heap_inuse",
formatBytes(memStats.HeapInuse, false),
"memory_heap_sys",
formatBytes(memStats.HeapSys, false),
)
})
}
var quit = make(chan os.Signal, 1) var quit = make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT) signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT)
cr.Start() cr.Start()
@ -214,11 +233,12 @@ func (c *command) runAsCommand() error {
func main() { func main() {
foreground := flag.Bool("foreground", false, "run the tool in the foreground") foreground := flag.Bool("foreground", false, "run the tool in the foreground")
profile := flag.String("profile", "", "collect runtime metrics and log them periodically on the given cron expression")
flag.Parse() flag.Parse()
c := newCommand() c := newCommand()
if *foreground { if *foreground {
c.must(c.runInForeground()) c.must(c.runInForeground(*profile))
} else { } else {
c.must(c.runAsCommand()) c.must(c.runAsCommand())
} }