Skip to content

Commit 5675c49

Browse files
Rui Liupichlermarc
Rui Liu
andauthored
fix: Update defect of of wrong resource attribute of "container.id" (#1682)
Co-authored-by: Marc Pichler <[email protected]>
1 parent deb9aa4 commit 5675c49

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

detectors/node/opentelemetry-resource-detector-container/src/detectors/ContainerDetector.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,29 @@ export class ContainerDetector implements Detector {
5757
this.UTF8_UNICODE
5858
);
5959
const splitData = rawData.trim().split('\n');
60-
for (const str of splitData) {
61-
if (str.length >= this.CONTAINER_ID_LENGTH) {
62-
return str.substring(str.length - this.CONTAINER_ID_LENGTH);
60+
for (const line of splitData) {
61+
const lastSlashIdx = line.lastIndexOf('/');
62+
if (lastSlashIdx === -1) {
63+
continue;
64+
}
65+
const lastSection = line.substring(lastSlashIdx + 1);
66+
const colonIdx = lastSection.lastIndexOf(':');
67+
if (colonIdx !== -1) {
68+
// since containerd v1.5.0+, containerId is divided by the last colon when the cgroupDriver is systemd:
69+
// https://github.com/containerd/containerd/blob/release/1.5/pkg/cri/server/helpers_linux.go#L64
70+
return lastSection.substring(colonIdx + 1);
71+
} else {
72+
let startIdx = lastSection.lastIndexOf('-');
73+
let endIdx = lastSection.lastIndexOf('.');
74+
75+
startIdx = startIdx === -1 ? 0 : startIdx + 1;
76+
if (endIdx === -1) {
77+
endIdx = lastSection.length;
78+
}
79+
if (startIdx > endIdx) {
80+
continue;
81+
}
82+
return lastSection.substring(startIdx, endIdx);
6383
}
6484
}
6585
return undefined;

detectors/node/opentelemetry-resource-detector-container/test/ContainerDetector.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { ContainerDetector } from '../src';
2828
describe('ContainerDetector', () => {
2929
let readStub;
3030
const correctCgroupV1Data =
31-
'bcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm';
31+
'12:pids:/kubepods.slice/bcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm';
3232
const correctCgroupV2Data = `tmhdefghijklmnopqrstuvwxyzafgrefghiugkmnopqrstuvwxyzabcdefghijkl/hostname
3333
fhkjdshgfhsdfjhdsfkjhfkdshkjhfd/host
3434
sahfhfjkhjhfhjdhfjkdhfkjdhfjkhhdsjfhdfhjdhfkj/somethingelse`;

0 commit comments

Comments
 (0)