Skip to content

Commit 285516d

Browse files
authored
Merge pull request #594 from oif/chore/optimize-netcollector-implementation
optimize netcollector implementation and custom `/proc` mount path
2 parents 4412a2b + 11ddb5e commit 285516d

12 files changed

+185
-226
lines changed

pkg/systemstatsmonitor/cpu_collector.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,17 @@ type cpuCollector struct {
4646
mSystemInterruptsTotal *metrics.Int64Metric
4747
mSystemCPUStat *metrics.Float64Metric // per-cpu time from /proc/stats
4848

49-
config *ssmtypes.CPUStatsConfig
49+
config *ssmtypes.CPUStatsConfig
50+
procPath string
5051

5152
lastUsageTime map[string]float64
5253
}
5354

54-
func NewCPUCollectorOrDie(cpuConfig *ssmtypes.CPUStatsConfig) *cpuCollector {
55-
cc := cpuCollector{config: cpuConfig}
55+
func NewCPUCollectorOrDie(cpuConfig *ssmtypes.CPUStatsConfig, procPath string) *cpuCollector {
56+
cc := cpuCollector{
57+
config: cpuConfig,
58+
procPath: procPath,
59+
}
5660

5761
var err error
5862
cc.mRunnableTaskCount, err = metrics.NewFloat64Metric(

pkg/systemstatsmonitor/cpu_collector_linux.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,14 @@ func (cc *cpuCollector) recordLoad() {
5252
}
5353

5454
func (cc *cpuCollector) recordSystemStats() {
55-
5655
// don't collect the load metrics if the configs are not present.
5756
if cc.mSystemCPUStat == nil && cc.mSystemInterruptsTotal == nil &&
5857
cc.mSystemProcessesTotal == nil && cc.mSystemProcsBlocked == nil &&
5958
cc.mSystemProcsRunning == nil {
6059
return
6160
}
6261

63-
fs, err := procfs.NewFS("/proc")
62+
fs, err := procfs.NewFS(cc.procPath)
6463
stats, err := fs.Stat()
6564
if err != nil {
6665
glog.Errorf("Failed to retrieve cpu/process stats: %v", err)

pkg/systemstatsmonitor/cpu_collector_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@ func TestCpuCollector(t *testing.T) {
6767
if err := json.Unmarshal([]byte(fakeCPUConfig), cfg); err != nil {
6868
t.Fatalf("cannot load cpu config: %s", err)
6969
}
70-
mc := NewCPUCollectorOrDie(cfg)
70+
mc := NewCPUCollectorOrDie(cfg, "/proc")
7171
mc.collect()
7272
}

0 commit comments

Comments
 (0)