Skip to content

Commit

Permalink
Don't directly access psutils
Browse files Browse the repository at this point in the history
  • Loading branch information
ioppermann committed Oct 28, 2024
1 parent e2def57 commit 175fb1a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 33 deletions.
29 changes: 8 additions & 21 deletions monitor/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
}

Expand All @@ -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
}

Expand All @@ -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
}
17 changes: 5 additions & 12 deletions monitor/mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 175fb1a

Please sign in to comment.