Fix some test races and other things found with go test -race (#711)

This commit is contained in:
Raja Subramanian
2022-05-24 10:16:36 +05:30
committed by GitHub
parent a1e264bf34
commit 33032f6c4b
5 changed files with 57 additions and 16 deletions
@@ -5,10 +5,12 @@ package prometheus
import (
"runtime"
"sync"
"github.com/mackerelio/go-osstat/cpu"
)
var cpuStatsLock sync.RWMutex
var lastCPUTotal, lastCPUIdle uint64
func getCPUStats() (cpuLoad float32, numCPUs uint32, err error) {
@@ -17,12 +19,14 @@ func getCPUStats() (cpuLoad float32, numCPUs uint32, err error) {
return
}
cpuStatsLock.Lock()
if lastCPUTotal > 0 && lastCPUTotal < cpuInfo.Total {
cpuLoad = 1 - float32(cpuInfo.Idle-lastCPUIdle)/float32(cpuInfo.Total-lastCPUTotal)
}
lastCPUTotal = cpuInfo.Total
lastCPUIdle = cpuInfo.Idle
cpuStatsLock.Unlock()
numCPUs = uint32(runtime.NumCPU())