Skip to content

Commit 56c592a

Browse files
authored
Merge pull request #587 from vteratipally/bug_fix
Add a check if the metric is nil so that collector doesn't collect metrics.
2 parents 1123fd2 + ebdd903 commit 56c592a

File tree

1 file changed

+65
-35
lines changed

1 file changed

+65
-35
lines changed

pkg/systemstatsmonitor/cpu_collector_linux.go

Lines changed: 65 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import (
2525
)
2626

2727
func (cc *cpuCollector) recordLoad() {
28-
if cc.mRunnableTaskCount == nil {
28+
// don't collect the load metrics if the configs are not present.
29+
if cc.mRunnableTaskCount == nil &&
30+
cc.mCpuLoad15m == nil && cc.mCpuLoad1m == nil && cc.mCpuLoad5m == nil {
2931
return
3032
}
3133

@@ -35,49 +37,77 @@ func (cc *cpuCollector) recordLoad() {
3537
return
3638
}
3739

38-
cc.mRunnableTaskCount.Record(map[string]string{}, loadAvg.Load1)
39-
40-
cc.mCpuLoad1m.Record(map[string]string{}, loadAvg.Load1)
41-
cc.mCpuLoad5m.Record(map[string]string{}, loadAvg.Load5)
42-
cc.mCpuLoad15m.Record(map[string]string{}, loadAvg.Load15)
40+
if cc.mRunnableTaskCount != nil {
41+
cc.mRunnableTaskCount.Record(map[string]string{}, loadAvg.Load1)
42+
}
43+
if cc.mCpuLoad1m != nil {
44+
cc.mCpuLoad1m.Record(map[string]string{}, loadAvg.Load1)
45+
}
46+
if cc.mCpuLoad5m != nil {
47+
cc.mCpuLoad5m.Record(map[string]string{}, loadAvg.Load5)
48+
}
49+
if cc.mCpuLoad15m != nil {
50+
cc.mCpuLoad15m.Record(map[string]string{}, loadAvg.Load15)
51+
}
4352
}
4453

4554
func (cc *cpuCollector) recordSystemStats() {
55+
56+
// don't collect the load metrics if the configs are not present.
57+
if cc.mSystemCPUStat == nil && cc.mSystemInterruptsTotal == nil &&
58+
cc.mSystemProcessesTotal == nil && cc.mSystemProcsBlocked == nil &&
59+
cc.mSystemProcsRunning == nil {
60+
return
61+
}
62+
4663
fs, err := procfs.NewFS("/proc")
4764
stats, err := fs.Stat()
4865
if err != nil {
4966
glog.Errorf("Failed to retrieve cpu/process stats: %v", err)
5067
return
5168
}
5269

53-
cc.mSystemProcessesTotal.Record(map[string]string{}, int64(stats.ProcessCreated))
54-
cc.mSystemProcsRunning.Record(map[string]string{}, int64(stats.ProcessesRunning))
55-
cc.mSystemProcsBlocked.Record(map[string]string{}, int64(stats.ProcessesBlocked))
56-
cc.mSystemInterruptsTotal.Record(map[string]string{}, int64(stats.IRQTotal))
57-
58-
for i, c := range stats.CPU {
59-
tags := map[string]string{}
60-
tags[cpuLabel] = fmt.Sprintf("cpu%d", i)
61-
62-
tags[stageLabel] = "user"
63-
cc.mSystemCPUStat.Record(tags, c.User)
64-
tags[stageLabel] = "nice"
65-
cc.mSystemCPUStat.Record(tags, c.Nice)
66-
tags[stageLabel] = "system"
67-
cc.mSystemCPUStat.Record(tags, c.System)
68-
tags[stageLabel] = "idle"
69-
cc.mSystemCPUStat.Record(tags, c.Idle)
70-
tags[stageLabel] = "iowait"
71-
cc.mSystemCPUStat.Record(tags, c.Iowait)
72-
tags[stageLabel] = "iRQ"
73-
cc.mSystemCPUStat.Record(tags, c.IRQ)
74-
tags[stageLabel] = "softIRQ"
75-
cc.mSystemCPUStat.Record(tags, c.SoftIRQ)
76-
tags[stageLabel] = "steal"
77-
cc.mSystemCPUStat.Record(tags, c.Steal)
78-
tags[stageLabel] = "guest"
79-
cc.mSystemCPUStat.Record(tags, c.Guest)
80-
tags[stageLabel] = "guestNice"
81-
cc.mSystemCPUStat.Record(tags, c.GuestNice)
70+
if cc.mSystemProcessesTotal != nil {
71+
cc.mSystemProcessesTotal.Record(map[string]string{}, int64(stats.ProcessCreated))
72+
}
73+
74+
if cc.mSystemProcsRunning != nil {
75+
cc.mSystemProcsRunning.Record(map[string]string{}, int64(stats.ProcessesRunning))
76+
}
77+
78+
if cc.mSystemProcsBlocked != nil {
79+
cc.mSystemProcsBlocked.Record(map[string]string{}, int64(stats.ProcessesBlocked))
80+
}
81+
82+
if cc.mSystemInterruptsTotal != nil {
83+
cc.mSystemInterruptsTotal.Record(map[string]string{}, int64(stats.IRQTotal))
84+
}
85+
86+
if cc.mSystemCPUStat != nil {
87+
for i, c := range stats.CPU {
88+
tags := map[string]string{}
89+
tags[cpuLabel] = fmt.Sprintf("cpu%d", i)
90+
91+
tags[stageLabel] = "user"
92+
cc.mSystemCPUStat.Record(tags, c.User)
93+
tags[stageLabel] = "nice"
94+
cc.mSystemCPUStat.Record(tags, c.Nice)
95+
tags[stageLabel] = "system"
96+
cc.mSystemCPUStat.Record(tags, c.System)
97+
tags[stageLabel] = "idle"
98+
cc.mSystemCPUStat.Record(tags, c.Idle)
99+
tags[stageLabel] = "iowait"
100+
cc.mSystemCPUStat.Record(tags, c.Iowait)
101+
tags[stageLabel] = "iRQ"
102+
cc.mSystemCPUStat.Record(tags, c.IRQ)
103+
tags[stageLabel] = "softIRQ"
104+
cc.mSystemCPUStat.Record(tags, c.SoftIRQ)
105+
tags[stageLabel] = "steal"
106+
cc.mSystemCPUStat.Record(tags, c.Steal)
107+
tags[stageLabel] = "guest"
108+
cc.mSystemCPUStat.Record(tags, c.Guest)
109+
tags[stageLabel] = "guestNice"
110+
cc.mSystemCPUStat.Record(tags, c.GuestNice)
111+
}
82112
}
83113
}

0 commit comments

Comments
 (0)