fix: monitor middleware reporting of CPU usage (#2984)

monitPIDCPU should be transient, not persistent.

Co-authored-by: Juan Calderon-Perez <835733+gaby@users.noreply.github.com>
pull/3043/head
Feng 2024-06-24 22:32:04 +08:00 committed by GitHub
parent 232c0fac0d
commit 4262f5b591
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 6 deletions

View File

@ -2,6 +2,7 @@ package monitor
import (
"os"
"runtime"
"sync"
"sync/atomic"
"time"
@ -59,14 +60,14 @@ func New(config ...Config) fiber.Handler {
// Start routine to update statistics
once.Do(func() {
p, _ := process.NewProcess(int32(os.Getpid())) //nolint:errcheck // TODO: Handle error
updateStatistics(p)
numcpu := runtime.NumCPU()
updateStatistics(p, numcpu)
go func() {
for {
time.Sleep(cfg.Refresh)
updateStatistics(p)
updateStatistics(p, numcpu)
}
}()
})
@ -101,10 +102,10 @@ func New(config ...Config) fiber.Handler {
}
}
func updateStatistics(p *process.Process) {
pidCPU, err := p.CPUPercent()
func updateStatistics(p *process.Process, numcpu int) {
pidCPU, err := p.Percent(0)
if err == nil {
monitPIDCPU.Store(pidCPU / 10)
monitPIDCPU.Store(pidCPU / float64(numcpu))
}
if osCPU, err := cpu.Percent(0, false); err == nil && len(osCPU) > 0 {