in apm-agent-core/src/main/java/co/elastic/apm/agent/metrics/builtin/CGroupMetrics.java [84:162]
private CgroupFiles findCgroupFiles(File procSelfCgroup, File mountInfo) {
if (!procSelfCgroup.canRead()) {
logger.debug("Cannot find/read /proc/self/cgroup file. Cgroup metrics will not be reported.");
return null;
}
String cgroupLine = null;
try (BufferedReader fileReader = new BufferedReader(new FileReader(procSelfCgroup))) {
String currentLine = fileReader.readLine();
while (currentLine != null) {
if (cgroupLine == null && currentLine.startsWith("0:")) {
cgroupLine = currentLine;
}
if (MEMORY_CGROUP.matcher(currentLine).matches()) {
cgroupLine = currentLine;
break;
}
currentLine = fileReader.readLine();
}
if (cgroupLine == null) {
logger.warn("No /proc/self/cgroup file line matched the tested patterns. Cgroup metrics will not be reported.");
return null;
}
CgroupFiles cgroupFiles;
// Try to discover the cgroup fs path from the mountinfo file
if (mountInfo.canRead()) {
String mountLine = null;
try (BufferedReader fileMountInfoReader = new BufferedReader(new FileReader(mountInfo))) {
mountLine = fileMountInfoReader.readLine();
while (mountLine != null) {
// cgroup v2
String rootCgroupFsPath = applyCgroupRegex(CGROUP2_MOUNT_POINT, mountLine);
if (rootCgroupFsPath != null) {
cgroupFiles = createCgroup2Files(cgroupLine, new File(rootCgroupFsPath));
if (cgroupFiles != null) {
return cgroupFiles;
}
}
// cgroup v1
String memoryMountPath = applyCgroupRegex(CGROUP1_MOUNT_POINT, mountLine);
if (memoryMountPath != null) {
cgroupFiles = createCgroup1Files(new File(memoryMountPath));
if (cgroupFiles != null) {
return cgroupFiles;
}
}
mountLine = fileMountInfoReader.readLine();
}
} catch (Exception e) {
logger.info("Failed to discover memory mount files path based on mountinfo line '{}'.", mountLine);
}
} else {
logger.info("Failed to find/read /proc/self/mountinfo file. Looking for memory files in /sys/fs/cgroup.");
}
// Failed to auto-discover the cgroup fs path from mountinfo, fall back to /sys/fs/cgroup
// cgroup v2
cgroupFiles = createCgroup2Files(cgroupLine, new File(DEFAULT_SYS_FS_CGROUP));
if (cgroupFiles != null) {
return cgroupFiles;
}
// cgroup v1
cgroupFiles = createCgroup1Files(new File(DEFAULT_SYS_FS_CGROUP + File.pathSeparator + "memory"));
if (cgroupFiles != null) {
return cgroupFiles;
}
} catch (Exception e) {
logger.error("Failed to discover memory mount files path based on cgroup line '" + cgroupLine +
"'. Cgroup metrics will not be reported", e);
}
return null;
}