From e200808ab758a25d6c6a64cf60bc686fb2d56b3b Mon Sep 17 00:00:00 2001 From: Shireesh Anjal <355479+anjalshireesh@users.noreply.github.com> Date: Thu, 20 Jun 2024 23:25:03 +0530 Subject: [PATCH] fix errors in metrics code on macos (#19965) - do not load proc fs metrics in case of macos - null-check TimeStat before accessing --- cmd/metrics-v2.go | 2 +- cmd/metrics-v3-system-cpu.go | 20 +++++++++++--------- cmd/metrics-v3-system-process.go | 12 +++++++----- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/cmd/metrics-v2.go b/cmd/metrics-v2.go index 73b9b4197..98c0161e8 100644 --- a/cmd/metrics-v2.go +++ b/cmd/metrics-v2.go @@ -1688,7 +1688,7 @@ func getMinioProcMetrics() *MetricsGroupV2 { cacheInterval: 10 * time.Second, } mg.RegisterRead(func(ctx context.Context) (metrics []MetricV2) { - if runtime.GOOS == "windows" { + if runtime.GOOS == globalWindowsOSName || runtime.GOOS == globalMacOSName { return nil } diff --git a/cmd/metrics-v3-system-cpu.go b/cmd/metrics-v3-system-cpu.go index d6526b0fd..cb31b8358 100644 --- a/cmd/metrics-v3-system-cpu.go +++ b/cmd/metrics-v3-system-cpu.go @@ -55,15 +55,17 @@ func loadCPUMetrics(ctx context.Context, m MetricValues, c *metricsCache) error } ts := cpuMetrics.TimesStat - tot := ts.User + ts.System + ts.Idle + ts.Iowait + ts.Nice + ts.Steal - cpuUserVal := math.Round(ts.User/tot*100*100) / 100 - m.Set(sysCPUUser, cpuUserVal) - cpuSystemVal := math.Round(ts.System/tot*100*100) / 100 - m.Set(sysCPUSystem, cpuSystemVal) - cpuNiceVal := math.Round(ts.Nice/tot*100*100) / 100 - m.Set(sysCPUNice, cpuNiceVal) - cpuStealVal := math.Round(ts.Steal/tot*100*100) / 100 - m.Set(sysCPUSteal, cpuStealVal) + if ts != nil { + tot := ts.User + ts.System + ts.Idle + ts.Iowait + ts.Nice + ts.Steal + cpuUserVal := math.Round(ts.User/tot*100*100) / 100 + m.Set(sysCPUUser, cpuUserVal) + cpuSystemVal := math.Round(ts.System/tot*100*100) / 100 + m.Set(sysCPUSystem, cpuSystemVal) + cpuNiceVal := math.Round(ts.Nice/tot*100*100) / 100 + m.Set(sysCPUNice, cpuNiceVal) + cpuStealVal := math.Round(ts.Steal/tot*100*100) / 100 + m.Set(sysCPUSteal, cpuStealVal) + } // metrics-resource.go runs a job to collect resource metrics including their Avg values and // stores them in resourceMetricsMap. We can use it to get the Avg values of CPU idle and IOWait. diff --git a/cmd/metrics-v3-system-process.go b/cmd/metrics-v3-system-process.go index 0db6140bc..01dbba88e 100644 --- a/cmd/metrics-v3-system-process.go +++ b/cmd/metrics-v3-system-process.go @@ -156,11 +156,13 @@ func loadProcessMetrics(ctx context.Context, m MetricValues, c *metricsCache) er m.Set(processUptimeSeconds, time.Since(globalBootTime).Seconds()) } - p, err := procfs.Self() - if err != nil { - metricsLogIf(ctx, err) - } else { - loadProcFSMetrics(ctx, p, m) + if runtime.GOOS != globalWindowsOSName && runtime.GOOS != globalMacOSName { + p, err := procfs.Self() + if err != nil { + metricsLogIf(ctx, err) + } else { + loadProcFSMetrics(ctx, p, m) + } } if globalIsDistErasure && globalLockServer != nil {