collector/cpu: split cpu freq metrics into separate collector (#1253)
The cpu frequency information is not always needed and/or available. This change allows the cpu frequency metrics to be enabled/disabled separately from the other cpu metrics, and also prevents a frequency metric failure (such as a parse error) from failing the main cpu collector. Fixes #1241 Signed-off-by: Paul Gier <pgier@redhat.com>
This commit is contained in:
parent
f028b81615
commit
cc847f2f44
9 changed files with 241 additions and 175 deletions
|
|
@ -17,20 +17,17 @@
|
|||
package collector
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/siebenmann/go-kstat"
|
||||
kstat "github.com/siebenmann/go-kstat"
|
||||
)
|
||||
|
||||
// #include <unistd.h>
|
||||
import "C"
|
||||
|
||||
type cpuCollector struct {
|
||||
cpu typedDesc
|
||||
cpuFreq *prometheus.Desc
|
||||
cpuFreqMax *prometheus.Desc
|
||||
cpu typedDesc
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
|
@ -40,30 +37,10 @@ func init() {
|
|||
func NewCpuCollector() (Collector, error) {
|
||||
return &cpuCollector{
|
||||
cpu: typedDesc{nodeCPUSecondsDesc, prometheus.CounterValue},
|
||||
cpuFreq: prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "frequency_hertz"),
|
||||
"Current cpu thread frequency in hertz.",
|
||||
[]string{"cpu"}, nil,
|
||||
),
|
||||
cpuFreqMax: prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "frequency_max_hertz"),
|
||||
"Maximum cpu thread frequency in hertz.",
|
||||
[]string{"cpu"}, nil,
|
||||
),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *cpuCollector) Update(ch chan<- prometheus.Metric) error {
|
||||
if err := c.updateCPUstats(ch); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.updateCPUfreq(ch); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *cpuCollector) updateCPUstats(ch chan<- prometheus.Metric) error {
|
||||
ncpus := C.sysconf(C._SC_NPROCESSORS_ONLN)
|
||||
|
||||
tok, err := kstat.Open()
|
||||
|
|
@ -95,46 +72,3 @@ func (c *cpuCollector) updateCPUstats(ch chan<- prometheus.Metric) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *cpuCollector) updateCPUfreq(ch chan<- prometheus.Metric) error {
|
||||
ncpus := C.sysconf(C._SC_NPROCESSORS_ONLN)
|
||||
|
||||
tok, err := kstat.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer tok.Close()
|
||||
|
||||
for cpu := 0; cpu < int(ncpus); cpu++ {
|
||||
ksCPUInfo, err := tok.Lookup("cpu_info", cpu, fmt.Sprintf("cpu_info%d", cpu))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cpuFreqV, err := ksCPUInfo.GetNamed("current_clock_Hz")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cpuFreqMaxV, err := ksCPUInfo.GetNamed("clock_MHz")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
lcpu := strconv.Itoa(cpu)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.cpuFreq,
|
||||
prometheus.GaugeValue,
|
||||
float64(cpuFreqV.UintVal),
|
||||
lcpu,
|
||||
)
|
||||
// Multiply by 1e+6 to convert MHz to Hz.
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.cpuFreqMax,
|
||||
prometheus.GaugeValue,
|
||||
float64(cpuFreqMaxV.IntVal)*1e+6,
|
||||
lcpu,
|
||||
)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue