Skip to content

Commit

Permalink
chore: remove redundant
Browse files Browse the repository at this point in the history
  • Loading branch information
VihasMakwana committed Nov 22, 2024
1 parent 3b4b69a commit 695ac10
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 23 deletions.
25 changes: 5 additions & 20 deletions metric/cpu/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -100,22 +100,22 @@ 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
// This will overwrite the currently stored samples.
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)
}

Expand All @@ -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.
Expand Down Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions metric/cpu/metrics_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit 695ac10

Please sign in to comment.