Skip to content

Commit 7fe1ae6

Browse files
authored
Avoid null references during what appears to be race condition (#21)
1 parent 9f99aa2 commit 7fe1ae6

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Needs to be defined before including Makefile.common to auto-generate targets
22
DOCKER_ARCHS ?= amd64 arm64 ppc64le
33
DOCKER_REPO ?= treydock
4-
GOLANGCI_LINT_VERSION ?= v1.44.2
4+
GOLANGCI_LINT_VERSION ?= v1.50.1
55

66
include Makefile.common
77

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
![GitHub All Releases](https://img.shields.io/github/downloads/treydock/cgroup_exporter/total)
66
[![codecov](https://codecov.io/gh/treydock/cgroup_exporter/branch/master/graph/badge.svg)](https://codecov.io/gh/treydock/cgroup_exporter)
77

8-
# Check mount Prometheus exporter
8+
# cgroup Prometheus exporter
99

1010
The `cgroup_exporter` produces metrics from cgroups.
1111

cgroup_exporter.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -328,17 +328,27 @@ func (e *Exporter) getMetrics(name string, pids map[string][]int) (CgroupMetric,
328328
return metric, err
329329
}
330330
stats, _ := ctrl.Stat(cgroups.IgnoreNotExist)
331-
metric.cpuUser = float64(stats.CPU.Usage.User) / 1000000000.0
332-
metric.cpuSystem = float64(stats.CPU.Usage.Kernel) / 1000000000.0
333-
metric.cpuTotal = float64(stats.CPU.Usage.Total) / 1000000000.0
334-
metric.memoryRSS = float64(stats.Memory.TotalRSS)
335-
metric.memoryCache = float64(stats.Memory.TotalCache)
336-
metric.memoryUsed = float64(stats.Memory.Usage.Usage)
337-
metric.memoryTotal = float64(stats.Memory.Usage.Limit)
338-
metric.memoryFailCount = float64(stats.Memory.Usage.Failcnt)
339-
metric.memswUsed = float64(stats.Memory.Swap.Usage)
340-
metric.memswTotal = float64(stats.Memory.Swap.Limit)
341-
metric.memswFailCount = float64(stats.Memory.Swap.Failcnt)
331+
if stats.CPU != nil {
332+
if stats.CPU.Usage != nil {
333+
metric.cpuUser = float64(stats.CPU.Usage.User) / 1000000000.0
334+
metric.cpuSystem = float64(stats.CPU.Usage.Kernel) / 1000000000.0
335+
metric.cpuTotal = float64(stats.CPU.Usage.Total) / 1000000000.0
336+
}
337+
}
338+
if stats.Memory != nil {
339+
metric.memoryRSS = float64(stats.Memory.TotalRSS)
340+
metric.memoryCache = float64(stats.Memory.TotalCache)
341+
if stats.Memory.Usage != nil {
342+
metric.memoryUsed = float64(stats.Memory.Usage.Usage)
343+
metric.memoryTotal = float64(stats.Memory.Usage.Limit)
344+
metric.memoryFailCount = float64(stats.Memory.Usage.Failcnt)
345+
}
346+
if stats.Memory.Swap != nil {
347+
metric.memswUsed = float64(stats.Memory.Swap.Usage)
348+
metric.memswTotal = float64(stats.Memory.Swap.Limit)
349+
metric.memswFailCount = float64(stats.Memory.Swap.Failcnt)
350+
}
351+
}
342352
if cpus, err := getCPUs(name, e.logger); err == nil {
343353
metric.cpus = len(cpus)
344354
metric.cpu_list = strings.Join(cpus, ",")

0 commit comments

Comments
 (0)