mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-03-05 01:56:35 +00:00
cpu: add workaround for counter resets related to % Processor Utility metric (#1637)
This commit is contained in:
@@ -27,6 +27,9 @@ type Collector struct {
|
|||||||
|
|
||||||
perfDataCollector *perfdata.Collector
|
perfDataCollector *perfdata.Collector
|
||||||
|
|
||||||
|
processorRTCValues map[string]cpuCounter
|
||||||
|
processorMPerfValues map[string]cpuCounter
|
||||||
|
|
||||||
logicalProcessors *prometheus.Desc
|
logicalProcessors *prometheus.Desc
|
||||||
cStateSecondsTotal *prometheus.Desc
|
cStateSecondsTotal *prometheus.Desc
|
||||||
timeTotal *prometheus.Desc
|
timeTotal *prometheus.Desc
|
||||||
@@ -43,6 +46,11 @@ type Collector struct {
|
|||||||
processorPrivilegedUtility *prometheus.Desc
|
processorPrivilegedUtility *prometheus.Desc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type cpuCounter struct {
|
||||||
|
lastValue uint32
|
||||||
|
totalValue float64
|
||||||
|
}
|
||||||
|
|
||||||
func New(config *Config) *Collector {
|
func New(config *Config) *Collector {
|
||||||
if config == nil {
|
if config == nil {
|
||||||
config = &ConfigDefaults
|
config = &ConfigDefaults
|
||||||
@@ -221,6 +229,9 @@ func (c *Collector) Build(_ *slog.Logger, _ *wmi.Client) error {
|
|||||||
nil,
|
nil,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
c.processorRTCValues = map[string]cpuCounter{}
|
||||||
|
c.processorMPerfValues = map[string]cpuCounter{}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -251,6 +262,30 @@ func (c *Collector) collectFull(ctx *types.ScrapeContext, logger *slog.Logger, c
|
|||||||
|
|
||||||
core := cpu.Name
|
core := cpu.Name
|
||||||
|
|
||||||
|
if val, ok := c.processorRTCValues[core]; ok {
|
||||||
|
c.processorRTCValues[core] = cpuCounter{
|
||||||
|
uint32(cpu.ProcessorRTC),
|
||||||
|
val.totalValue + float64(uint32(cpu.ProcessorRTC)-val.lastValue),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
c.processorRTCValues[core] = cpuCounter{
|
||||||
|
uint32(cpu.ProcessorRTC),
|
||||||
|
0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if val, ok := c.processorMPerfValues[core]; ok {
|
||||||
|
c.processorMPerfValues[core] = cpuCounter{
|
||||||
|
uint32(cpu.ProcessorMPerf),
|
||||||
|
val.totalValue + float64(uint32(cpu.ProcessorMPerf)-val.lastValue),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
c.processorMPerfValues[core] = cpuCounter{
|
||||||
|
uint32(cpu.ProcessorMPerf),
|
||||||
|
0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
coreCount++
|
coreCount++
|
||||||
|
|
||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
@@ -350,13 +385,13 @@ func (c *Collector) collectFull(ctx *types.ScrapeContext, logger *slog.Logger, c
|
|||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
c.processorMPerf,
|
c.processorMPerf,
|
||||||
prometheus.CounterValue,
|
prometheus.CounterValue,
|
||||||
cpu.ProcessorMPerf,
|
c.processorMPerfValues[core].totalValue,
|
||||||
core,
|
core,
|
||||||
)
|
)
|
||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
c.processorRTC,
|
c.processorRTC,
|
||||||
prometheus.CounterValue,
|
prometheus.CounterValue,
|
||||||
cpu.ProcessorRTC,
|
c.processorRTCValues[core].totalValue,
|
||||||
core,
|
core,
|
||||||
)
|
)
|
||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
@@ -393,6 +428,30 @@ func (c *Collector) collectPDH(ch chan<- prometheus.Metric) error {
|
|||||||
for core, coreData := range data {
|
for core, coreData := range data {
|
||||||
coreCount++
|
coreCount++
|
||||||
|
|
||||||
|
if val, ok := c.processorRTCValues[core]; ok {
|
||||||
|
c.processorRTCValues[core] = cpuCounter{
|
||||||
|
uint32(coreData[privilegedUtilitySeconds].SecondValue),
|
||||||
|
val.totalValue + float64(uint32(coreData[privilegedUtilitySeconds].SecondValue)-val.lastValue),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
c.processorRTCValues[core] = cpuCounter{
|
||||||
|
uint32(coreData[privilegedUtilitySeconds].SecondValue),
|
||||||
|
0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if val, ok := c.processorMPerfValues[core]; ok {
|
||||||
|
c.processorMPerfValues[core] = cpuCounter{
|
||||||
|
uint32(coreData[processorPerformance].SecondValue),
|
||||||
|
val.totalValue + float64(uint32(coreData[processorPerformance].SecondValue)-val.lastValue),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
c.processorMPerfValues[core] = cpuCounter{
|
||||||
|
uint32(coreData[processorPerformance].SecondValue),
|
||||||
|
0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
c.cStateSecondsTotal,
|
c.cStateSecondsTotal,
|
||||||
prometheus.CounterValue,
|
prometheus.CounterValue,
|
||||||
|
|||||||
Reference in New Issue
Block a user