diff --git a/detectors/node/opentelemetry-resource-detector-container/src/detectors/ContainerDetector.ts b/detectors/node/opentelemetry-resource-detector-container/src/detectors/ContainerDetector.ts index 73b26f9ed4..78bffb7572 100644 --- a/detectors/node/opentelemetry-resource-detector-container/src/detectors/ContainerDetector.ts +++ b/detectors/node/opentelemetry-resource-detector-container/src/detectors/ContainerDetector.ts @@ -108,7 +108,7 @@ export class ContainerDetector implements Detector { private async _getContainerId(): Promise { try { return ( - (await this._getContainerIdV1()) ?? (await this._getContainerIdV2()) + (await this._getContainerIdV1()) || (await this._getContainerIdV2()) ); } catch (e) { if (e instanceof Error) { diff --git a/detectors/node/opentelemetry-resource-detector-container/test/ContainerDetector.test.ts b/detectors/node/opentelemetry-resource-detector-container/test/ContainerDetector.test.ts index 8ee03d1f36..71fbf1c5de 100644 --- a/detectors/node/opentelemetry-resource-detector-container/test/ContainerDetector.test.ts +++ b/detectors/node/opentelemetry-resource-detector-container/test/ContainerDetector.test.ts @@ -94,6 +94,16 @@ describe('ContainerDetector', () => { assert.ok(resource); }); + it('should return a correctCgroupV2Data resource with v1Detector returns empty string ', async () => { + readStub = sinon.stub(ContainerDetector, 'readFileAsync' as any); + sinon.stub(containerDetector, '_getContainerIdV1' as any).resolves(''); + sinon + .stub(containerDetector, '_getContainerIdV2' as any) + .resolves(correctCgroupV2Data); + const containerId = await containerDetector['_getContainerId'](); + assert.strictEqual(containerId, correctCgroupV2Data); + }); + it('should return a resource without attribute container.id when cgroup file does not contain valid Container ID', async () => { readStub = sinon .stub(ContainerDetector, 'readFileAsync' as any)