Skip to content

Commit 8ecfe70

Browse files
committed
fix
1 parent 6975b43 commit 8ecfe70

File tree

6 files changed

+85
-20
lines changed

6 files changed

+85
-20
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ toolchain go1.24.2
77
require (
88
github.com/prometheus/client_golang v1.20.5
99
github.com/prometheus/common v0.61.0
10-
github.com/shirou/gopsutil/v3 v3.24.5
10+
github.com/shirou/gopsutil/v4 v4.24.11
1111
github.com/spf13/cobra v1.8.0
1212
github.com/spf13/viper v1.18.2
1313

@@ -28,6 +28,7 @@ require github.com/fxamacker/cbor/v2 v2.9.0
2828
require (
2929
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
3030
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
31+
github.com/ebitengine/purego v0.8.1 // indirect
3132
github.com/emicklei/go-restful/v3 v3.12.2 // indirect
3233
github.com/fsnotify/fsnotify v1.7.0 // indirect
3334
github.com/go-logr/logr v1.4.2 // indirect
@@ -57,7 +58,6 @@ require (
5758
github.com/prometheus/client_model v0.6.1 // indirect
5859
github.com/sagikazarmark/locafero v0.4.0 // indirect
5960
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
60-
github.com/shoenig/go-m1cpu v0.1.7 // indirect
6161
github.com/sourcegraph/conc v0.3.0 // indirect
6262
github.com/spf13/afero v1.11.0 // indirect
6363
github.com/spf13/cast v1.6.0 // indirect

go.sum

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
1010
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1111
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
1212
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
13+
github.com/ebitengine/purego v0.8.1 h1:sdRKd6plj7KYW33EH5As6YKfe8m9zbN9JMrOjNVF/BE=
14+
github.com/ebitengine/purego v0.8.1/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
1315
github.com/emicklei/go-restful/v3 v3.12.2 h1:DhwDP0vY3k8ZzE0RunuJy8GhNpPL6zqLkDf9B/a0/xU=
1416
github.com/emicklei/go-restful/v3 v3.12.2/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
1517
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
@@ -117,12 +119,8 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke
117119
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
118120
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
119121
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
120-
github.com/shirou/gopsutil/v3 v3.24.5 h1:i0t8kL+kQTvpAYToeuiVk3TgDeKOFioZO3Ztz/iZ9pI=
121-
github.com/shirou/gopsutil/v3 v3.24.5/go.mod h1:bsoOS1aStSs9ErQ1WWfxllSeS1K5D+U30r2NfcubMVk=
122-
github.com/shoenig/go-m1cpu v0.1.7 h1:C76Yd0ObKR82W4vhfjZiCp0HxcSZ8Nqd84v+HZ0qyI0=
123-
github.com/shoenig/go-m1cpu v0.1.7/go.mod h1:KkDOw6m3ZJQAPHbrzkZki4hnx+pDRR1Lo+ldA56wD5w=
124-
github.com/shoenig/test v1.7.0 h1:eWcHtTXa6QLnBvm0jgEabMRN/uJ4DMV3M8xUGgRkZmk=
125-
github.com/shoenig/test v1.7.0/go.mod h1:UxJ6u/x2v/TNs/LoLxBNJRV9DiwBBKYxXSyczsBHFoI=
122+
github.com/shirou/gopsutil/v4 v4.24.11 h1:WaU9xqGFKvFfsUv94SXcUPD7rCkU0vr/asVdQOBZNj8=
123+
github.com/shirou/gopsutil/v4 v4.24.11/go.mod h1:s4D/wg+ag4rG0WO7AiTj2BeYCRhym0vM7DHbZRxnIT8=
126124
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
127125
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
128126
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=

internal/metrics/collector.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ import (
3131
"go.opentelemetry.io/otel/sdk/resource"
3232
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
3333

34-
"github.com/shirou/gopsutil/v3/disk"
35-
"github.com/shirou/gopsutil/v3/host"
36-
"github.com/shirou/gopsutil/v3/load"
37-
"github.com/shirou/gopsutil/v3/mem"
38-
"github.com/shirou/gopsutil/v3/net"
39-
"github.com/shirou/gopsutil/v3/process"
34+
"github.com/shirou/gopsutil/v4/disk"
35+
"github.com/shirou/gopsutil/v4/host"
36+
"github.com/shirou/gopsutil/v4/load"
37+
"github.com/shirou/gopsutil/v4/mem"
38+
"github.com/shirou/gopsutil/v4/net"
39+
"github.com/shirou/gopsutil/v4/process"
4040

4141
constants "catops/config"
4242
)

internal/metrics/cpu.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"sync"
77
"time"
88

9-
"github.com/shirou/gopsutil/v3/cpu"
9+
"github.com/shirou/gopsutil/v4/cpu"
1010
)
1111

1212
// CPUMetrics contains detailed CPU usage breakdown

internal/metrics/network.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"sync"
88
"time"
99

10-
"github.com/shirou/gopsutil/v3/net"
10+
"github.com/shirou/gopsutil/v4/net"
1111
)
1212

1313
var (

internal/metrics/service_detector.go

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,78 @@ package metrics
33
import (
44
"fmt"
55
"strings"
6+
"sync"
7+
"time"
68

7-
"github.com/shirou/gopsutil/v3/net"
8-
"github.com/shirou/gopsutil/v3/process"
9+
"github.com/shirou/gopsutil/v4/net"
10+
"github.com/shirou/gopsutil/v4/process"
911
)
1012

13+
// Cache for process CPU times (for delta calculation)
14+
var (
15+
procCpuTimesCache = make(map[int32]procCpuCacheEntry)
16+
procCpuTimesCacheMu sync.RWMutex
17+
)
18+
19+
type procCpuCacheEntry struct {
20+
userTime float64
21+
sysTime float64
22+
sampleTime time.Time
23+
}
24+
25+
// getProcessCPUPercent calculates CPU% for a process using delta from cached values.
26+
// Non-blocking - uses process.Times() which just reads /proc/[pid]/stat.
27+
func getProcessCPUPercent(proc *process.Process) float64 {
28+
times, err := proc.Times()
29+
if err != nil || times == nil {
30+
return 0
31+
}
32+
33+
now := time.Now()
34+
pid := proc.Pid
35+
currentTotal := times.User + times.System
36+
37+
procCpuTimesCacheMu.Lock()
38+
defer procCpuTimesCacheMu.Unlock()
39+
40+
prev, exists := procCpuTimesCache[pid]
41+
if !exists {
42+
// First call - store baseline, return 0
43+
procCpuTimesCache[pid] = procCpuCacheEntry{
44+
userTime: times.User,
45+
sysTime: times.System,
46+
sampleTime: now,
47+
}
48+
return 0
49+
}
50+
51+
elapsed := now.Sub(prev.sampleTime).Seconds()
52+
if elapsed <= 0 {
53+
return 0
54+
}
55+
56+
prevTotal := prev.userTime + prev.sysTime
57+
cpuDelta := currentTotal - prevTotal
58+
59+
// Update cache
60+
procCpuTimesCache[pid] = procCpuCacheEntry{
61+
userTime: times.User,
62+
sysTime: times.System,
63+
sampleTime: now,
64+
}
65+
66+
// CPU% = (cpu time delta / wall time delta) * 100
67+
cpuPercent := (cpuDelta / elapsed) * 100
68+
if cpuPercent < 0 {
69+
cpuPercent = 0
70+
}
71+
if cpuPercent > 100 {
72+
cpuPercent = 100
73+
}
74+
75+
return cpuPercent
76+
}
77+
1178
// ListeningPort represents a port that is being listened on
1279
type ListeningPort struct {
1380
Port int `json:"port"`
@@ -61,8 +128,8 @@ func (d *ServiceDetector) DetectServices() ([]ServiceInfo, error) {
61128
continue // Skip unknown services
62129
}
63130

64-
// Get process stats
65-
cpuPercent, _ := proc.CPUPercent()
131+
// Get process stats using non-blocking delta calculation
132+
cpuPercent := getProcessCPUPercent(proc)
66133
memoryPercent, _ := proc.MemoryPercent()
67134
memoryInfo, _ := proc.MemoryInfo()
68135
status, _ := proc.Status()

0 commit comments

Comments
 (0)