-
Notifications
You must be signed in to change notification settings - Fork 845
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
feat(core): add more scalable replacements for getEnv(), getEnvWithoutDefaults() #5443
feat(core): add more scalable replacements for getEnv(), getEnvWithoutDefaults() #5443
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #5443 +/- ##
==========================================
+ Coverage 94.77% 94.79% +0.02%
==========================================
Files 309 309
Lines 7967 7998 +31
Branches 1678 1687 +9
==========================================
+ Hits 7551 7582 +31
Misses 416 416
|
export function getNumberFromEnv(key: string): number | undefined { | ||
const raw = process.env[key]?.trim(); | ||
if (raw == null || raw === '') { | ||
return undefined; | ||
} | ||
|
||
const value = Number(raw); | ||
if (isNaN(value)) { | ||
diag.warn( | ||
`Unknown value ${raw} for ${key}, expected a number, using defaults` | ||
); | ||
return undefined; | ||
} | ||
|
||
return value; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note for reviewers: I've left out the sub-classifications of integer
, duration
and timeout
from the spec on purpose for now. These are mostly implemented in the consuming packages at the moment.
We can add these additional functions later if we see that we re-implement the same thing over and over again.
export function getStringListFromEnv(key: string): string[] | undefined { | ||
return getStringFromEnv(key) | ||
?.split(',') | ||
.map(v => v.trim()) | ||
.filter(s => s !== ''); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note for reviewers: this is not in the spec but it has proven useful in quite a few places OTEL_PROPAGATORS
, OTEL_SEMCONV_STABILITY_OPT_IN
, ...
For contrib packages to get a release that uses the replacements, we'd need to backport the new |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with some nits.
The longer I thought about trimming all string values the more I became clear to me that it's likely not a good idea. I think keeping whitespace is more in line with what the user expects, in line with |
cc @trentm if you want to have another look: bd5ca62 did change the behavior from trimming whitespace when retrieving strings to not trimming, see the comment above for the reasoning behind the change |
I don't think that'd be necessary - I've had a quick look and I think the changes would be fairly simple to do. The packages there are all intended for Node.js and only get strings. They are also all non-otel env vars (with the exception of I'll open a few PRs to take care of those :) Edit: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stamping, but again with a question.
Which problem is this PR solving?
See #5217
Currently we're accepting "environment variable-like" configuration even in the browser. This makes little sense IMO, since the SDK has to be configured in code anyway, so we're just offering duplicate ways to configure an SDK. Removing that feature, however, would be a breaking change - which is why #5217 is scheduled for SDK 2.0.
This PR lays the foundation for that by adding an easy replacement for
getEnv()
andgetEnvWithoutDefaults()
. It takes the env var name directly and no-ops in the browser. Once these replacements are used, we can release SDK 2.0 and code-changes to remove usages from the browser versions of the new SDK completely in a refactor.Further, we currently have a problem with scaling
getEnv()
and the associatedENVIRONMENT
type any further. For any new experimental feature, we have to add something to the stableENVIRONMENT
type, which is introduces friction during development as a stable type is modified for an experimental features.Most environment variables are only used once. At the moment, everyone that uses
getEnv()
has to import and parse all environment variables, even the ones they don't care about. The properties fromENVIRONMENT
can also not be minified, nor tree-shaken - similar to what was happening with@opentelemetry/semantic-conventions
but at a smaller scale.This PR introduces new functions that map to the spec's idea of which types of env vars exist. It leaves some out, but the one's I've left out are mostly convenience functions that we can add later. Base functionality can be established with just getting
string
,number
,boolean
andstring[]
from environment variables.With the proposed change here
OTEL_SEMCONV_STABILITY_OPT_IN
could be exported from@opentelemetry/instrumentation
, for instance)@opentelemetry/core
package anymoregetEnv()
andgetEnvWithoutDefaults()
,ENVIRONMENT
,DEFAULT_ENVIRONMENT
,RAW_ENVIRONMENT
, et al. in a follow-upFollow-ups
After this is merged, I will
getEnv()
et al from@opentelemetry/core
Type of change
How Has This Been Tested?
getEnv()
andgetEnvWithoutDefaults()
with the new functions in draft feat(core): introduce replacement functions for getEnv, getEnvWithoutDefaults #5429 to show that this works