-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Refactor CPU usage metrics handling for platform-specific logic
Separated CPU usage metric calculations into platform-specific files to improve code organization. `cpu_other.go` handles non-Linux platforms while `cpu_linux.go` adds additional metrics and handles Linux-specific logic. Updated main statistics processing to use the new functions. Signed-off-by: Christian Roessner <[email protected]>
- Loading branch information
Christian Roessner
committed
Oct 7, 2024
1 parent
8cd752c
commit 26ec71f
Showing
3 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (C) 2024 Christian Rößner | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
//go:build linux | ||
|
||
package stats | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
) | ||
|
||
var cpuNiceUsage = promauto.NewGauge(prometheus.GaugeOpts{ | ||
Name: "cpu_nice_usage_percent", | ||
Help: "CPU nice usage in percent", | ||
}) | ||
|
||
var cpuIowaitUsage = promauto.NewGauge(prometheus.GaugeOpts{ | ||
Name: "cpu_iowait_usage_percent", | ||
Help: "CPU iowait usage in percent", | ||
}) | ||
|
||
var cpuIRQUsage = promauto.NewGauge(prometheus.GaugeOpts{ | ||
Name: "cpu_irq_usage_percent", | ||
Help: "CPU irq usage in percent", | ||
}) | ||
|
||
var cpuSoftIRQUsage = promauto.NewGauge(prometheus.GaugeOpts{ | ||
Name: "cpu_softirq_usage_percent", | ||
Help: "CPU softirq usage in percent", | ||
}) | ||
|
||
var cpuStealUsage = promauto.NewGauge(prometheus.GaugeOpts{ | ||
Name: "cpu_steal_usage_percent", | ||
Help: "CPU steal usage in percent", | ||
}) | ||
|
||
func setNewStats(oldCpu, newCpu *cpu.Stats, total float64) { | ||
cpuUserUsage.Set(float64(newCpu.User-oldCpu.User) / total * 100) | ||
cpuSystemUsage.Set(float64(newCpu.System-oldCpu.System) / total * 100) | ||
cpuIdleUsage.Set(float64(newCpu.Idle-oldCpu.Idle) / total * 100) | ||
|
||
cpuIowaitUsage.Set(float64(newCpu.Iowait-oldCpu.Iowait) / total * 100) | ||
cpuIRQUsage.Set(float64(newCpu.IRQ-oldCpu.IRQ) / total * 100) | ||
cpuSoftIRQUsage.Set(float64(newCpu.SoftIRQ-oldCpu.SoftIRQ) / total * 100) | ||
cpuStealUsage.Set(float64(newCpu.Steal-oldCpu.Steal) / total * 100) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (C) 2024 Christian Rößner | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
//go:build !linux | ||
|
||
package stats | ||
|
||
import ( | ||
"github.com/mackerelio/go-osstat/cpu" | ||
) | ||
|
||
func setNewStats(oldCpu, newCpu *cpu.Stats, total float64) { | ||
cpuUserUsage.Set(float64(newCpu.User-oldCpu.User) / total * 100) | ||
cpuSystemUsage.Set(float64(newCpu.System-oldCpu.System) / total * 100) | ||
cpuIdleUsage.Set(float64(newCpu.Idle-oldCpu.Idle) / total * 100) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters