From 695ac10504625e7ba4330e6849f092a7ca2a632f Mon Sep 17 00:00:00 2001 From: Vihas Makwana Date: Fri, 22 Nov 2024 17:09:26 +0530 Subject: [PATCH] chore: remove redundant --- metric/cpu/metrics.go | 25 +++++-------------------- metric/cpu/metrics_windows.go | 6 +++--- 2 files changed, 8 insertions(+), 23 deletions(-) diff --git a/metric/cpu/metrics.go b/metric/cpu/metrics.go index 8d7e0860e3..c039d091a4 100644 --- a/metric/cpu/metrics.go +++ b/metric/cpu/metrics.go @@ -6,7 +6,7 @@ // not use this file except in compliance with the License. // You may obtain a copy of the License at // -// http://www.apache.org/licenses/LICENSE-2.0 +// http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, // software distributed under the License is distributed on an @@ -100,14 +100,14 @@ func New(hostfs resolve.Resolver) *Monitor { // This will overwrite the currently stored samples. func (m *Monitor) Fetch() (Metrics, error) { metric, err := Get(m.Hostfs) - if err != nil && !errors.Is(err, &PerfError{}) { + if err != nil { return Metrics{}, fmt.Errorf("error fetching CPU metrics: %w", err) } oldLastSample := m.lastSample m.lastSample = metric - return Metrics{previousSample: oldLastSample.totals, currentSample: metric.totals, count: len(metric.list), isTotals: true}, err + return Metrics{previousSample: oldLastSample.totals, currentSample: metric.totals, count: len(metric.list), isTotals: true}, nil } // FetchCores collects a new sample of CPU usage metrics per-core @@ -115,7 +115,7 @@ func (m *Monitor) Fetch() (Metrics, error) { func (m *Monitor) FetchCores() ([]Metrics, error) { metric, err := Get(m.Hostfs) - if err != nil && !errors.Is(err, &PerfError{}) { + if err != nil { return nil, fmt.Errorf("error fetching CPU metrics: %w", err) } @@ -140,7 +140,7 @@ func (m *Monitor) FetchCores() ([]Metrics, error) { } } m.lastSample = metric - return coreMetrics, err + return coreMetrics, nil } // Metrics stores the current and the last sample collected by a Beat. @@ -244,18 +244,3 @@ func cpuMetricTimeDelta(prev, current opt.Uint, timeDelta uint64, numCPU int) fl pct := float64(cpuDelta) / float64(timeDelta) return metric.Round(pct * float64(numCPU)) } - -type PerfError struct { - err error -} - -func (p *PerfError) Error() string { - if p.err == nil { - return "" - } - return fmt.Sprintf("Error while reading performance counter data: %s", p.err.Error()) -} - -func (p *PerfError) Unwrap() error { - return p.err -} diff --git a/metric/cpu/metrics_windows.go b/metric/cpu/metrics_windows.go index 7aa0e9881c..608a980c4b 100644 --- a/metric/cpu/metrics_windows.go +++ b/metric/cpu/metrics_windows.go @@ -67,7 +67,7 @@ func Get(_ resolve.Resolver) (CPUMetrics, error) { return CPUMetrics{}, fmt.Errorf("error calling populatePerCPUMetrics: %w", err) } - kernel, user, idle, err := populateGlobalCPUMetrics(q, int64(len(globalMetrics.list))) + kernel, user, idle, err := populateGlobalCPUMetrics(q, len(globalMetrics.list)) if err != nil { return CPUMetrics{}, fmt.Errorf("error calling populateGlobalCPUMetrics: %w", err) } @@ -79,7 +79,7 @@ func Get(_ resolve.Resolver) (CPUMetrics, error) { return globalMetrics, nil } -func populateGlobalCPUMetrics(q *pdh.Query, numCpus int64) (time.Duration, time.Duration, time.Duration, error) { +func populateGlobalCPUMetrics(q *pdh.Query, numCpus int) (time.Duration, time.Duration, time.Duration, error) { kernel, err := q.GetRawCounterValue(totalKernelTimeCounter) if err != nil { return 0, 0, 0, fmt.Errorf("error getting Privileged Time counter: %w", err) @@ -95,7 +95,7 @@ func populateGlobalCPUMetrics(q *pdh.Query, numCpus int64) (time.Duration, time. // _Total values returned by PerfCounters are averaged by number of cpus i.e. average time for system as a whole // Previously, we used to return sum of times for all CPUs. // To be backward compatible with previous version, multiply the average time by number of CPUs. - return time.Duration(kernel.FirstValue * 100 * numCpus), time.Duration(idle.FirstValue * 100 * numCpus), time.Duration(user.FirstValue * 100 * numCpus), nil + return time.Duration(kernel.FirstValue * 100 * int64(numCpus)), time.Duration(idle.FirstValue * 100 * int64(numCpus)), time.Duration(user.FirstValue * 100 * int64(numCpus)), nil } func populatePerCPUMetrics(q *pdh.Query) ([]CPU, error) {