Skip to content

Commit 75e3471

Browse files
committed
feat(aws-ecs-detector): improve container ID validation and test coverage
1 parent 1f31841 commit 75e3471

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

packages/resource-detector-aws/src/detectors/AwsEcsDetector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export class AwsEcsDetector implements ResourceDetector {
192192
ecsMatch &&
193193
ecsMatch[1] &&
194194
ecsMatch[1].length >= 12 &&
195-
ecsMatch[1].length <= 128
195+
ecsMatch[1].length <= AwsEcsDetector.CONTAINER_ID_LENGTH
196196
) {
197197
return ecsMatch[1];
198198
}
@@ -204,7 +204,7 @@ export class AwsEcsDetector implements ResourceDetector {
204204
if (
205205
lastSegment &&
206206
lastSegment.length >= 12 &&
207-
lastSegment.length <= 128
207+
lastSegment.length <= AwsEcsDetector.CONTAINER_ID_LENGTH
208208
) {
209209
return lastSegment;
210210
}

packages/resource-detector-aws/test/detectors/AwsEcsDetector.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,5 +532,60 @@ describe('AwsEcsResourceDetector', () => {
532532

533533
nockScope.done();
534534
});
535+
536+
it('should extract container ID from Docker format cgroup', async () => {
537+
const dockerContainerId = 'a4d00c9dd675d67f866c786181419e1b44832d4696780152e61afd44a3e02856';
538+
const cgroupData = `1:blkio:/docker/${dockerContainerId}
539+
2:cpu:/docker/${dockerContainerId}
540+
3:cpuacct:/docker/${dockerContainerId}`;
541+
542+
setupMocks(cgroupData);
543+
const nockScope = setupMetadataNock();
544+
545+
const resource = detectResources({ detectors: [awsEcsDetector] });
546+
await resource.waitForAsyncAttributes?.();
547+
548+
sinon.assert.calledOnce(readStub);
549+
assert.ok(resource);
550+
assertEcsResource(resource, {});
551+
assertContainerResource(resource, {
552+
name: testHostname,
553+
id: dockerContainerId,
554+
});
555+
556+
nockScope.done();
557+
});
558+
559+
it('should extract container ID from mixed ECS and Docker format cgroup', async () => {
560+
const taskId = '447438d8540d49dca93b4f0a488ebe90';
561+
const containerId = `${taskId}-1364044452`;
562+
const cgroupData = `11:memory:/ecs/${taskId}/${containerId}
563+
10:devices:/ecs/${taskId}/${containerId}
564+
9:freezer:/ecs/${taskId}/${containerId}
565+
8:blkio:/ecs/${taskId}/${containerId}
566+
7:perf_event:/ecs/${taskId}/${containerId}
567+
6:net_cls,net_prio:/ecs/${taskId}/${containerId}
568+
5:cpuset:/ecs/${taskId}/${containerId}
569+
4:pids:/ecs/${taskId}/${containerId}
570+
3:hugetlb:/ecs/${taskId}/${containerId}
571+
2:cpu,cpuacct:/ecs/${taskId}/${containerId}
572+
1:name=systemd:/ecs/${taskId}/${containerId}`;
573+
574+
setupMocks(cgroupData);
575+
const nockScope = setupMetadataNock();
576+
577+
const resource = detectResources({ detectors: [awsEcsDetector] });
578+
await resource.waitForAsyncAttributes?.();
579+
580+
sinon.assert.calledOnce(readStub);
581+
assert.ok(resource);
582+
assertEcsResource(resource, {});
583+
assertContainerResource(resource, {
584+
name: testHostname,
585+
id: containerId,
586+
});
587+
588+
nockScope.done();
589+
});
535590
});
536591
});

0 commit comments

Comments
 (0)