Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: monitor middleware reporting of CPU usage #2984

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions middleware/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package monitor

import (
"os"
"runtime"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -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)
}
}()
})
Expand Down Expand Up @@ -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))
gaby marked this conversation as resolved.
Show resolved Hide resolved
}

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