From 9b5e1e0d02a4d8ab9347e4a1f48d945a9dfdf4c2 Mon Sep 17 00:00:00 2001 From: AliDatadog Date: Thu, 11 Jan 2024 14:32:38 +0100 Subject: [PATCH 1/4] lint --- src/main/java/com/timgroup/statsd/CgroupReader.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/timgroup/statsd/CgroupReader.java b/src/main/java/com/timgroup/statsd/CgroupReader.java index 31baa4b7..4d36dda7 100644 --- a/src/main/java/com/timgroup/statsd/CgroupReader.java +++ b/src/main/java/com/timgroup/statsd/CgroupReader.java @@ -8,7 +8,8 @@ import java.util.regex.Pattern; /** - * A reader class that retrieves the current container ID parsed from a the cgroup file. + * A reader class that retrieves the current container ID parsed from a the + * cgroup file. * */ class CgroupReader { @@ -16,8 +17,7 @@ class CgroupReader { private static final String CONTAINER_SOURCE = "[0-9a-f]{64}"; private static final String TASK_SOURCE = "[0-9a-f]{32}-\\d+"; private static final Pattern LINE_RE = Pattern.compile("^\\d+:[^:]*:(.+)$", Pattern.MULTILINE | Pattern.UNIX_LINES); - private static final Pattern CONTAINER_RE = - Pattern.compile( + private static final Pattern CONTAINER_RE = Pattern.compile( "(" + CONTAINER_SOURCE + "|" + TASK_SOURCE + ")(?:.scope)?$"); private boolean readOnce = false; @@ -27,7 +27,8 @@ class CgroupReader { * Parses /proc/self/cgroup and returns the container ID if available. * * @throws IOException - * if /proc/self/cgroup is readable and still an I/O error occurs reading from the stream + * if /proc/self/cgroup is readable and still an I/O error + * occurs reading from the stream */ public String getContainerID() throws IOException { if (readOnce) { @@ -56,7 +57,7 @@ private String read(Path path) throws IOException { * Parses a Cgroup file content and returns the corresponding container ID. * * @param cgroupsContent - * Cgroup file content + * Cgroup file content */ public static String parse(final String cgroupsContent) { final Matcher lines = LINE_RE.matcher(cgroupsContent); From 73bf9fb88e77a3c648f64fc9b585fe1602ff38a3 Mon Sep 17 00:00:00 2001 From: AliDatadog Date: Thu, 11 Jan 2024 16:52:05 +0100 Subject: [PATCH 2/4] retrieve the inode of the cgroup node controller and send it as container id --- .../com/timgroup/statsd/CgroupReader.java | 151 ++++++++++- .../com/timgroup/statsd/CgroupReaderTest.java | 249 ++++++++++++------ 2 files changed, 303 insertions(+), 97 deletions(-) diff --git a/src/main/java/com/timgroup/statsd/CgroupReader.java b/src/main/java/com/timgroup/statsd/CgroupReader.java index 4d36dda7..886556e7 100644 --- a/src/main/java/com/timgroup/statsd/CgroupReader.java +++ b/src/main/java/com/timgroup/statsd/CgroupReader.java @@ -1,9 +1,15 @@ package com.timgroup.statsd; +import java.io.BufferedReader; import java.io.IOException; +import java.io.StringReader; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -14,50 +20,89 @@ */ class CgroupReader { private static final Path CGROUP_PATH = Paths.get("/proc/self/cgroup"); + /** + * DEFAULT_CGROUP_MOUNT_PATH is the default cgroup mount path. + **/ + private static final Path DEFAULT_CGROUP_MOUNT_PATH = Paths.get("/sys/fs/cgroup"); + /** + * CGROUP_NS_PATH is the path to the cgroup namespace file. + **/ + private static final Path CGROUP_NS_PATH = Paths.get("/proc/self/ns/cgroup"); private static final String CONTAINER_SOURCE = "[0-9a-f]{64}"; private static final String TASK_SOURCE = "[0-9a-f]{32}-\\d+"; private static final Pattern LINE_RE = Pattern.compile("^\\d+:[^:]*:(.+)$", Pattern.MULTILINE | Pattern.UNIX_LINES); private static final Pattern CONTAINER_RE = Pattern.compile( "(" + CONTAINER_SOURCE + "|" + TASK_SOURCE + ")(?:.scope)?$"); + /** + * CGROUPV1_BASE_CONTROLLER is the controller used to identify the container-id + * in cgroup v1 (memory). + **/ + private static final String CGROUPV1_BASE_CONTROLLER = "memory"; + /** + * CGROUPV2_BASE_CONTROLLER is the controller used to identify the container-id + * in cgroup v2. + **/ + private static final String CGROUPV2_BASE_CONTROLLER = ""; + /** + * HOST_CGROUP_NAMESPACE_INODE is the inode of the host cgroup namespace. + **/ + private static final long HOST_CGROUP_NAMESPACE_INODE = 0xEFFFFFFBL; + private boolean readOnce = false; + /** + * containerID holds either the container ID or the cgroup controller inode. + **/ public String containerID; /** - * Parses /proc/self/cgroup and returns the container ID if available. + * Returns the container ID if available or the cgroup controller inode. * - * @throws IOException - * if /proc/self/cgroup is readable and still an I/O error - * occurs reading from the stream + * @throws IOException if /proc/self/cgroup is readable and still an I/O error + * occurs reading from the stream. */ public String getContainerID() throws IOException { if (readOnce) { return containerID; } - containerID = read(CGROUP_PATH); + final String cgroupContent = read(CGROUP_PATH); + if (cgroupContent == null || cgroupContent.isEmpty()) { + return null; + } + containerID = parse(cgroupContent); + /* + * If the container ID is not available it means that the application is either + * not running in a container or running is private cgroup namespace, we + * fallback to the cgroup controller inode. The agent (7.51+) will use it to get + * the container ID. + * + */ + if ((containerID == null || containerID.equals("")) && !isHostCgroupNamespace(CGROUP_NS_PATH)) { + containerID = getCgroupInode(DEFAULT_CGROUP_MOUNT_PATH, cgroupContent); + } return containerID; } + /** + * Parses `path` (=/proc/self/cgroup) and returns the container ID if available. + * + * @throws IOException if /proc/self/cgroup is readable and still an I/O error + * occurs reading from the stream. + */ private String read(Path path) throws IOException { readOnce = true; if (!Files.isReadable(path)) { return null; } - final String content = new String(Files.readAllBytes(path)); - if (content.isEmpty()) { - return null; - } - - return parse(content); + return new String(Files.readAllBytes(path)); } /** * Parses a Cgroup file content and returns the corresponding container ID. * - * @param cgroupsContent - * Cgroup file content + * @param cgroupsContent Cgroup file content */ public static String parse(final String cgroupsContent) { final Matcher lines = LINE_RE.matcher(cgroupsContent); @@ -71,4 +116,84 @@ public static String parse(final String cgroupsContent) { return null; } + + /** + * Returns true if the host cgroup namespace is used. + * It looks at the inode of `/proc/self/ns/cgroup` and compares it to + * HOST_CGROUP_NAMESPACE_INODE. + * + * @param path Path to the cgroup namespace file. + */ + private static boolean isHostCgroupNamespace(final Path path) { + long hostCgroupInode = inodeForPath(path); + return hostCgroupInode == HOST_CGROUP_NAMESPACE_INODE; + } + + /** + * Returns the inode for the given path. + * + * @param path Path to the cgroup namespace file. + */ + private static long inodeForPath(final Path path) { + try { + long inode = (long) Files.getAttribute(path, "unix:ino"); + return inode; + } catch (Exception e) { + return 0; + } + } + + /** + * Returns the cgroup controller inode for the given cgroup mount path and + * procSelfCgroupPath. + * + * @param cgroupMountPath Path to the cgroup mount point. + * @param cgroupContent String content of the cgroup file. + */ + public static String getCgroupInode(final Path cgroupMountPath, final String cgroupContent) throws IOException { + Map cgroupControllersPaths = parseCgroupNodePath(cgroupContent); + if (cgroupControllersPaths == null) { + return null; + } + + // Retrieve the cgroup inode from /sys/fs/cgroup+controller+cgroupNodePath + List controllers = Arrays.asList(CGROUPV1_BASE_CONTROLLER, CGROUPV2_BASE_CONTROLLER); + for (String controller : controllers) { + String cgroupNodePath = cgroupControllersPaths.get(controller); + if (cgroupNodePath == null) { + continue; + } + Path path = Paths.get(cgroupMountPath.toString(), controller, cgroupNodePath); + long inode = inodeForPath(path); + if (inode > 2) { + return "in-" + inode; + } + } + + return null; + } + + /** + * Returns a map of cgroup controllers and their corresponding cgroup path. + * + * @param cgroupContent Cgroup file content. + */ + public static Map parseCgroupNodePath(final String cgroupContent) throws IOException { + Map res = new HashMap<>(); + BufferedReader br = new BufferedReader(new StringReader(cgroupContent)); + + String line; + while ((line = br.readLine()) != null) { + String[] tokens = line.split(":"); + if (tokens.length != 3) { + continue; + } + if (CGROUPV1_BASE_CONTROLLER.equals(tokens[1]) || tokens[1].isEmpty()) { + res.put(tokens[1], tokens[2]); + } + } + + br.close(); + return res; + } } diff --git a/src/test/java/com/timgroup/statsd/CgroupReaderTest.java b/src/test/java/com/timgroup/statsd/CgroupReaderTest.java index 8d6036da..11ab6bd9 100644 --- a/src/test/java/com/timgroup/statsd/CgroupReaderTest.java +++ b/src/test/java/com/timgroup/statsd/CgroupReaderTest.java @@ -1,8 +1,17 @@ package com.timgroup.statsd; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import org.junit.Assume; +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; @@ -12,112 +21,184 @@ public class CgroupReaderTest { public void containerID_parse() throws Exception { // Docker String docker = new StringBuilder() - .append("13:name=systemd:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("12:pids:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("11:hugetlb:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("10:net_prio:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("9:perf_event:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("8:net_cls:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("7:freezer:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("6:devices:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("5:memory:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("4:blkio:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("3:cpuacct:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("2:cpu:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("1:cpuset:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860") - .toString(); - - assertThat(CgroupReader.parse(docker), equalTo("3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860")); + .append("13:name=systemd:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("12:pids:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("11:hugetlb:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("10:net_prio:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("9:perf_event:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("8:net_cls:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("7:freezer:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("6:devices:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("5:memory:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("4:blkio:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("3:cpuacct:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("2:cpu:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("1:cpuset:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860") + .toString(); + + assertThat(CgroupReader.parse(docker), + equalTo("3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860")); // Kubernetes String kubernetes = new StringBuilder() - .append("11:perf_event:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") - .append("10:pids:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") - .append("9:memory:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") - .append("8:cpu,cpuacct:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") - .append("7:blkio:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") - .append("6:cpuset:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") - .append("5:devices:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") - .append("4:freezer:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") - .append(" 3:net_cls,net_prio:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") - .append("2:hugetlb:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") - .append("1:name=systemd:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1") - .toString(); - - assertThat(CgroupReader.parse(kubernetes), equalTo("3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1")); + .append("11:perf_event:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") + .append("10:pids:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") + .append("9:memory:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") + .append("8:cpu,cpuacct:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") + .append("7:blkio:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") + .append("6:cpuset:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") + .append("5:devices:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") + .append("4:freezer:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") + .append(" 3:net_cls,net_prio:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") + .append("2:hugetlb:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") + .append("1:name=systemd:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1") + .toString(); + + assertThat(CgroupReader.parse(kubernetes), + equalTo("3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1")); // ECS EC2 String ecs = new StringBuilder() - .append("9:perf_event:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") - .append("8:memory:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") - .append("7:hugetlb:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") - .append("6:freezer:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") - .append("5:devices:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") - .append("4:cpuset:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") - .append("3:cpuacct:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") - .append("2:cpu:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") - .append("1:blkio:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce") - .toString(); - - assertThat(CgroupReader.parse(ecs), equalTo("38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce")); + .append("9:perf_event:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") + .append("8:memory:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") + .append("7:hugetlb:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") + .append("6:freezer:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") + .append("5:devices:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") + .append("4:cpuset:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") + .append("3:cpuacct:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") + .append("2:cpu:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") + .append("1:blkio:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce") + .toString(); + + assertThat(CgroupReader.parse(ecs), + equalTo("38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce")); // ECS Fargate String ecsFargate = new StringBuilder() - .append("11:hugetlb:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") - .append("10:pids:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") - .append("9:cpuset:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") - .append("8:net_cls,net_prio:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") - .append("7:cpu,cpuacct:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") - .append("6:perf_event:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") - .append("5:freezer:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") - .append("4:devices:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") - .append("3:blkio:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") - .append("2:memory:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") - .append("1:name=systemd:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") - .toString(); - - assertThat(CgroupReader.parse(ecsFargate), equalTo("432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da")); + .append("11:hugetlb:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") + .append("10:pids:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") + .append("9:cpuset:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") + .append("8:net_cls,net_prio:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") + .append("7:cpu,cpuacct:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") + .append("6:perf_event:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") + .append("5:freezer:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") + .append("4:devices:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") + .append("3:blkio:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") + .append("2:memory:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") + .append("1:name=systemd:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") + .toString(); + + assertThat(CgroupReader.parse(ecsFargate), + equalTo("432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da")); // ECS Fargate >= 1.4.0 String ecsFargate14 = new StringBuilder() - .append("11:hugetlb:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") - .append("10:pids:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") - .append("9:cpuset:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") - .append("8:net_cls,net_prio:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") - .append("7:cpu,cpuacct:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") - .append("6:perf_event:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") - .append("5:freezer:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") - .append("4:devices:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") - .append("3:blkio:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") - .append("2:memory:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") - .append("1:name=systemd:/ecs/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") - .toString(); + .append("11:hugetlb:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") + .append("10:pids:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") + .append("9:cpuset:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") + .append("8:net_cls,net_prio:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") + .append("7:cpu,cpuacct:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") + .append("6:perf_event:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") + .append("5:freezer:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") + .append("4:devices:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") + .append("3:blkio:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") + .append("2:memory:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") + .append("1:name=systemd:/ecs/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") + .toString(); assertThat(CgroupReader.parse(ecsFargate14), equalTo("34dc0b5e626f2c5c4c5170e34b10e765-1234567890")); // Linux non-containerized String nonContainerized = new StringBuilder() - .append("11:blkio:/user.slice/user-0.slice/session-14.scope\n") - .append("10:memory:/user.slice/user-0.slice/session-14.scope\n") - .append("9:hugetlb:/\n") - .append("8:cpuset:/\n") - .append("7:pids:/user.slice/user-0.slice/session-14.scope\n") - .append("6:freezer:/\n") - .append("5:net_cls,net_prio:/\n") - .append("4:perf_event:/\n") - .append("3:cpu,cpuacct:/user.slice/user-0.slice/session-14.scope\n") - .append("2:devices:/user.slice/user-0.slice/session-14.scope\n") - .append("1:name=systemd:/user.slice/user-0.slice/session-14.scope\n") - .toString(); + .append("11:blkio:/user.slice/user-0.slice/session-14.scope\n") + .append("10:memory:/user.slice/user-0.slice/session-14.scope\n") + .append("9:hugetlb:/\n") + .append("8:cpuset:/\n") + .append("7:pids:/user.slice/user-0.slice/session-14.scope\n") + .append("6:freezer:/\n") + .append("5:net_cls,net_prio:/\n") + .append("4:perf_event:/\n") + .append("3:cpu,cpuacct:/user.slice/user-0.slice/session-14.scope\n") + .append("2:devices:/user.slice/user-0.slice/session-14.scope\n") + .append("1:name=systemd:/user.slice/user-0.slice/session-14.scope\n") + .toString(); assertNull(CgroupReader.parse(nonContainerized)); // Linux 4.4 String linux44 = new StringBuilder() - .append("11:pids:/system.slice/docker-cde7c2bab394630a42d73dc610b9c57415dced996106665d427f6d0566594411.scope\n") - .append("1:name=systemd:/system.slice/docker-cde7c2bab394630a42d73dc610b9c57415dced996106665d427f6d0566594411.scope\n") - .toString(); + .append("11:pids:/system.slice/docker-cde7c2bab394630a42d73dc610b9c57415dced996106665d427f6d0566594411.scope\n") + .append("1:name=systemd:/system.slice/docker-cde7c2bab394630a42d73dc610b9c57415dced996106665d427f6d0566594411.scope\n") + .toString(); + + assertThat(CgroupReader.parse(linux44), + equalTo("cde7c2bab394630a42d73dc610b9c57415dced996106665d427f6d0566594411")); + } + + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + @Before + public void assumeNotWindows() { + Assume.assumeFalse(System.getProperty("os.name").startsWith("Windows")); + } + + @Test + public void testWithExistingCgroupV1Inode() throws IOException { + + folder.create(); + Path cgroupMountPath = folder.newFolder("sys", "fs", "cgroup").toPath(); + Path controllerDir = cgroupMountPath.resolve("memory"); + Path cgroupPath = controllerDir.resolve("docker"); + Path nodePath = cgroupPath.resolve("3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860"); + Files.createDirectories(nodePath); + + long inode = (long) Files.getAttribute(nodePath, "unix:ino"); + + String cgroupContent = "myfile\5:memory:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n"; + String inodeResult = CgroupReader.getCgroupInode(cgroupMountPath, cgroupContent); + + assertEquals("in-" + inode, inodeResult); + } + + @Test + public void testWithExistingCgroupV2Inode() throws IOException { + + folder.create(); + Path cgroupMountPath = folder.newFolder("sys", "fs", "cgroup").toPath(); + Files.createDirectories(cgroupMountPath); + + long inodeNumber = (long) Files.getAttribute(cgroupMountPath, "unix:ino"); + String cgroupContent = "0::/\n"; + String inodeResult = CgroupReader.getCgroupInode(cgroupMountPath, cgroupContent); + + assertEquals("in-" + inodeNumber, inodeResult); + } + + @Test + public void testWithNonExistentCgroupNodePath() throws IOException { + + folder.create(); + Path cgroupMountPath = folder.newFolder("sys", "fs", "cgroup").toPath(); + + String cgroupContent = "memory:/nonexistentpath\n"; + String inodeResult = CgroupReader.getCgroupInode(cgroupMountPath, + cgroupContent); + + assertNull(inodeResult); + + } + + @Test + public void testWithEmptyCgroupContent() throws IOException { + + folder.create(); + Path cgroupMountPath = folder.newFolder("sys", "fs", "cgroup").toPath(); + + String cgroupContent = ""; + String inodeResult = CgroupReader.getCgroupInode(cgroupMountPath, + cgroupContent); - assertThat(CgroupReader.parse(linux44), equalTo("cde7c2bab394630a42d73dc610b9c57415dced996106665d427f6d0566594411")); + assertNull(inodeResult); } } From 7113c886b054ecfa0d22d385fef0f0ab0f79492b Mon Sep 17 00:00:00 2001 From: AliDatadog Date: Mon, 15 Jan 2024 14:29:39 +0100 Subject: [PATCH 3/4] apply suggestions: add comments to explain some choices and use CHGROUPV2_BASE_CONTROLLER --- .../java/com/timgroup/statsd/CgroupReader.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/timgroup/statsd/CgroupReader.java b/src/main/java/com/timgroup/statsd/CgroupReader.java index 886556e7..6fb46f4e 100644 --- a/src/main/java/com/timgroup/statsd/CgroupReader.java +++ b/src/main/java/com/timgroup/statsd/CgroupReader.java @@ -76,6 +76,8 @@ public String getContainerID() throws IOException { * not running in a container or running is private cgroup namespace, we * fallback to the cgroup controller inode. The agent (7.51+) will use it to get * the container ID. + * In Host cgroup namespace, the container ID should be found. If it is not + * found, it means that the application is running on a host/vm. * */ if ((containerID == null || containerID.equals("")) && !isHostCgroupNamespace(CGROUP_NS_PATH)) { @@ -85,7 +87,7 @@ public String getContainerID() throws IOException { } /** - * Parses `path` (=/proc/self/cgroup) and returns the container ID if available. + * Returns the content of `path` (=/proc/self/cgroup). * * @throws IOException if /proc/self/cgroup is readable and still an I/O error * occurs reading from the stream. @@ -100,7 +102,9 @@ private String read(Path path) throws IOException { } /** - * Parses a Cgroup file content and returns the corresponding container ID. + * Parses a Cgroup file (=/proc/self/cgroup) content and returns the + * corresponding container ID. It can be found only if the container + * is running in host cgroup namespace. * * @param cgroupsContent Cgroup file content */ @@ -165,6 +169,10 @@ public static String getCgroupInode(final Path cgroupMountPath, final String cgr } Path path = Paths.get(cgroupMountPath.toString(), controller, cgroupNodePath); long inode = inodeForPath(path); + /* + * Inode 0 is not a valid inode. Inode 1 is a bad block inode and inode 2 is the + * root of a filesystem. We can safely ignore them. + */ if (inode > 2) { return "in-" + inode; } @@ -188,7 +196,7 @@ public static Map parseCgroupNodePath(final String cgroupContent if (tokens.length != 3) { continue; } - if (CGROUPV1_BASE_CONTROLLER.equals(tokens[1]) || tokens[1].isEmpty()) { + if (CGROUPV1_BASE_CONTROLLER.equals(tokens[1]) || CGROUPV2_BASE_CONTROLLER.equals(tokens[1])) { res.put(tokens[1], tokens[2]); } } From c15f5f4aeeafce69931a8b70496f8f36f036e9dd Mon Sep 17 00:00:00 2001 From: AliDatadog Date: Tue, 16 Jan 2024 15:04:25 +0100 Subject: [PATCH 4/4] undo lint --- .../com/timgroup/statsd/CgroupReader.java | 4 +- .../com/timgroup/statsd/CgroupReaderTest.java | 173 +++++++++--------- 2 files changed, 86 insertions(+), 91 deletions(-) diff --git a/src/main/java/com/timgroup/statsd/CgroupReader.java b/src/main/java/com/timgroup/statsd/CgroupReader.java index 6fb46f4e..4ab54a4a 100644 --- a/src/main/java/com/timgroup/statsd/CgroupReader.java +++ b/src/main/java/com/timgroup/statsd/CgroupReader.java @@ -14,8 +14,8 @@ import java.util.regex.Pattern; /** - * A reader class that retrieves the current container ID parsed from a the - * cgroup file. + * A reader class that retrieves the current container ID or the cgroup controller + * inode parsed from the cgroup file. * */ class CgroupReader { diff --git a/src/test/java/com/timgroup/statsd/CgroupReaderTest.java b/src/test/java/com/timgroup/statsd/CgroupReaderTest.java index 11ab6bd9..548d0728 100644 --- a/src/test/java/com/timgroup/statsd/CgroupReaderTest.java +++ b/src/test/java/com/timgroup/statsd/CgroupReaderTest.java @@ -21,118 +21,113 @@ public class CgroupReaderTest { public void containerID_parse() throws Exception { // Docker String docker = new StringBuilder() - .append("13:name=systemd:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("12:pids:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("11:hugetlb:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("10:net_prio:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("9:perf_event:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("8:net_cls:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("7:freezer:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("6:devices:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("5:memory:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("4:blkio:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("3:cpuacct:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("2:cpu:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") - .append("1:cpuset:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860") - .toString(); - - assertThat(CgroupReader.parse(docker), - equalTo("3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860")); + .append("13:name=systemd:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("12:pids:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("11:hugetlb:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("10:net_prio:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("9:perf_event:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("8:net_cls:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("7:freezer:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("6:devices:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("5:memory:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("4:blkio:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("3:cpuacct:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("2:cpu:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860\n") + .append("1:cpuset:/docker/3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860") + .toString(); + + assertThat(CgroupReader.parse(docker), equalTo("3726184226f5d3147c25fdeab5b60097e378e8a720503a5e19ecfdf29f869860")); // Kubernetes String kubernetes = new StringBuilder() - .append("11:perf_event:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") - .append("10:pids:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") - .append("9:memory:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") - .append("8:cpu,cpuacct:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") - .append("7:blkio:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") - .append("6:cpuset:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") - .append("5:devices:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") - .append("4:freezer:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") - .append(" 3:net_cls,net_prio:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") - .append("2:hugetlb:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") - .append("1:name=systemd:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1") - .toString(); - - assertThat(CgroupReader.parse(kubernetes), - equalTo("3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1")); + .append("11:perf_event:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") + .append("10:pids:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") + .append("9:memory:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") + .append("8:cpu,cpuacct:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") + .append("7:blkio:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") + .append("6:cpuset:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") + .append("5:devices:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") + .append("4:freezer:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") + .append(" 3:net_cls,net_prio:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") + .append("2:hugetlb:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1\n") + .append("1:name=systemd:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1") + .toString(); + + assertThat(CgroupReader.parse(kubernetes), equalTo("3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1")); // ECS EC2 String ecs = new StringBuilder() - .append("9:perf_event:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") - .append("8:memory:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") - .append("7:hugetlb:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") - .append("6:freezer:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") - .append("5:devices:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") - .append("4:cpuset:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") - .append("3:cpuacct:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") - .append("2:cpu:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") - .append("1:blkio:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce") - .toString(); - - assertThat(CgroupReader.parse(ecs), - equalTo("38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce")); + .append("9:perf_event:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") + .append("8:memory:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") + .append("7:hugetlb:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") + .append("6:freezer:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") + .append("5:devices:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") + .append("4:cpuset:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") + .append("3:cpuacct:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") + .append("2:cpu:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce\n") + .append("1:blkio:/ecs/name-ecs-classic/5a0d5ceddf6c44c1928d367a815d890f/38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce") + .toString(); + + assertThat(CgroupReader.parse(ecs), equalTo("38fac3e99302b3622be089dd41e7ccf38aff368a86cc339972075136ee2710ce")); // ECS Fargate String ecsFargate = new StringBuilder() - .append("11:hugetlb:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") - .append("10:pids:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") - .append("9:cpuset:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") - .append("8:net_cls,net_prio:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") - .append("7:cpu,cpuacct:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") - .append("6:perf_event:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") - .append("5:freezer:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") - .append("4:devices:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") - .append("3:blkio:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") - .append("2:memory:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") - .append("1:name=systemd:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") - .toString(); - - assertThat(CgroupReader.parse(ecsFargate), - equalTo("432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da")); + .append("11:hugetlb:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") + .append("10:pids:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") + .append("9:cpuset:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") + .append("8:net_cls,net_prio:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") + .append("7:cpu,cpuacct:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") + .append("6:perf_event:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") + .append("5:freezer:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") + .append("4:devices:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") + .append("3:blkio:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") + .append("2:memory:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") + .append("1:name=systemd:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da\n") + .toString(); + + assertThat(CgroupReader.parse(ecsFargate), equalTo("432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da")); // ECS Fargate >= 1.4.0 String ecsFargate14 = new StringBuilder() - .append("11:hugetlb:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") - .append("10:pids:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") - .append("9:cpuset:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") - .append("8:net_cls,net_prio:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") - .append("7:cpu,cpuacct:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") - .append("6:perf_event:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") - .append("5:freezer:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") - .append("4:devices:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") - .append("3:blkio:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") - .append("2:memory:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") - .append("1:name=systemd:/ecs/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") - .toString(); + .append("11:hugetlb:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") + .append("10:pids:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") + .append("9:cpuset:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") + .append("8:net_cls,net_prio:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") + .append("7:cpu,cpuacct:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") + .append("6:perf_event:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") + .append("5:freezer:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") + .append("4:devices:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") + .append("3:blkio:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") + .append("2:memory:/ecs/55091c13-b8cf-4801-b527-f4601742204d/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") + .append("1:name=systemd:/ecs/34dc0b5e626f2c5c4c5170e34b10e765-1234567890\n") + .toString(); assertThat(CgroupReader.parse(ecsFargate14), equalTo("34dc0b5e626f2c5c4c5170e34b10e765-1234567890")); // Linux non-containerized String nonContainerized = new StringBuilder() - .append("11:blkio:/user.slice/user-0.slice/session-14.scope\n") - .append("10:memory:/user.slice/user-0.slice/session-14.scope\n") - .append("9:hugetlb:/\n") - .append("8:cpuset:/\n") - .append("7:pids:/user.slice/user-0.slice/session-14.scope\n") - .append("6:freezer:/\n") - .append("5:net_cls,net_prio:/\n") - .append("4:perf_event:/\n") - .append("3:cpu,cpuacct:/user.slice/user-0.slice/session-14.scope\n") - .append("2:devices:/user.slice/user-0.slice/session-14.scope\n") - .append("1:name=systemd:/user.slice/user-0.slice/session-14.scope\n") - .toString(); + .append("11:blkio:/user.slice/user-0.slice/session-14.scope\n") + .append("10:memory:/user.slice/user-0.slice/session-14.scope\n") + .append("9:hugetlb:/\n") + .append("8:cpuset:/\n") + .append("7:pids:/user.slice/user-0.slice/session-14.scope\n") + .append("6:freezer:/\n") + .append("5:net_cls,net_prio:/\n") + .append("4:perf_event:/\n") + .append("3:cpu,cpuacct:/user.slice/user-0.slice/session-14.scope\n") + .append("2:devices:/user.slice/user-0.slice/session-14.scope\n") + .append("1:name=systemd:/user.slice/user-0.slice/session-14.scope\n") + .toString(); assertNull(CgroupReader.parse(nonContainerized)); // Linux 4.4 String linux44 = new StringBuilder() - .append("11:pids:/system.slice/docker-cde7c2bab394630a42d73dc610b9c57415dced996106665d427f6d0566594411.scope\n") - .append("1:name=systemd:/system.slice/docker-cde7c2bab394630a42d73dc610b9c57415dced996106665d427f6d0566594411.scope\n") - .toString(); + .append("11:pids:/system.slice/docker-cde7c2bab394630a42d73dc610b9c57415dced996106665d427f6d0566594411.scope\n") + .append("1:name=systemd:/system.slice/docker-cde7c2bab394630a42d73dc610b9c57415dced996106665d427f6d0566594411.scope\n") + .toString(); - assertThat(CgroupReader.parse(linux44), - equalTo("cde7c2bab394630a42d73dc610b9c57415dced996106665d427f6d0566594411")); + assertThat(CgroupReader.parse(linux44), equalTo("cde7c2bab394630a42d73dc610b9c57415dced996106665d427f6d0566594411")); } @Rule