Skip to content

Commit

Permalink
Merge pull request #587 from vteratipally/bug_fix
Browse files Browse the repository at this point in the history
Add a check if the metric is nil so that collector doesn't collect metrics.
  • Loading branch information
k8s-ci-robot authored Aug 31, 2021
2 parents 1123fd2 + ebdd903 commit 56c592a
Showing 1 changed file with 65 additions and 35 deletions.
100 changes: 65 additions & 35 deletions pkg/systemstatsmonitor/cpu_collector_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import (
)

func (cc *cpuCollector) recordLoad() {
if cc.mRunnableTaskCount == nil {
// don't collect the load metrics if the configs are not present.
if cc.mRunnableTaskCount == nil &&
cc.mCpuLoad15m == nil && cc.mCpuLoad1m == nil && cc.mCpuLoad5m == nil {
return
}

Expand All @@ -35,49 +37,77 @@ func (cc *cpuCollector) recordLoad() {
return
}

cc.mRunnableTaskCount.Record(map[string]string{}, loadAvg.Load1)

cc.mCpuLoad1m.Record(map[string]string{}, loadAvg.Load1)
cc.mCpuLoad5m.Record(map[string]string{}, loadAvg.Load5)
cc.mCpuLoad15m.Record(map[string]string{}, loadAvg.Load15)
if cc.mRunnableTaskCount != nil {
cc.mRunnableTaskCount.Record(map[string]string{}, loadAvg.Load1)
}
if cc.mCpuLoad1m != nil {
cc.mCpuLoad1m.Record(map[string]string{}, loadAvg.Load1)
}
if cc.mCpuLoad5m != nil {
cc.mCpuLoad5m.Record(map[string]string{}, loadAvg.Load5)
}
if cc.mCpuLoad15m != nil {
cc.mCpuLoad15m.Record(map[string]string{}, loadAvg.Load15)
}
}

func (cc *cpuCollector) recordSystemStats() {

// don't collect the load metrics if the configs are not present.
if cc.mSystemCPUStat == nil && cc.mSystemInterruptsTotal == nil &&
cc.mSystemProcessesTotal == nil && cc.mSystemProcsBlocked == nil &&
cc.mSystemProcsRunning == nil {
return
}

fs, err := procfs.NewFS("/proc")
stats, err := fs.Stat()
if err != nil {
glog.Errorf("Failed to retrieve cpu/process stats: %v", err)
return
}

cc.mSystemProcessesTotal.Record(map[string]string{}, int64(stats.ProcessCreated))
cc.mSystemProcsRunning.Record(map[string]string{}, int64(stats.ProcessesRunning))
cc.mSystemProcsBlocked.Record(map[string]string{}, int64(stats.ProcessesBlocked))
cc.mSystemInterruptsTotal.Record(map[string]string{}, int64(stats.IRQTotal))

for i, c := range stats.CPU {
tags := map[string]string{}
tags[cpuLabel] = fmt.Sprintf("cpu%d", i)

tags[stageLabel] = "user"
cc.mSystemCPUStat.Record(tags, c.User)
tags[stageLabel] = "nice"
cc.mSystemCPUStat.Record(tags, c.Nice)
tags[stageLabel] = "system"
cc.mSystemCPUStat.Record(tags, c.System)
tags[stageLabel] = "idle"
cc.mSystemCPUStat.Record(tags, c.Idle)
tags[stageLabel] = "iowait"
cc.mSystemCPUStat.Record(tags, c.Iowait)
tags[stageLabel] = "iRQ"
cc.mSystemCPUStat.Record(tags, c.IRQ)
tags[stageLabel] = "softIRQ"
cc.mSystemCPUStat.Record(tags, c.SoftIRQ)
tags[stageLabel] = "steal"
cc.mSystemCPUStat.Record(tags, c.Steal)
tags[stageLabel] = "guest"
cc.mSystemCPUStat.Record(tags, c.Guest)
tags[stageLabel] = "guestNice"
cc.mSystemCPUStat.Record(tags, c.GuestNice)
if cc.mSystemProcessesTotal != nil {
cc.mSystemProcessesTotal.Record(map[string]string{}, int64(stats.ProcessCreated))
}

if cc.mSystemProcsRunning != nil {
cc.mSystemProcsRunning.Record(map[string]string{}, int64(stats.ProcessesRunning))
}

if cc.mSystemProcsBlocked != nil {
cc.mSystemProcsBlocked.Record(map[string]string{}, int64(stats.ProcessesBlocked))
}

if cc.mSystemInterruptsTotal != nil {
cc.mSystemInterruptsTotal.Record(map[string]string{}, int64(stats.IRQTotal))
}

if cc.mSystemCPUStat != nil {
for i, c := range stats.CPU {
tags := map[string]string{}
tags[cpuLabel] = fmt.Sprintf("cpu%d", i)

tags[stageLabel] = "user"
cc.mSystemCPUStat.Record(tags, c.User)
tags[stageLabel] = "nice"
cc.mSystemCPUStat.Record(tags, c.Nice)
tags[stageLabel] = "system"
cc.mSystemCPUStat.Record(tags, c.System)
tags[stageLabel] = "idle"
cc.mSystemCPUStat.Record(tags, c.Idle)
tags[stageLabel] = "iowait"
cc.mSystemCPUStat.Record(tags, c.Iowait)
tags[stageLabel] = "iRQ"
cc.mSystemCPUStat.Record(tags, c.IRQ)
tags[stageLabel] = "softIRQ"
cc.mSystemCPUStat.Record(tags, c.SoftIRQ)
tags[stageLabel] = "steal"
cc.mSystemCPUStat.Record(tags, c.Steal)
tags[stageLabel] = "guest"
cc.mSystemCPUStat.Record(tags, c.Guest)
tags[stageLabel] = "guestNice"
cc.mSystemCPUStat.Record(tags, c.GuestNice)
}
}
}

0 comments on commit 56c592a

Please sign in to comment.