-
Notifications
You must be signed in to change notification settings - Fork 156
feat(commons): environment variable helpers #3945
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
svozza
reviewed
May 22, 2025
svozza
approved these changes
May 22, 2025
|
am29d
approved these changes
May 22, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
commons
This item relates to the Commons Utility
dependencies
Changes that touch dependencies, e.g. Dependabot, etc.
feature
PRs that introduce new features or minor changes
size/XL
PRs between 500-999 LOC, often PRs that grown with feedback
tests
PRs that add or change tests
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Changes
This PR adds new zero-dependency functional utilities to read and parse environment variables.
Customers can read and cast environment variables into
string
,number
, andboolean
using the respective helpers.By default, the environment variable is considered required, and the function will throw an error if not present.
Customers can customize the error message thrown by the helper by passing an optional
errorMessage
parameter.Alternatively, if the env variable is optional, they can also provide a default fallback value that will be used if the env variable is not present.
The PR also adds a few higher-level utilities to read slightly more complex values from environment variables.
For example,
getTruthyBooleanFromEnv
andgetFalsyBooleanFromEnv
respectively allow customers to read values like1
,y
,yes
,on
,true
,0
,n
,no
,off
,false
and cast them to their correspondingboolean
values.Likewise, the
isDevMode
helper allows customers to detect if their application is running in a development environment based on thePOWERTOOLS_DEV
env variable, and thegetServiceName
one retrieves the value of thePOWERTOOLS_SERVICE_NAME
one.Finally, the
getXrayTraceDataFromEnv
,isRequestXRaySampled
, andgetXRayTraceIdFromEnv
all read the_X_AMZN_TRACE_ID
env variable and extract/return different kind of information from it.While this work comes from a feature request (#3159) most of the advantage to customers will come in the form of smaller utilities over time, since we'll be able to use these helpers across other Powertools for AWS utilities and replace the existing
EnvironmentVariablesService
class-based model which is extremely verbose and requires props drilling if you want to access env variables deep into an inheritance stack.For Logger, Tracer, and Metrics we'll migrate to these utilities in the next major version while for other utilities we'll evaluate on a case-by-case basis. New utilities should use these helpers since the beginning.
Issue number: closes #3159
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.