mirror of
https://github.com/datarhei/core.git
synced 2025-12-24 13:07:56 +08:00
Don't directly access psutils
This commit is contained in:
@@ -2,7 +2,6 @@ package monitor
|
||||
|
||||
import (
|
||||
"github.com/datarhei/core/v16/monitor/metric"
|
||||
"github.com/datarhei/core/v16/psutil"
|
||||
"github.com/datarhei/core/v16/resources"
|
||||
)
|
||||
|
||||
@@ -15,13 +14,11 @@ type cpuCollector struct {
|
||||
limitDescr *metric.Description
|
||||
throttleDescr *metric.Description
|
||||
|
||||
ncpu float64
|
||||
resources resources.Resources
|
||||
}
|
||||
|
||||
func NewCPUCollector(rsc resources.Resources) metric.Collector {
|
||||
c := &cpuCollector{
|
||||
ncpu: 1,
|
||||
resources: rsc,
|
||||
}
|
||||
|
||||
@@ -33,10 +30,6 @@ func NewCPUCollector(rsc resources.Resources) metric.Collector {
|
||||
c.limitDescr = metric.NewDesc("cpu_limit", "Percentage of CPU to be consumed", nil)
|
||||
c.throttleDescr = metric.NewDesc("cpu_throttling", "Whether the CPU is currently throttled", nil)
|
||||
|
||||
if ncpu, err := psutil.CPUCounts(); err == nil {
|
||||
c.ncpu = ncpu
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -61,29 +54,23 @@ func (c *cpuCollector) Describe() []*metric.Description {
|
||||
func (c *cpuCollector) Collect() metric.Metrics {
|
||||
metrics := metric.NewMetrics()
|
||||
|
||||
metrics.Add(metric.NewValue(c.ncpuDescr, c.ncpu))
|
||||
rinfo := c.resources.Info()
|
||||
|
||||
limit, _, _, _ := c.resources.Limits()
|
||||
metrics.Add(metric.NewValue(c.ncpuDescr, rinfo.CPU.NCPU))
|
||||
|
||||
metrics.Add(metric.NewValue(c.limitDescr, limit))
|
||||
metrics.Add(metric.NewValue(c.limitDescr, rinfo.CPU.Limit))
|
||||
|
||||
cpu, _, _ := c.resources.ShouldLimit()
|
||||
throttling := .0
|
||||
if cpu {
|
||||
if rinfo.CPU.Throttling {
|
||||
throttling = 1
|
||||
}
|
||||
|
||||
metrics.Add(metric.NewValue(c.throttleDescr, throttling))
|
||||
|
||||
stat, err := psutil.CPUPercent()
|
||||
if err != nil {
|
||||
return metrics
|
||||
}
|
||||
|
||||
metrics.Add(metric.NewValue(c.systemDescr, stat.System))
|
||||
metrics.Add(metric.NewValue(c.userDescr, stat.User))
|
||||
metrics.Add(metric.NewValue(c.idleDescr, stat.Idle))
|
||||
metrics.Add(metric.NewValue(c.otherDescr, stat.Other))
|
||||
metrics.Add(metric.NewValue(c.systemDescr, rinfo.CPU.System))
|
||||
metrics.Add(metric.NewValue(c.userDescr, rinfo.CPU.User))
|
||||
metrics.Add(metric.NewValue(c.idleDescr, rinfo.CPU.Idle))
|
||||
metrics.Add(metric.NewValue(c.otherDescr, rinfo.CPU.Other))
|
||||
|
||||
return metrics
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package monitor
|
||||
|
||||
import (
|
||||
"github.com/datarhei/core/v16/monitor/metric"
|
||||
"github.com/datarhei/core/v16/psutil"
|
||||
"github.com/datarhei/core/v16/resources"
|
||||
)
|
||||
|
||||
@@ -44,25 +43,19 @@ func (c *memCollector) Describe() []*metric.Description {
|
||||
func (c *memCollector) Collect() metric.Metrics {
|
||||
metrics := metric.NewMetrics()
|
||||
|
||||
_, limit, _, _ := c.resources.Limits()
|
||||
rinfo := c.resources.Info()
|
||||
|
||||
metrics.Add(metric.NewValue(c.limitDescr, float64(limit)))
|
||||
metrics.Add(metric.NewValue(c.limitDescr, float64(rinfo.Mem.Limit)))
|
||||
|
||||
_, memory, _ := c.resources.ShouldLimit()
|
||||
throttling := .0
|
||||
if memory {
|
||||
if rinfo.Mem.Throttling {
|
||||
throttling = 1
|
||||
}
|
||||
|
||||
metrics.Add(metric.NewValue(c.throttleDescr, throttling))
|
||||
|
||||
stat, err := psutil.Memory()
|
||||
if err != nil {
|
||||
return metrics
|
||||
}
|
||||
|
||||
metrics.Add(metric.NewValue(c.totalDescr, float64(stat.Total)))
|
||||
metrics.Add(metric.NewValue(c.freeDescr, float64(stat.Available)))
|
||||
metrics.Add(metric.NewValue(c.totalDescr, float64(rinfo.Mem.Total)))
|
||||
metrics.Add(metric.NewValue(c.freeDescr, float64(rinfo.Mem.Available)))
|
||||
|
||||
return metrics
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user