Skip to content

Commit

Permalink
fix: fix timeout in aws resource detector (#2186)
Browse files Browse the repository at this point in the history
* fix: fix timeout in aws resource detector

Current timeout is 1 sec, which is causing EC2 metadata api request timed out.
Increase the timeout to 5 sec.

* Updated the timeouts as per comments

---------

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
  • Loading branch information
arunp0 and pichlermarc authored Jun 13, 2024
1 parent caf7cb5 commit 9e4726c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class AwsEc2Detector implements Detector {
const timeoutId = setTimeout(() => {
req.abort();
reject(new Error('EC2 metadata api request timed out.'));
}, 1000);
}, this.MILLISECOND_TIME_OUT);

const req = http.request(options, res => {
clearTimeout(timeoutId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ describe('awsEc2Detector', () => {
scope.done();
});

it('should throw when timed out', async () => {
it('should throw when timed out', function (done) {
this.timeout(6000);
const expectedError = new Error('EC2 metadata api request timed out.');
const scope = nock(AWS_HOST)
.put(AWS_TOKEN_PATH)
Expand All @@ -120,17 +121,21 @@ describe('awsEc2Detector', () => {
.reply(200, () => mockedIdentityResponse)
.get(AWS_HOST_PATH)
.matchHeader(AWS_METADATA_TOKEN_HEADER, mockedTokenResponse)
.delayConnection(2000)
.delayConnection(5000)
.reply(200, () => mockedHostResponse);

try {
await awsEc2Detector.detect();
assert.ok(false, 'Expected to throw');
} catch (err) {
assert.deepStrictEqual(err, expectedError);
}

scope.done();
awsEc2Detector
.detect()
.then(() => {
assert.ok(false, 'Expected to throw');
})
.catch(err => {
assert.deepStrictEqual(err, expectedError);
})
.finally(() => {
scope.done();
done();
});
});

it('should throw when replied with an Error', async () => {
Expand Down

0 comments on commit 9e4726c

Please sign in to comment.