Skip to content

Conversation

uttam282005
Copy link
Contributor

@uttam282005 uttam282005 commented Jul 19, 2025

  • Removed EnvironmentVariablesService class.
  • Replaced repeated environment variable reads with a single #envConfig object populated once during setOptions().
  • Updated Metrics.ts to use #envConfig for values like namespace, serviceName, functionName, etc.
  • Removed all references to envVarsService, including getEnvVarsService() and setEnvVarsService() methods.

This refactor improves runtime performance by avoiding redundant environment variable reads and follows a cleaner, more functional approach. It also aligns with the setOptions() pattern for centralized config management and eliminates ~50 lines of unnecessary code.


Summary

What’s being changed?

This PR removes the verbose class-based EnvironmentVariablesService and replaces it with a private, cached #envConfig object in the Metrics class. The object is populated once during initialization via the setOptions() method using functional utilities from @aws-lambda-powertools/commons/utils/env.

Why?

  • Performance: Avoids repeated environment variable lookups at runtime.
  • Code quality: Cleaner, more maintainable, and aligns with existing config patterns.
  • Consistency: Follows the tenets of making things explicit, safe, and minimal.

Benefits

  • Improved runtime efficiency.
  • Simplified implementation.
  • Easier to reason about and maintain.

#4136


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label Jul 19, 2025
@boring-cyborg boring-cyborg bot added the metrics This item relates to the Metrics Utility label Jul 19, 2025
Copy link

boring-cyborg bot commented Jul 19, 2025

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #typescript channel on our Powertools for AWS Lambda Discord: Invite link

@uttam282005
Copy link
Contributor Author

@svozza you are right. It's not actually needed as inferred type of the whole expression is string | undefined which is compatible with the type of namespace.
I will remove it.

@dreamorosi
Copy link
Contributor

No acknowledgement section found. Please make sure you used the template to open a PR and didn't remove the acknowledgment section. Check the template at .github/PULL_REQUEST_TEMPLATE.md#acknowledgment

Hi @uttam282005 thank you for your contribution - please make sure to review the acknowledgment section here and paste it in the PR body as-is and without modifications if you agree with its text.

We won't be able to merge the PR otherwise. Thank you!

Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

A couple small changes to maintain full parsing of POWERTOOLS_METRICS_DISABLED and remove non-null assertions from the #envConfig.

Copy link

@uttam282005
Copy link
Contributor Author

@dreamorosi I’ve implemented the suggested changes. Please let me know if there’s anything else that needs to be addressed.

@dreamorosi dreamorosi requested review from dreamorosi and svozza July 21, 2025 12:11
@dreamorosi dreamorosi merged commit 919063b into aws-powertools:main Jul 21, 2025
46 checks passed
Copy link

boring-cyborg bot commented Jul 21, 2025

Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience!

Copy link
Contributor

@aws-powertools/lambda-typescript No related issues found. Please ensure 'pending-release' label is applied before releasing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
metrics This item relates to the Metrics Utility size/L PRs between 100-499 LOC
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Maintenance: replace EnvironmentVariablesService class with helper functions in Metrics
3 participants