fix: derive CPU total from per-core average to avoid split measurement windows

This commit is contained in:
Joel Male
2026-02-25 15:17:30 +10:00
parent f95623aad8
commit 57e68204bd
+11 -10
View File
@@ -861,21 +861,22 @@ func collectMetrics(config *Config) (*Metrics, error) {
}
}
// CPU
cpuPercent, err := cpu.Percent(time.Second, false)
// CPU — single sample with percpu=true, then average for the total.
// Calling cpu.Percent twice with separate 1s windows can produce
// wildly inconsistent totals vs per-core values.
cpuPerCore, err := cpu.Percent(time.Second, true)
if err != nil {
return nil, fmt.Errorf("failed to get CPU percent: %w", err)
}
if len(cpuPercent) > 0 {
metrics.CPU.Percent = cpuPercent[0]
metrics.CPU.PerCore = cpuPerCore
if len(cpuPerCore) > 0 {
var sum float64
for _, p := range cpuPerCore {
sum += p
}
metrics.CPU.Percent = sum / float64(len(cpuPerCore))
}
cpuPercentPerCore, err := cpu.Percent(time.Second, true)
if err != nil {
return nil, fmt.Errorf("failed to get per-core CPU percent: %w", err)
}
metrics.CPU.PerCore = cpuPercentPerCore
cpuCount, err := cpu.Counts(true)
if err != nil {
return nil, fmt.Errorf("failed to get CPU count: %w", err)