-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Support env substitution in configuration file if variable not set #4458
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
Conversation
Codecov Report
@@ Coverage Diff @@
## main #4458 +/- ##
==========================================
+ Coverage 90.69% 90.70% +0.01%
==========================================
Files 178 178
Lines 10356 10361 +5
==========================================
+ Hits 9392 9398 +6
+ Misses 746 745 -1
Partials 218 218
Continue to review full report at Codecov.
|
config/configmapprovider/expand.go
Outdated
@@ -86,6 +87,10 @@ func expandEnv(s string) string { | |||
if str == "$" { | |||
return "$" | |||
} | |||
return os.Getenv(str) | |||
pair := strings.Split(str, ":") | |||
if envValue := os.Getenv(pair[0]); envValue != "" || len(pair) == 1 { |
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.
Getenv
doesn't distinguish between an empty value and an unset variable, we probably want to use LookupEnv
instead, so that people can set an empty value if needed
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.
ACK. Changed to LookupEnv
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.
This needs a release note on the changelog
@@ -1,5 +1,12 @@ | |||
test_map: | |||
extra: "$EXTRA" | |||
env: | |||
recv.1: "$ENV_VALUE_1" | |||
recv.2: "${ENV_VALUE_2:default value for 2 not substituted}" |
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.
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.
Noted. I have changed the syntax to ${parameter:-word}
config/configmapprovider/expand.go
Outdated
@@ -86,6 +87,10 @@ func expandEnv(s string) string { | |||
if str == "$" { | |||
return "$" | |||
} | |||
return os.Getenv(str) | |||
pair := strings.Split(str, ":") |
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.
On ${ENV:a:b}
if ENV
is unset it will be replaced by a
instead of a:b
. You need to use SplitN
instead with n
equals 1 2, to split only by the first separator :
. I would add this as a testcase too.
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.
Good catch. Now using SplitN
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.
This looks good to me and I believe it's easier to accept for maintainers since it doesn't rely on an external dependency and the logic is simple enough. I left a small comment about the changelog wording
Co-authored-by: Pablo Baeyens <[email protected]>
@mx-psi thanks for the review :) but if you want to make everything nice and consistent I want you to think also about:
So a TL;DR users asks us to expend more than just envvar and we need to have a "syntax" consistent for all these sources. |
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.
I am currently working on the first part of #4190 (config_sources
). After that I will work on value_source
support, which is a more generalized case of env variable substitution. Until that is done it is best not to add further features to env variables otherwise we may end up with env variables being unsupportable special case. I am blocking this to avoid that.
Thanks for the links @bogdandrutu and @tigrannajaryan, it's useful context. I don't have the full context on the final look of config sources, so I wasn't aware about this interfering with these efforts. It may be useful for contributors to point to this on the issue linked (and if config sources work makes certain parts of the Collector to be on a "feature freeze" phase, it may be useful to explicitly document this somewhere too). |
Update: I am not actively looking into this right now. @bogdandrutu will probably take it over. I will remove by block so that you don't need to wait for me. |
This PR was marked stale due to lack of activity. It will be closed in 7 days. |
This PR was marked stale due to lack of activity. It will be closed in 7 days. |
This PR was marked stale due to lack of activity. It will be closed in 14 days. |
@@ -6,6 +6,8 @@ | |||
|
|||
- Remove `pdata.AttributeMap.InitFromMap` (#4429) | |||
|
|||
## 💡 Enhancements 💡 | |||
- Support shell-like syntax for environment variable substitution defaults (`${ENV:-default}`) (#4458) |
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.
Food for thought: We are going towards a direction of using "URI" like locations/identifiers for configs. I do understand that this is natural for lots of the people, but we should consider something like "env:ENV?default=foo" or in this direction.
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.
It feels unnatural to have $ENV
when there is no default and $env:ENV?default=foo
when there is a default; is that what we would be aiming for?
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.
I am proposing ${env:ENV}
and ${env:ENV?default=foo}
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.
That seems acceptable to me, it's close to PowerShell's syntax so it's not completely new. It will be a big breaking change, for end-users, though.
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.
I think for few versions (4-5) we will still support the old way. We are not going to just break the users :)
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.
Sounds like a sensible plan to me!
This PR was marked stale due to lack of activity. It will be closed in 14 days. |
Closed as inactive. Feel free to reopen if this PR is still being worked on. |
See #2534 (comment) for a workaround |
Description:
FEAT: Support for expanding environment variable in case it is not set.
The basic syntax is:
${ENV_VAR:-DEFAULT_VALUE}
Link to tracking Issue: #4384
Testing: Unit Tests
This PR is similar to #4387, instead this doesn't use extra dependencies.
I think this is a relatively common ask especially when configuration is allowed via environment variables. A fallback is expected in case the variable is not set.