Skip to content

Commit

Permalink
make cgroup errors skippable
Browse files Browse the repository at this point in the history
  • Loading branch information
fearful-symmetry committed Mar 18, 2024
1 parent 8a02eaa commit 22c2e1c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
2 changes: 1 addition & 1 deletion metric/system/cgroup/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func SupportedSubsystems(rootfs resolve.Resolver) (map[string]struct{}, error) {
// The returned map contains the subsystem name as a key and the value is the
// mountpoint.
func SubsystemMountpoints(rootfs resolve.Resolver, subsystems map[string]struct{}) (Mountpoints, error) {

// TODO: will we run into mount namespace issues if we use /proc/self/mountinfo?
mountinfo, err := os.Open(rootfs.ResolveHostFS("/proc/self/mountinfo"))
if err != nil {
return Mountpoints{}, err
Expand Down
12 changes: 7 additions & 5 deletions metric/system/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,14 @@ func (procStats *Stats) pidFill(pid int, filter bool) (ProcState, bool, error) {
if procStats.EnableCgroups {
cgStats, err := procStats.cgroups.GetStatsForPid(status.Pid.ValueOr(0))
if err != nil {
return status, true, fmt.Errorf("cgroups.GetStatsForPid: %w", err)
}
status.Cgroup = cgStats
if ok {
status.Cgroup.FillPercentages(last.Cgroup, status.SampleTime, last.SampleTime)
procStats.logger.Debugf("Non-fatal error fetching cgroups metrics for pid %d, metrics are valid but partial: %s", pid, err)
} else {
status.Cgroup = cgStats
if ok {
status.Cgroup.FillPercentages(last.Cgroup, status.SampleTime, last.SampleTime)
}
}

} // end cgroups processor

status, err = FillMetricsRequiringMoreAccess(pid, status)
Expand Down
19 changes: 19 additions & 0 deletions metric/system/process/process_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,25 @@ func TestParseProcStat(t *testing.T) {
assert.Equal(t, want, got, "")
}

func TestCgroupsBadCgroupsConfig(t *testing.T) {
_ = logp.DevelopmentSetup()
testStats := Stats{CPUTicks: true,
EnableCgroups: true,
EnableNetwork: true,
Hostfs: resolve.NewTestResolver("/"),
Procs: []string{".*"},
CgroupOpts: cgroup.ReaderOptions{RootfsMountpoint: resolve.NewTestResolver("testdata")}, // procs here have no cgroup data, leading to errors
}
err := testStats.Init()
require.NoError(t, err)

// make sure we still have proc data despite cgroups errors
procs, _, err := testStats.Get()
require.NoError(t, err)
t.Logf("got %d procs", len(procs))
require.NotEmpty(t, procs)
}

func TestParseIO(t *testing.T) {
path := resolve.NewTestResolver("testdata/")
data, err := getIOData(path, 42)
Expand Down
15 changes: 15 additions & 0 deletions metric/system/process/testdata/proc/cgroups
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#subsys_name hierarchy num_cgroups enabled
cpuset 0 176 1
cpu 0 176 1
cpuacct 0 176 1
blkio 0 176 1
memory 0 176 1
devices 0 176 1
freezer 0 176 1
net_cls 0 176 1
perf_event 0 176 1
net_prio 0 176 1
hugetlb 0 176 1
pids 0 176 1
rdma 0 176 1
misc 0 176 1
1 change: 1 addition & 0 deletions metric/system/process/testdata/proc/self/mountinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
29 23 0:26 / /sys/fs/cgroup rw,nosuid,nodev,noexec,relatime shared:4 - cgroup2 cgroup2 rw,seclabel,nsdelegate,memory_recursiveprot

0 comments on commit 22c2e1c

Please sign in to comment.