Skip to content

Commit aea98ba

Browse files
authored
Fix container detection fails in k8s with containerd v1.5.0+ environments (#4733)
* pr with issue#4730 * PR for issue#4730 Signed-off-by: wuzhaoyin <[email protected]> * Annotation adjustment Signed-off-by: wuzhaoyin <[email protected]> * fix code owner review required fail Signed-off-by: wuzhaoyin <[email protected]> Signed-off-by: wuzhaoyin <[email protected]>
1 parent 928d112 commit aea98ba

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

sdk-extensions/resources/src/main/java/io/opentelemetry/sdk/extension/resources/ContainerResource.java

+21-9
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,31 @@ private static String getIdFromLine(String line) {
8686
return null;
8787
}
8888

89+
String containerId;
90+
8991
String lastSection = line.substring(lastSlashIdx + 1);
90-
int startIdx = lastSection.lastIndexOf('-');
91-
int endIdx = lastSection.lastIndexOf('.');
92+
int colonIdx = lastSection.lastIndexOf(':');
9293

93-
startIdx = startIdx == -1 ? 0 : startIdx + 1;
94-
if (endIdx == -1) {
95-
endIdx = lastSection.length();
96-
}
97-
if (startIdx > endIdx) {
98-
return null;
94+
if (colonIdx != -1) {
95+
// since containerd v1.5.0+, containerId is divided by the last colon when the cgroupDriver is
96+
// systemd:
97+
// https://github.com/containerd/containerd/blob/release/1.5/pkg/cri/server/helpers_linux.go#L64
98+
containerId = lastSection.substring(colonIdx + 1);
99+
} else {
100+
int startIdx = lastSection.lastIndexOf('-');
101+
int endIdx = lastSection.lastIndexOf('.');
102+
103+
startIdx = startIdx == -1 ? 0 : startIdx + 1;
104+
if (endIdx == -1) {
105+
endIdx = lastSection.length();
106+
}
107+
if (startIdx > endIdx) {
108+
return null;
109+
}
110+
111+
containerId = lastSection.substring(startIdx, endIdx);
99112
}
100113

101-
String containerId = lastSection.substring(startIdx, endIdx);
102114
if (OtelEncodingUtils.isValidBase16String(containerId) && !containerId.isEmpty()) {
103115
return containerId;
104116
} else {

sdk-extensions/resources/src/test/java/io/opentelemetry/sdk/extension/resources/ContainerResourceTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ void buildResource_Valid(@TempDir Path tempFolder) throws IOException {
8484
"11:perf_event:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod4415fd05_2c0f_4533_909b_f2180dca8d7c.slice/cri-containerd-713a77a26fe2a38ebebd5709604a048c3d380db1eb16aa43aca0b2499e54733c.scope");
8585
assertThat(getContainerId(buildResource(cgroup5)))
8686
.isEqualTo("713a77a26fe2a38ebebd5709604a048c3d380db1eb16aa43aca0b2499e54733c");
87+
88+
// with colon, env: k8s v1.24.0, the cgroupDriver by systemd(default), and container is
89+
// cri-containerd v1.6.8
90+
Path cgroup6 =
91+
createCGroup(
92+
tempFolder.resolve("cgroup6"),
93+
"11:devices:/system.slice/containerd.service/kubepods-pod87a18a64_b74a_454a_b10b_a4a36059d0a3.slice:cri-containerd:05c48c82caff3be3d7f1e896981dd410e81487538936914f32b624d168de9db0");
94+
assertThat(getContainerId(buildResource(cgroup6)))
95+
.isEqualTo("05c48c82caff3be3d7f1e896981dd410e81487538936914f32b624d168de9db0");
8796
}
8897

8998
private static String getContainerId(Resource resource) {

0 commit comments

Comments
 (0)