Skip to content

Commit ad560df

Browse files
sjinksdavid-luna
andauthored
fix(detector-container): properly detect container ID when using Podman (#2448)
Co-authored-by: David Luna <[email protected]>
1 parent d11efb3 commit ad560df

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class ContainerDetector implements DetectorSync {
3535
readonly DEFAULT_CGROUP_V2_PATH = '/proc/self/mountinfo';
3636
readonly UTF8_UNICODE = 'utf8';
3737
readonly HOSTNAME = 'hostname';
38-
readonly MARKING_PREFIX = 'containers';
38+
readonly MARKING_PREFIX = ['containers', 'overlay-containers'];
3939
readonly CRIO = 'crio-';
4040
readonly CRI_CONTAINERD = 'cri-containerd-';
4141
readonly DOCKER = 'docker-';
@@ -105,7 +105,7 @@ export class ContainerDetector implements DetectorSync {
105105
const strArray = str?.split('/') ?? [];
106106
for (let i = 0; i < strArray.length - 1; i++) {
107107
if (
108-
strArray[i] === this.MARKING_PREFIX &&
108+
this.MARKING_PREFIX.includes(strArray[i]) &&
109109
strArray[i + 1]?.length === this.CONTAINER_ID_LENGTH
110110
) {
111111
return strArray[i + 1];

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const DEFAULT_CGROUP_V1_PATH = '/proc/self/cgroup';
1818
export const DEFAULT_CGROUP_V2_PATH = '/proc/self/mountinfo';
1919
export const UTF8_UNICODE = 'utf8';
2020
export const HOSTNAME = 'hostname';
21-
export const MARKING_PREFIX = 'containers';
21+
export const MARKING_PREFIX = ['containers', 'overlay-containers'];
2222
export const CRIO = 'crio-';
2323
export const CRI_CONTAINERD = 'cri-containerd-';
2424
export const DOCKER = 'docker-';

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ describe('ContainerDetector', () => {
3232
const correctCgroupV2Data = `containers/tmhdefghijklmnopqrstuvwxyzafgrefghiugkmnopqrstuvwxyzabcdefghijkl/hostname
3333
fhkjdshgfhsdfjhdsfkjhfkdshkjhfd/host
3434
sahfhfjkhjhfhjdhfjkdhfkjdhfjkhhdsjfhdfhjdhfkj/somethingelse`;
35+
const correctCgroupV2PodmanData =
36+
'4245 4237 0:94 /containers/overlay-containers/4e9dc37d00ebd2daea029d84bb37764ce12d746a6f3a33c5969cee15c4fc4418/userdata/hostname /etc/hostname rw - tmpfs tmpfs rw';
3537

3638
const wrongCgroupV2Data =
3739
'bcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm/wrongkeyword';
@@ -85,6 +87,22 @@ describe('ContainerDetector', () => {
8587
});
8688
});
8789

90+
it('should return a resource with container ID with a valid container ID present for v2 (Podman)', async () => {
91+
readStub = sinon.stub(ContainerDetector, 'readFileAsync' as any);
92+
93+
readStub.onFirstCall().resolves('');
94+
readStub.onSecondCall().resolves(correctCgroupV2PodmanData);
95+
96+
const resource = containerDetector.detect();
97+
await resource.waitForAsyncAttributes?.();
98+
sinon.assert.calledTwice(readStub);
99+
100+
assert.ok(resource);
101+
assertContainerResource(resource, {
102+
id: '4e9dc37d00ebd2daea029d84bb37764ce12d746a6f3a33c5969cee15c4fc4418',
103+
});
104+
});
105+
88106
it('should return a empty resource with failed hostname check for v2', async () => {
89107
readStub = sinon.stub(ContainerDetector, 'readFileAsync' as any);
90108

0 commit comments

Comments
 (0)