Skip to content

Commit 9761a91

Browse files
authored
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
1 parent 3e863cf commit 9761a91

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export class ContainerDetector implements Detector {
108108
private async _getContainerId(): Promise<string | undefined> {
109109
try {
110110
return (
111-
(await this._getContainerIdV1()) ?? (await this._getContainerIdV2())
111+
(await this._getContainerIdV1()) || (await this._getContainerIdV2())
112112
);
113113
} catch (e) {
114114
if (e instanceof Error) {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ describe('ContainerDetector', () => {
9494
assert.ok(resource);
9595
});
9696

97+
it('should return a correctCgroupV2Data resource with v1Detector returns empty string ', async () => {
98+
readStub = sinon.stub(ContainerDetector, 'readFileAsync' as any);
99+
sinon.stub(containerDetector, '_getContainerIdV1' as any).resolves('');
100+
sinon
101+
.stub(containerDetector, '_getContainerIdV2' as any)
102+
.resolves(correctCgroupV2Data);
103+
const containerId = await containerDetector['_getContainerId']();
104+
assert.strictEqual(containerId, correctCgroupV2Data);
105+
});
106+
97107
it('should return a resource without attribute container.id when cgroup file does not contain valid Container ID', async () => {
98108
readStub = sinon
99109
.stub(ContainerDetector, 'readFileAsync' as any)

0 commit comments

Comments
 (0)