Skip to content

Commit

Permalink
fix: Update defect of of wrong resource attribute of "container.id" (#…
Browse files Browse the repository at this point in the history
…1682)

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
  • Loading branch information
Rui Liu and pichlermarc committed Sep 19, 2023
1 parent deb9aa4 commit 5675c49
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,29 @@ export class ContainerDetector implements Detector {
this.UTF8_UNICODE
);
const splitData = rawData.trim().split('\n');
for (const str of splitData) {
if (str.length >= this.CONTAINER_ID_LENGTH) {
return str.substring(str.length - this.CONTAINER_ID_LENGTH);
for (const line of splitData) {
const lastSlashIdx = line.lastIndexOf('/');
if (lastSlashIdx === -1) {
continue;
}
const lastSection = line.substring(lastSlashIdx + 1);
const colonIdx = lastSection.lastIndexOf(':');
if (colonIdx !== -1) {
// since containerd v1.5.0+, containerId is divided by the last colon when the cgroupDriver is systemd:
// https://github.com/containerd/containerd/blob/release/1.5/pkg/cri/server/helpers_linux.go#L64
return lastSection.substring(colonIdx + 1);
} else {
let startIdx = lastSection.lastIndexOf('-');
let endIdx = lastSection.lastIndexOf('.');

startIdx = startIdx === -1 ? 0 : startIdx + 1;
if (endIdx === -1) {
endIdx = lastSection.length;
}
if (startIdx > endIdx) {
continue;
}
return lastSection.substring(startIdx, endIdx);
}
}
return undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { ContainerDetector } from '../src';
describe('ContainerDetector', () => {
let readStub;
const correctCgroupV1Data =
'bcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm';
'12:pids:/kubepods.slice/bcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm';
const correctCgroupV2Data = `tmhdefghijklmnopqrstuvwxyzafgrefghiugkmnopqrstuvwxyzabcdefghijkl/hostname
fhkjdshgfhsdfjhdsfkjhfkdshkjhfd/host
sahfhfjkhjhfhjdhfjkdhfkjdhfjkhhdsjfhdfhjdhfkj/somethingelse`;
Expand Down

0 comments on commit 5675c49

Please sign in to comment.