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

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

merged 1 commit into from
Sep 27, 2024

Conversation

roy-dydx
Copy link
Contributor

@roy-dydx roy-dydx commented Sep 27, 2024

Changelist

[Describe or list the changes made in this PR]

Test Plan

[Describe how this PR was tested (if applicable)]

Author/Reviewer Checklist

  • If this PR has changes that result in a different app state given the same prior state and transaction list, manually add the state-breaking label.
  • If the PR has breaking postgres changes to the indexer add the indexer-postgres-breaking label.
  • If this PR isn't state-breaking but has changes that modify behavior in PrepareProposal or ProcessProposal, manually add the label proposal-breaking.
  • If this PR is one of many that implement a specific feature, manually label them all feature:[feature-name].
  • If you wish to for mergify-bot to automatically create a PR to backport your change to a release branch, manually add the label backport/[branch-name].
  • Manually add any of the following labels: refactor, chore, bug.

Summary by CodeRabbit

  • New Features
    • Introduced a new function to retrieve the availability zone ID for AWS ECS containers.
    • Added a new configuration property for specifying the AWS region.
    • Expanded export capabilities to include new functionalities from the az-id module.
  • Dependencies
    • Added a new dependency for AWS EC2 client operations.

@roy-dydx roy-dydx marked this pull request as ready for review September 27, 2024 18:19
@roy-dydx roy-dydx requested a review from a team as a code owner September 27, 2024 18:19
Copy link
Contributor

coderabbitai bot commented Sep 27, 2024

Walkthrough

The changes introduce a new dependency on the AWS SDK for EC2, add a function to retrieve availability zone IDs for AWS ECS containers, and expand the configuration schema to include the AWS region. Additionally, the module exports have been updated to include the new functionality from the az-id module, enhancing the project's ability to interact with AWS services.

Changes

Files Change Summary
indexer/packages/base/package.json Added dependency: @aws-sdk/client-ec2 with version ^3.354.0.
indexer/packages/base/src/az-id.ts Introduced async function getAvailabilityZoneId() to retrieve the availability zone ID for an AWS ECS container, including error handling for metadata retrieval and EC2 command execution.
indexer/packages/base/src/config.ts Added new configuration property AWS_REGION to baseConfigSchema, defined as a string with a default value of an empty string.
indexer/packages/base/src/index.ts Added export statement export * from './az-id'; to include all entities from the az-id module.

Sequence Diagram(s)

sequenceDiagram
    participant App
    participant ECS
    participant EC2

    App->>ECS: Check environment variables
    ECS-->>App: Return metadata URI and region
    App->>ECS: Construct metadata URL
    App->>ECS: Fetch availability zone name
    ECS-->>App: Return zone name
    App->>EC2: Describe availability zones
    EC2-->>App: Return zone ID
    App-->>ECS: Log and return zone ID
Loading

Poem

In the meadow, a rabbit hops with glee,
New changes flutter like leaves on a tree.
With AWS magic, we reach for the sky,
Availability zones, oh my, oh my!
Configs now sparkle, dependencies grow,
Let’s dance in the code, let our features flow! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Outside diff range and nitpick comments (3)
indexer/packages/base/src/config.ts (1)

42-42: LGTM! Consider adding documentation and evaluating requirements.

The addition of the AWS_REGION property is consistent with the existing schema structure and uses appropriate parsing.

Consider the following improvements:

  1. Add a comment explaining the purpose and usage of this property.
  2. Evaluate if this property should be required in certain environments (e.g., PRODUCTION, STAGING).
  3. Consider adding validation for the AWS region format if necessary.

Example implementation with improvements:

  // AWS region for services requiring AWS SDK integration
  AWS_REGION: parseString({
    default: '',
    requireInEnv: [NodeEnv.PRODUCTION, NodeEnv.STAGING],
    validate: (value) => /^[a-z]{2}-[a-z]+-\d$/.test(value) || value === '',
  }),
indexer/packages/base/src/az-id.ts (2)

8-8: Use truthiness checks for environment variables

Instead of comparing environment variables to empty strings, consider using truthiness checks to handle cases where the variables might be undefined or null.

Modify the condition as follows:

- if (config.ECS_CONTAINER_METADATA_URI_V4 !== '' && config.AWS_REGION !== '') {
+ if (config.ECS_CONTAINER_METADATA_URI_V4 && config.AWS_REGION) {

7-51: Consider adding unit tests for the new function

The getAvailabilityZoneId function interacts with AWS services and handles multiple error scenarios. To ensure its reliability and correct behavior, especially when dependencies fail or environment variables are missing, consider adding unit tests that cover:

  • Successful retrieval of the availability zone ID.
  • Handling of missing or empty environment variables.
  • Responses when AWS services return errors.
  • Behavior when AvailabilityZones is undefined or empty.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 7193a10 and 746258e.

⛔ Files ignored due to path filters (1)
  • indexer/pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (4)
  • indexer/packages/base/package.json (1 hunks)
  • indexer/packages/base/src/az-id.ts (1 hunks)
  • indexer/packages/base/src/config.ts (1 hunks)
  • indexer/packages/base/src/index.ts (1 hunks)
🔇 Additional comments (2)
indexer/packages/base/src/index.ts (1)

17-17: LGTM! Verify the az-id module.

The new export statement is correctly added and aligns with the PR objective of adding a function to fetch the availability zone ID.

Let's verify the existence and content of the az-id module:

This script will help us confirm that the az-id module exists and contains the expected functionality for fetching the availability zone ID.

indexer/packages/base/package.json (1)

35-35: Consider the implications of adding the AWS SDK dependency.

The addition of @aws-sdk/client-ec2 aligns with the PR objective of fetching the availability zone ID. However, please consider the following points:

  1. Impact on project size and build time: The AWS SDK is a large dependency. Ensure that this doesn't significantly increase your build times or bundle size.
  2. Security: As with any new dependency, be aware of potential security implications. Regularly update this package and monitor for any security advisories.
  3. Scope: Consider if this dependency is necessary for the entire project. If it's only used in a specific module, you might want to move it to a more localized package.json to avoid unnecessary imports in other parts of the project.

To verify the necessity and usage of this new dependency, you can run the following script:

This will help ensure that the dependency is actually being used in the codebase and identify where it's being imported.

✅ Verification successful

Adjusting the search commands to target specific file extensions.

The previous scripts encountered errors with the --type flag. Let's try searching by file extensions instead:


Dependency @aws-sdk/client-ec2 Usage Confirmed

The @aws-sdk/client-ec2 package is actively used in src/az-id.ts for fetching the availability zone ID, aligning with the PR objectives. The usage is appropriately localized within the base package, ensuring no unnecessary imports elsewhere in the project.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for usage of @aws-sdk/client-ec2 in the codebase

# Test: Search for imports of the EC2 client
echo "Searching for EC2 client imports:"
rg --type typescript --type javascript "from\s+['\"]@aws-sdk/client-ec2['\"]" -g '!node_modules'

# Test: Search for any other usage of the EC2 namespace
echo "Searching for other EC2 namespace usage:"
rg --type typescript --type javascript "EC2" -g '!node_modules' -g '!*.json'

Length of output: 417


Script:

# Searching for EC2 client imports in .ts, .tsx, .js, .jsx files
rg "from\s+['\"]@aws-sdk/client-ec2['\"]" -g "*.ts" -g "*.tsx" -g "*.js" -g "*.jsx" -g "!node_modules"

# Searching for EC2 namespace usage in .ts, .tsx, .js, .jsx files
rg "EC2" -g "*.ts" -g "*.tsx" -g "*.js" -g "*.jsx" -g "!node_modules" -g "!*.json"

Length of output: 633

} 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. ',

} 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.',

} 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.',

Comment on lines +21 to +26
const zoneId = ec2Response.AvailabilityZones![0].ZoneId!;
logger.info({
at: 'az-id#getAvailabilityZoneId',
message: `Got availability zone id ${zoneId}.`,
});
return ec2Response.AvailabilityZones![0].ZoneId!;
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 '';
}

@roy-dydx roy-dydx merged commit 5533a8f into main Sep 27, 2024
16 checks passed
@roy-dydx roy-dydx deleted the roy/r1 branch September 27, 2024 18:44
@roy-dydx
Copy link
Contributor Author

https://github.com/Mergifyio release/indexer/v5.x

@roy-dydx
Copy link
Contributor Author

https://github.com/Mergifyio release/indexer/v7.x

Copy link
Contributor

mergify bot commented Sep 30, 2024

release /indexer/v5.x

❌ Sorry but I didn't understand the command. Please consult the commands documentation 📚.

Copy link
Contributor

mergify bot commented Sep 30, 2024

release /indexer/v7.x

❌ Sorry but I didn't understand the command. Please consult the commands documentation 📚.

@roy-dydx
Copy link
Contributor Author

https://github.com/Mergifyio backport release/indexer/v5.x

Copy link
Contributor

mergify bot commented Sep 30, 2024

backport release/indexer/v5.x

🛑 Command backport release/indexer/v5.x cancelled because of a new backport command with different arguments

@roy-dydx
Copy link
Contributor Author

https://github.com/Mergifyio backport release/indexer/v5.x

Copy link
Contributor

mergify bot commented Sep 30, 2024

backport release/indexer/v5.x

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Sep 30, 2024
(cherry picked from commit 5533a8f)

# Conflicts:
#	indexer/pnpm-lock.yaml
@roy-dydx
Copy link
Contributor Author

https://github.com/Mergifyio backport release/indexer/v7.x

Copy link
Contributor

mergify bot commented Sep 30, 2024

backport release/indexer/v7.x

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Sep 30, 2024
roy-dydx added a commit that referenced this pull request Sep 30, 2024
…2410)

Co-authored-by: roy-dydx <133032749+roy-dydx@users.noreply.github.com>
roy-dydx added a commit that referenced this pull request Sep 30, 2024
…2409)

Co-authored-by: roy-dydx <133032749+roy-dydx@users.noreply.github.com>
Co-authored-by: Roy Li <roy@dydx.exchange>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

2 participants