Skip to content

Commit c0d0eea

Browse files
authored
fix: lint fixes (#2138)
* feat: lint fixes for cpu fix * fix: lint fixes * fix: lint fixes
1 parent 28cb2fe commit c0d0eea

File tree

6 files changed

+35
-12
lines changed

6 files changed

+35
-12
lines changed

internal/windows/api/pdh_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ func BenchmarkUTF16PtrToString(b *testing.B) {
172172
// BenchmarkUTF16PtrToString_LongString benchmarks with a longer string.
173173
func BenchmarkUTF16PtrToString_LongString(b *testing.B) {
174174
// Create a longer test string
175-
testString := "This is a much longer string to test the performance of the UTF16PtrToString function with more realistic data that might be encountered in real-world scenarios"
175+
testString := "This is a much longer string to test the performance of the UTF16PtrToString function " +
176+
"with more realistic data that might be encountered in real-world scenarios"
176177
utf16Ptr, err := syscall.UTF16PtrFromString(testString)
177178
require.NoError(b, err)
178179

pkg/metrics/cpu.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package metrics
44

55
import (
6+
"errors"
67
"fmt"
78
"runtime/debug"
89

@@ -17,6 +18,9 @@ import (
1718
var (
1819
cpuLog = log.WithComponent("CPUMonitor")
1920

21+
// ErrCPUMonitorPanic is returned when a panic occurs in CPUMonitor.Sample.
22+
ErrCPUMonitorPanic = errors.New("panic in CPUMonitor.Sample")
23+
2024
// defaultCPUSample is a reusable zero-value CPU sample.
2125
defaultCPUSample = &CPUSample{
2226
CPUPercent: 0,
@@ -53,13 +57,15 @@ func (m *CPUMonitor) Close() error {
5357
if m.windowsMonitor != nil {
5458
return m.windowsMonitor.close()
5559
}
60+
5661
return nil
5762
}
5863

59-
func (m *CPUMonitor) Sample() (sample *CPUSample, err error) {
64+
func (m *CPUMonitor) Sample() (*CPUSample, error) {
65+
var err error
6066
defer func() {
6167
if panicErr := recover(); panicErr != nil {
62-
err = fmt.Errorf("panic in CPUMonitor.Sample: %v\nStack: %s", panicErr, debug.Stack())
68+
err = fmt.Errorf("%w: %v\nStack: %s", ErrCPUMonitorPanic, panicErr, debug.Stack())
6369
}
6470
}()
6571

@@ -92,7 +98,8 @@ func (m *CPUMonitor) Sample() (sample *CPUSample, err error) {
9298
var userPercent, stolenPercent, systemPercent, ioWaitPercent float64
9399

94100
// Calculate total manually instead of using deprecated Total() method
95-
deltaTotal := delta.User + delta.Nice + delta.System + delta.Idle + delta.Iowait + delta.Irq + delta.Softirq + delta.Steal + delta.Guest + delta.GuestNice
101+
deltaTotal := delta.User + delta.Nice + delta.System + delta.Idle + delta.Iowait +
102+
delta.Irq + delta.Softirq + delta.Steal + delta.Guest + delta.GuestNice
96103
if deltaTotal != 0 {
97104
userPercent = userDelta / deltaTotal * 100.0
98105
stolenPercent = stolenDelta / deltaTotal * 100.0
@@ -101,7 +108,7 @@ func (m *CPUMonitor) Sample() (sample *CPUSample, err error) {
101108
}
102109
idlePercent := 100 - userPercent - systemPercent - ioWaitPercent - stolenPercent
103110

104-
sample = &CPUSample{
111+
sample := &CPUSample{
105112
CPUPercent: userPercent + systemPercent + ioWaitPercent + stolenPercent,
106113
CPUUserPercent: userPercent,
107114
CPUSystemPercent: systemPercent,
@@ -117,7 +124,7 @@ func (m *CPUMonitor) Sample() (sample *CPUSample, err error) {
117124

118125
m.last = currentTimes
119126

120-
return
127+
return sample, err
121128
}
122129

123130
func cpuDelta(current, previous *cpu.TimesStat) *cpu.TimesStat {

pkg/metrics/cpu_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ func TestCPUSample(t *testing.T) {
5050
assert.LessOrEqual(t, result.CPUSystemPercent, 100.0, "CPU system percent should be <= 100")
5151
assert.GreaterOrEqual(t, result.CPUIdlePercent, 0.0, "CPU idle percent should be >= 0")
5252
assert.LessOrEqual(t, result.CPUIdlePercent, 100.0, "CPU idle percent should be <= 100")
53-
54-
// Platform-specific validations would be handled in platform-specific test files
5553
}
5654

5755
func TestCPUSample_MultipleCalls(t *testing.T) {
@@ -91,6 +89,7 @@ func TestCPUSample_JSON_Marshaling(t *testing.T) {
9189

9290
// Test JSON unmarshaling
9391
var unmarshaledSample CPUSample
92+
9493
err = json.Unmarshal(jsonData, &unmarshaledSample)
9594
require.NoError(t, err)
9695
assert.InDelta(t, sample.CPUPercent, unmarshaledSample.CPUPercent, 0.001)

pkg/metrics/cpu_unix.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
// Copyright 2020 New Relic Corporation. All rights reserved.
55
// SPDX-License-Identifier: Apache-2.0
6+
67
package metrics
78

89
import (

pkg/metrics/cpu_windows.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package metrics
88

99
import (
10+
"errors"
1011
"fmt"
1112
"time"
1213

@@ -17,7 +18,12 @@ import (
1718
)
1819

1920
//nolint:gochecknoglobals
20-
var cpuWindowsLog = log.WithComponent("CPUWindows")
21+
var (
22+
cpuWindowsLog = log.WithComponent("CPUWindows")
23+
24+
// ErrNoUserTimeData indicates no user time data is available.
25+
ErrNoUserTimeData = errors.New("no user time data available")
26+
)
2127

2228
// Windows CPU performance counter paths using wildcard for all cores.
2329
const (
@@ -49,10 +55,15 @@ type WindowsCPUMonitor struct {
4955
func NewCPUMonitor(context agent.AgentContext) *CPUMonitor {
5056
winMonitor := &WindowsCPUMonitor{
5157
context: context,
58+
rawPoll: nil,
59+
started: false,
5260
requiresTwoSamples: true, // PDH requires two samples for rate counters
61+
lastSample: nil,
62+
lastTimestamp: time.Time{},
5363
}
5464
return &CPUMonitor{
5565
context: context,
66+
last: nil,
5667
cpuTimes: nil,
5768
windowsMonitor: winMonitor,
5869
}
@@ -79,6 +90,7 @@ func (w *WindowsCPUMonitor) initializeRawPDH() error {
7990
}
8091

8192
w.started = true
93+
8294
return nil
8395
}
8496

@@ -101,7 +113,7 @@ func (w *WindowsCPUMonitor) sample() (*CPUSample, error) {
101113
idleTimeData := rawData[idleTimeAllCores]
102114

103115
if len(userTimeData) == 0 {
104-
return nil, fmt.Errorf("no user time data available")
116+
return nil, fmt.Errorf("failed to get CPU user time data: %w", ErrNoUserTimeData) //nolint:wrapcheck
105117
}
106118

107119
// For the first sample, we need two collections to calculate rates
@@ -177,7 +189,7 @@ func (w *WindowsCPUMonitor) sample() (*CPUSample, error) {
177189
return sample, nil
178190
}
179191

180-
// calculateCPUTimeDelta calculates the delta between current and last counter samples
192+
// calculateCPUTimeDelta calculates the delta between current and last counter samples.
181193
func (w *WindowsCPUMonitor) calculateCPUTimeDelta(
182194
currentData []nrwin.CPUGroupInfo,
183195
lastData []nrwin.CPUGroupInfo,
@@ -215,6 +227,7 @@ func (w *WindowsCPUMonitor) calculateCPUTimeDelta(
215227
"last": lastInfo.RawValue.FirstValue,
216228
"delta": delta,
217229
}).Debug("Counter wrapped - skipping sample (current < last)")
230+
218231
continue
219232
}
220233

@@ -247,6 +260,7 @@ func (w *WindowsCPUMonitor) close() error {
247260
if w.started && w.rawPoll != nil {
248261
return w.rawPoll.Close()
249262
}
263+
250264
return nil
251265
}
252266

pkg/metrics/cpu_windows_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ func TestCalculatePercent(t *testing.T) {
260260
t.Parallel()
261261

262262
result := calculatePercent(tt.part, tt.total)
263-
assert.InDelta(t, tt.expected, result, 0.0001, "calculatePercent(%v, %v) should return %f, got %f", tt.part, tt.total, tt.expected, result)
263+
assert.InDelta(t, tt.expected, result, 0.0001,
264+
"calculatePercent(%v, %v) should return %f, got %f", tt.part, tt.total, tt.expected, result)
264265
})
265266
}
266267
}

0 commit comments

Comments
 (0)