Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to fetch availability zone id (#2326) #2390

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions indexer/packages/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"homepage": "https://github.com/dydxprotocol/indexer#readme",
"dependencies": {
"@aws-sdk/client-ec2": "^3.354.0",
"axios": "^1.2.1",
"big.js": "^6.2.1",
"bignumber.js": "^9.0.2",
Expand Down
51 changes: 51 additions & 0 deletions indexer/packages/base/src/az-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { DescribeAvailabilityZonesCommand, EC2Client } from '@aws-sdk/client-ec2';

import { axiosRequest } from './axios';
import config from './config';
import logger from './logger';

export async function getAvailabilityZoneId(): Promise<string> {
if (config.ECS_CONTAINER_METADATA_URI_V4 !== '' && config.AWS_REGION !== '') {
const taskUrl = `${config.ECS_CONTAINER_METADATA_URI_V4}/task`;
try {
const response = await axiosRequest({
method: 'GET',
url: taskUrl,
}) as { AvailabilityZone: string };
const client = new EC2Client({ region: config.AWS_REGION });
const command = new DescribeAvailabilityZonesCommand({
ZoneNames: [response.AvailabilityZone],
});
try {
const ec2Response = await client.send(command);
const zoneId = ec2Response.AvailabilityZones![0].ZoneId!;
logger.info({
at: 'az-id#getAvailabilityZoneId',
message: `Got availability zone id ${zoneId}.`,
});
return ec2Response.AvailabilityZones![0].ZoneId!;
Comment on lines +21 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ensure AvailabilityZones data is valid before accessing

The code assumes that ec2Response.AvailabilityZones is defined and contains at least one element. Accessing ec2Response.AvailabilityZones![0].ZoneId! without proper checks may lead to runtime errors if the array is undefined or empty. Please add a check to confirm that ec2Response.AvailabilityZones exists and has at least one element before accessing it.

Consider modifying the code as follows:

- const zoneId = ec2Response.AvailabilityZones![0].ZoneId!;
- logger.info({
-   at: 'az-id#getAvailabilityZoneId',
-   message: `Got availability zone id ${zoneId}.`,
- });
- return ec2Response.AvailabilityZones![0].ZoneId!;
+ if (
+   ec2Response.AvailabilityZones &&
+   ec2Response.AvailabilityZones.length > 0 &&
+   ec2Response.AvailabilityZones[0].ZoneId
+ ) {
+   const zoneId = ec2Response.AvailabilityZones[0].ZoneId;
+   logger.info({
+     at: 'az-id#getAvailabilityZoneId',
+     message: `Got availability zone id ${zoneId}.`,
+   });
+   return zoneId;
+ } else {
+   logger.error({
+     at: 'az-id#getAvailabilityZoneId',
+     message: 'AvailabilityZones is undefined or empty.',
+   });
+   return '';
+ }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const zoneId = ec2Response.AvailabilityZones![0].ZoneId!;
logger.info({
at: 'az-id#getAvailabilityZoneId',
message: `Got availability zone id ${zoneId}.`,
});
return ec2Response.AvailabilityZones![0].ZoneId!;
if (
ec2Response.AvailabilityZones &&
ec2Response.AvailabilityZones.length > 0 &&
ec2Response.AvailabilityZones[0].ZoneId
) {
const zoneId = ec2Response.AvailabilityZones[0].ZoneId;
logger.info({
at: 'az-id#getAvailabilityZoneId',
message: `Got availability zone id ${zoneId}.`,
});
return zoneId;
} else {
logger.error({
at: 'az-id#getAvailabilityZoneId',
message: 'AvailabilityZones is undefined or empty.',
});
return '';
}

} catch (error) {
logger.error({
at: 'az-id#getAvailabilityZoneId',
message: 'Failed to fetch availabilty zone id from EC2. ',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Typographical error in log message

There's a typo in the log message: "availabilty" should be "availability".

Apply the following diff to fix the typo:

- message: 'Failed to fetch availabilty zone id from EC2. ',
+ message: 'Failed to fetch availability zone id from EC2. ',
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
message: 'Failed to fetch availabilty zone id from EC2. ',
message: 'Failed to fetch availability zone id from EC2. ',

error,
});
return '';
}
} catch (error) {
logger.error({
at: 'az-id#getAvailabilityZoneId',
message: 'Failed to retrieve availability zone from metadata endpoint. No availabilty zone id found.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Typographical error in log message

There's a typo in the log message: "availabilty" should be "availability".

Apply the following diff to fix the typo:

- message: 'Failed to retrieve availability zone from metadata endpoint. No availabilty zone id found.',
+ message: 'Failed to retrieve availability zone from metadata endpoint. No availability zone id found.',
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
message: 'Failed to retrieve availability zone from metadata endpoint. No availabilty zone id found.',
message: 'Failed to retrieve availability zone from metadata endpoint. No availability zone id found.',

error,
taskUrl,
});
return '';
}
} else {
logger.error({
at: 'az-id#getAvailabilityZoneId',
message: 'No metadata URI or region. No availabilty zone id found.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Typographical error in log message

There's a typo in the log message: "availabilty" should be "availability".

Apply the following diff to fix the typo:

- message: 'No metadata URI or region. No availabilty zone id found.',
+ message: 'No metadata URI or region. No availability zone id found.',
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
message: 'No metadata URI or region. No availabilty zone id found.',
message: 'No metadata URI or region. No availability zone id found.',

});
return '';
}
}
1 change: 1 addition & 0 deletions indexer/packages/base/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const baseConfigSchema = {
STATSD_PORT: parseInteger({ default: 8125 }),
LOG_LEVEL: parseString({ default: 'debug' }),
ECS_CONTAINER_METADATA_URI_V4: parseString({ default: '' }),
AWS_REGION: parseString({ default: '' }),
};

export default parseSchema(baseConfigSchema);
1 change: 1 addition & 0 deletions indexer/packages/base/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export * from './bugsnag';
export * from './stats-util';
export * from './date-helpers';
export * from './instance-id';
export * from './az-id';

// Do this outside logger.ts to avoid a dependency cycle with logger transports that may trigger
// additional logging.
Expand Down
Loading
Loading