diff --git a/cmd/exporter.go b/cmd/exporter.go index f445ae6..918eeb4 100644 --- a/cmd/exporter.go +++ b/cmd/exporter.go @@ -6,6 +6,7 @@ import ( "io" "log/slog" "os" + "sync" "time" "github.com/ncw/directio" @@ -32,7 +33,10 @@ func NewExporter(conf *Config, alloc *Alloc, metrics *Metrics) *Exporter { } // starts the primary go-routine, which will run the io checks for ever -func (exp *Exporter) RunIOchecks() { +func (exp *Exporter) RunIOchecks() sync.WaitGroup { + var wg sync.WaitGroup + wg.Add(1) + go func() { for { var res_r, res_w Result @@ -60,6 +64,8 @@ func (exp *Exporter) RunIOchecks() { time.Sleep(time.Duration(exp.conf.Sleeptime) * time.Second) } }() + + return wg } // call an io measurement and collect time needed diff --git a/cmd/root.go b/cmd/root.go index f1943b3..d661cc9 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -31,7 +31,7 @@ func Run() { alloc := NewAlloc() exporter := NewExporter(conf, alloc, metrics) - exporter.RunIOchecks() + wg := exporter.RunIOchecks() http.Handle("/metrics", promhttp.HandlerFor( metrics.registry, @@ -49,6 +49,8 @@ func Run() { if err := http.ListenAndServe(fmt.Sprintf(":%d", conf.Port), nil); err != nil { log.Fatal(err) } + + wg.Wait() } func report(err error, fd *os.File) bool {