Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cpu: add workaround for counter resets related to % Processor Utility metric #1637

Merged
merged 5 commits into from
Oct 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 61 additions & 2 deletions pkg/collector/cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type Collector struct {

perfDataCollector *perfdata.Collector

processorRTCValues map[string]cpuCounter
processorMPerfValues map[string]cpuCounter

logicalProcessors *prometheus.Desc
cStateSecondsTotal *prometheus.Desc
timeTotal *prometheus.Desc
Expand All @@ -43,6 +46,11 @@ type Collector struct {
processorPrivilegedUtility *prometheus.Desc
}

type cpuCounter struct {
lastValue uint32
totalValue float64
}

func New(config *Config) *Collector {
if config == nil {
config = &ConfigDefaults
Expand Down Expand Up @@ -221,6 +229,9 @@ func (c *Collector) Build(_ *slog.Logger, _ *wmi.Client) error {
nil,
)

c.processorRTCValues = map[string]cpuCounter{}
c.processorMPerfValues = map[string]cpuCounter{}

return nil
}

Expand Down Expand Up @@ -251,6 +262,30 @@ func (c *Collector) collectFull(ctx *types.ScrapeContext, logger *slog.Logger, c

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++

ch <- prometheus.MustNewConstMetric(
Expand Down Expand Up @@ -350,13 +385,13 @@ func (c *Collector) collectFull(ctx *types.ScrapeContext, logger *slog.Logger, c
ch <- prometheus.MustNewConstMetric(
c.processorMPerf,
prometheus.CounterValue,
cpu.ProcessorMPerf,
c.processorMPerfValues[core].totalValue,
core,
)
ch <- prometheus.MustNewConstMetric(
c.processorRTC,
prometheus.CounterValue,
cpu.ProcessorRTC,
c.processorRTCValues[core].totalValue,
core,
)
ch <- prometheus.MustNewConstMetric(
Expand Down Expand Up @@ -393,6 +428,30 @@ func (c *Collector) collectPDH(ch chan<- prometheus.Metric) error {
for core, coreData := range data {
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(
c.cStateSecondsTotal,
prometheus.CounterValue,
Expand Down