From 9761a9160c862b9f8a35c00afc26217973e4462a Mon Sep 17 00:00:00 2001 From: Abhinav Mathur Date: Mon, 10 Jun 2024 07:10:54 -0400 Subject: [PATCH] fix: ContainerId not detected post version 0.2.2 (#2270) * ?? only checks for null and undefined * added unit test to cover the fix * added unit test to cover the fix * lint fixes * lint fixes - line too long --- .../src/detectors/ContainerDetector.ts | 2 +- .../test/ContainerDetector.test.ts | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) 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)