-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Configurable cgroup ID regex for process discovery component (#…
…1557) * feat: Configurable cgroup ID regex for process discovery component * Add relevant unit tests and update docs Signed-off-by: Mahendra Paipuri <[email protected]> * docs: Address PR comments Co-authored-by: Clayton Cornell <[email protected]> * docs: Improve docs based on PR comments * refactor: Update regexp during args update Signed-off-by: Mahendra Paipuri <[email protected]> * test: Add a unit test to verify cgroup regex updating Signed-off-by: Mahendra Paipuri <[email protected]> * test: Add missing import Signed-off-by: Mahendra Paipuri <[email protected]> * refactor: Export cgroup paths in targets * This is a simplified approach to the original idea. Here we export cgroup paths as one of the labels and users can use relabel component to retrieve the relevant cgroup IDs. * In the case of cgroups v1, we export all the controllers paths delimited by `|` where as in cgroups v2, there is always one path Signed-off-by: Mahendra Paipuri <[email protected]> * docs: Add an example on how to use cgroup path Disable cgroup_path meta data by default * docs: Correct default value of `cgroup_path` in docs Co-authored-by: Christian Simon <[email protected]> * Update changelog * docs: Update based on review comment Co-authored-by: Clayton Cornell <[email protected]> --------- Signed-off-by: Mahendra Paipuri <[email protected]> Co-authored-by: Clayton Cornell <[email protected]> Co-authored-by: Christian Simon <[email protected]>
- Loading branch information
1 parent
2b7003d
commit cbc8723
Showing
7 changed files
with
153 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//go:build linux | ||
|
||
package process | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"strings" | ||
) | ||
|
||
// getPathFromCGroup fetches cgroup path(s) from process. | ||
// In the case of cgroups v2 (unified), there will be only | ||
// one path and function returns that path. In the case | ||
// cgroups v1, there will be one path for each controller. | ||
// The function will join all the paths using `|` and | ||
// returns as one string. Users can use relabel component | ||
// to retrieve the path that they are interested. | ||
func getPathFromCGroup(cgroup io.Reader) string { | ||
var paths []string | ||
scanner := bufio.NewScanner(cgroup) | ||
for scanner.Scan() { | ||
line := scanner.Bytes() | ||
paths = append(paths, string(line)) | ||
} | ||
return strings.Join(paths, "|") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//go:build linux | ||
|
||
package process | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGenericCGroupMatching(t *testing.T) { | ||
type testcase = struct { | ||
name, cgroup, expectedPath string | ||
} | ||
testcases := []testcase{ | ||
{ | ||
name: "cgroups v2", | ||
cgroup: `0::/system.slice/slurmstepd.scope/job_1446354/step_batch/user/task_0`, // cgroups v2 | ||
expectedPath: `0::/system.slice/slurmstepd.scope/job_1446354/step_batch/user/task_0`, | ||
}, | ||
{ | ||
name: "cgroups v1", | ||
cgroup: `12:rdma:/ | ||
11:devices:/machine/qemu-1-instance-00000025.libvirt-qemu/emulator | ||
10:cpuset:/machine/qemu-1-instance-00000025.libvirt-qemu/emulator | ||
9:blkio:/machine/qemu-1-instance-00000025.libvirt-qemu/emulator | ||
8:pids:/user.slice/user-118.slice/session-5.scope | ||
7:memory:/machine/qemu-1-instance-00000025.libvirt-qemu/emulator | ||
6:hugetlb:/ | ||
5:net_cls,net_prio:/ | ||
4:perf_event:/ | ||
3:cpu,cpuacct:/machine/qemu-1-instance-00000025.libvirt-qemu/emulator | ||
2:freezer:/machine/qemu-1-instance-00000025.libvirt-qemu/emulator | ||
1:name=systemd:/user.slice/user-118.slice/session-5.scope`, // cgroups v1 | ||
expectedPath: "12:rdma:/|11:devices:/machine/qemu-1-instance-00000025.libvirt-qemu/emulator|10:cpuset:/machine/qemu-1-instance-00000025.libvirt-qemu/emulator|9:blkio:/machine/qemu-1-instance-00000025.libvirt-qemu/emulator|8:pids:/user.slice/user-118.slice/session-5.scope|7:memory:/machine/qemu-1-instance-00000025.libvirt-qemu/emulator|6:hugetlb:/|5:net_cls,net_prio:/|4:perf_event:/|3:cpu,cpuacct:/machine/qemu-1-instance-00000025.libvirt-qemu/emulator|2:freezer:/machine/qemu-1-instance-00000025.libvirt-qemu/emulator|1:name=systemd:/user.slice/user-118.slice/session-5.scope", | ||
}, | ||
{ | ||
name: "empty cgroups path", // Should not happen in real cases | ||
cgroup: "", | ||
expectedPath: "", | ||
}, | ||
} | ||
for i, tc := range testcases { | ||
t.Run(fmt.Sprintf("testcase %d %s", i, tc.name), func(t *testing.T) { | ||
cgroupID := getPathFromCGroup(bytes.NewReader([]byte(tc.cgroup))) | ||
expected := tc.expectedPath | ||
require.Equal(t, expected, cgroupID) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters