Skip to content

Commit 187269e

Browse files
authored
Fix #8462 - Use more permissive regexp to extract containerid (#18272)
1 parent de3d249 commit 187269e

4 files changed

Lines changed: 29 additions & 11 deletions

File tree

instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/CgroupV2ContainerIdExtractor.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import static java.util.Optional.empty;
99
import static java.util.logging.Level.WARNING;
10+
import static java.util.stream.Collectors.toList;
1011

1112
import java.io.IOException;
1213
import java.nio.file.Path;
@@ -16,7 +17,6 @@
1617
import java.util.logging.Logger;
1718
import java.util.regex.Matcher;
1819
import java.util.regex.Pattern;
19-
import java.util.stream.Stream;
2020

2121
/** Utility for extracting the container ID from runtimes inside cgroup v2 containers. */
2222
class CgroupV2ContainerIdExtractor {
@@ -25,7 +25,8 @@ class CgroupV2ContainerIdExtractor {
2525
Logger.getLogger(CgroupV2ContainerIdExtractor.class.getName());
2626

2727
static final Path V2_CGROUP_PATH = Paths.get("/proc/self/mountinfo");
28-
private static final Pattern CONTAINER_ID_RE = Pattern.compile("^[0-9a-f]{64}$");
28+
private static final Pattern CONTAINER_ID_RE =
29+
Pattern.compile("^\\d+ \\d+ \\d+:\\d+ [^ ]*/containers/[^ ]*?([0-9a-f]{64})");
2930
private static final Pattern CONTAINERD_CONTAINER_ID_RE =
3031
Pattern.compile("cri-containerd:[0-9a-f]{64}");
3132
private static final Pattern CRIO_CONTAINER_ID_RE = Pattern.compile("\\/crio-[0-9a-f]{64}");
@@ -76,12 +77,19 @@ Optional<String> extractContainerId() {
7677
return optCid;
7778
}
7879

79-
return fileAsList.stream()
80-
.filter(line -> line.contains("/containers/"))
81-
.flatMap(line -> Stream.of(line.split("/")))
82-
.map(CONTAINER_ID_RE::matcher)
83-
.filter(Matcher::matches)
84-
.reduce((first, second) -> second)
85-
.map(matcher -> matcher.group(0));
80+
List<String> containerIds =
81+
fileAsList.stream()
82+
.filter(line -> line.contains("/containers/"))
83+
.map(CONTAINER_ID_RE::matcher)
84+
.filter(Matcher::find)
85+
.map(matcher -> matcher.group(1))
86+
.distinct()
87+
.collect(toList());
88+
if (containerIds.size() > 1) {
89+
logger.log(WARNING, "Multiple container ids found in v2 cgroup path: {0}", containerIds);
90+
}
91+
return containerIds.isEmpty()
92+
? empty()
93+
: Optional.of(containerIds.get(containerIds.size() - 1));
8694
}
8795
}

instrumentation/resources/library/src/test/java/io/opentelemetry/instrumentation/resources/CgroupV2ContainerIdExtractorTest.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,19 @@ void extractSuccess_containerd() throws IOException {
6565
}
6666

6767
@Test
68-
void extractSuccess_podman() throws IOException {
68+
void extractSuccess_podman1() throws IOException {
6969
verifyContainerId(
70-
"podman_proc_self_mountinfo",
70+
"podman_proc_self_mountinfo1",
7171
"2a33efc76e519c137fe6093179653788bed6162d4a15e5131c8e835c968afbe6");
7272
}
7373

74+
@Test
75+
void extractSuccess_podman2() throws IOException {
76+
verifyContainerId(
77+
"podman_proc_self_mountinfo2",
78+
"1b8f3c3819fbae7b858e55c52ec5783d06aa9cbf337815ecb9b96f47babcee5d");
79+
}
80+
7481
@Test
7582
void extractSuccess_crio() throws IOException {
7683
verifyContainerId(

instrumentation/resources/library/src/test/resources/podman_proc_self_mountinfo renamed to instrumentation/resources/library/src/test/resources/podman_proc_self_mountinfo1

File renamed without changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
495 499 0:54 /containers/overlay-containers/1b8f3c3819fbae7b858e55c52ec5783d06aa9cbf337815ecb9b96f47babcee5d/userdata/.containerenv /run/.containerenv rw,nosuid,nodev,relatime - tmpfs tmpfs rw,size=792480k,nr_inodes=198120,mode=700,uid=1000,gid=1000,inode64
2+
496 499 0:54 /containers/overlay-containers/1b8f3c3819fbae7b858e55c52ec5783d06aa9cbf337815ecb9b96f47babcee5d/userdata/run/secrets /run/secrets rw,nosuid,nodev,relatime - tmpfs tmpfs rw,size=792480k,nr_inodes=198120,mode=700,uid=1000,gid=1000,inode64
3+
499 325 0:81 / / rw,relatime - overlay overlay rw,lowerdir=/home/kiosk/.local/share/containers/storage/overlay/l/HRSQNMWW6Y6RMZ7ACTG6KK3IXD:/home/kiosk/.local/share/containers/storage/overlay/l/W23BT5LKDOIT7F5GNQ3DAFQW2J:/home/kiosk/.local/share/containers/storage/overlay/l/EHSFOISJJJSHGBWH7JZPSVFNCA:/home/kiosk/.local/share/containers/storage/overlay/l/7HOJBSPCIOYYKOXI2UHNAQRVGE:/home/kiosk/.local/share/containers/storage/overlay/l/WY4HKNEYETZGULKVTKU2SXGSZE:/home/kiosk/.local/share/containers/storage/overlay/l/MKJVFIO2LEFULLXCPVPNGOG7PQ:/home/kiosk/.local/share/containers/storage/overlay/l/DSPUZD5TRP673BCXTUOO5LG5X7:/home/kiosk/.local/share/containers/storage/overlay/l/JB6M4RNLP4O5WKSGNPGZW3SXNU:/home/kiosk/.local/share/containers/storage/overlay/l/CNVLAFAR4SHT6HZIKIXMEOBIIF:/home/kiosk/.local/share/containers/storage/overlay/l/VQZCBYV3Y6PV2PHNZDTRQ7UYOU,upperdir=/home/kiosk/.local/share/containers/storage/overlay/cf26b720fcbfb0546ac3eee4be26d3e2a97e9ee51ad878bcb1ebb0b5d378d5ab/diff,workdir=/home/kiosk/.local/share/containers/storage/overlay/cf26b720fcbfb0546ac3eee4be26d3e2a97e9ee51ad878bcb1ebb0b5d378d5ab/work,redirect_dir=nofollow,uuid=on,volatile,userxattr

0 commit comments

Comments
 (0)