Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update defect of of wrong resource attribute of "container.id" #1682

Merged
merged 6 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,29 @@
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);

Check warning on line 70 in detectors/node/opentelemetry-resource-detector-container/src/detectors/ContainerDetector.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L70 was not covered by tests
} 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;

Check warning on line 80 in detectors/node/opentelemetry-resource-detector-container/src/detectors/ContainerDetector.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L80 was not covered by tests
}
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