Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
add timeout for AWS MetadataAccessor (#318)
Browse files Browse the repository at this point in the history
* add timeout for awsMetadataAccessor

* style fix
  • Loading branch information
mayurkale22 authored Jan 31, 2019
1 parent 99c04ee commit 54d46ea
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/opencensus-resource-util/src/resource-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@ export async function getAwsEC2Resource(): Promise<Resource> {
*/
async function awsMetadataAccessor<T>(): Promise<T> {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
reject(new Error('EC2 metadata api request timed out.'));
}, 2000);

const req = http.get(AWS_INSTANCE_IDENTITY_DOCUMENT_URI, (res) => {
clearTimeout(timeoutId);
const {statusCode} = res;
res.setEncoding('utf8');
let rawData = '';
Expand All @@ -142,7 +147,10 @@ async function awsMetadataAccessor<T>(): Promise<T> {
}
});
});
req.on('error', (err) => reject(err));
req.on('error', (err) => {
clearTimeout(timeoutId);
reject(err);
});
});
}

Expand Down

0 comments on commit 54d46ea

Please sign in to comment.