Skip to content

Commit 4262f5b

Browse files
nyufenggaby
andauthored
fix: monitor middleware reporting of CPU usage (#2984)
monitPIDCPU should be transient, not persistent. Co-authored-by: Juan Calderon-Perez <[email protected]>
1 parent 232c0fa commit 4262f5b

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

middleware/monitor/monitor.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package monitor
22

33
import (
44
"os"
5+
"runtime"
56
"sync"
67
"sync/atomic"
78
"time"
@@ -59,14 +60,14 @@ func New(config ...Config) fiber.Handler {
5960
// Start routine to update statistics
6061
once.Do(func() {
6162
p, _ := process.NewProcess(int32(os.Getpid())) //nolint:errcheck // TODO: Handle error
62-
63-
updateStatistics(p)
63+
numcpu := runtime.NumCPU()
64+
updateStatistics(p, numcpu)
6465

6566
go func() {
6667
for {
6768
time.Sleep(cfg.Refresh)
6869

69-
updateStatistics(p)
70+
updateStatistics(p, numcpu)
7071
}
7172
}()
7273
})
@@ -101,10 +102,10 @@ func New(config ...Config) fiber.Handler {
101102
}
102103
}
103104

104-
func updateStatistics(p *process.Process) {
105-
pidCPU, err := p.CPUPercent()
105+
func updateStatistics(p *process.Process, numcpu int) {
106+
pidCPU, err := p.Percent(0)
106107
if err == nil {
107-
monitPIDCPU.Store(pidCPU / 10)
108+
monitPIDCPU.Store(pidCPU / float64(numcpu))
108109
}
109110

110111
if osCPU, err := cpu.Percent(0, false); err == nil && len(osCPU) > 0 {

0 commit comments

Comments
 (0)