Skip to content

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

Closed
wants to merge 4 commits into from

Conversation

neelayu
Copy link
Contributor

@neelayu neelayu commented Nov 18, 2021

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.

@neelayu neelayu requested review from a team and dmitryax November 18, 2021 16:06
@codecov
Copy link

codecov bot commented Nov 18, 2021

Codecov Report

Merging #4458 (358688f) into main (db4aa87) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            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              
Impacted Files Coverage Δ
config/configmapprovider/expand.go 73.68% <100.00%> (+1.98%) ⬆️
exporter/otlpexporter/otlp.go 72.83% <0.00%> (ø)
client/client.go 89.65% <0.00%> (+3.94%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update db4aa87...358688f. Read the comment docs.

@@ -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 {
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ACK. Changed to LookupEnv

Copy link
Member

@mx-psi mx-psi left a 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}"
Copy link
Member

@mx-psi mx-psi Nov 19, 2021

Choose a reason for hiding this comment

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

Bash and zsh use the ${parameter:-word} syntax (${name-word} works too on zsh). I would prefer to stick to the known convention for this.

Copy link
Contributor Author

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}

@@ -86,6 +87,10 @@ func expandEnv(s string) string {
if str == "$" {
return "$"
}
return os.Getenv(str)
pair := strings.Split(str, ":")
Copy link
Member

@mx-psi mx-psi Nov 19, 2021

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.

Copy link
Contributor Author

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

Copy link
Member

@mx-psi mx-psi left a 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]>
@bogdandrutu
Copy link
Member

@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.

Copy link
Member

@tigrannajaryan tigrannajaryan left a 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.

@mx-psi
Copy link
Member

mx-psi commented Nov 22, 2021

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).

@tigrannajaryan
Copy link
Member

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.

@tigrannajaryan tigrannajaryan dismissed their stale review November 29, 2021 18:49

Not working on this anymore.

@github-actions
Copy link
Contributor

github-actions bot commented Dec 7, 2021

This PR was marked stale due to lack of activity. It will be closed in 7 days.

@github-actions github-actions bot added the Stale label Dec 7, 2021
@bogdandrutu bogdandrutu removed the Stale label Dec 8, 2021
@github-actions
Copy link
Contributor

This PR was marked stale due to lack of activity. It will be closed in 7 days.

@github-actions
Copy link
Contributor

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)
Copy link
Member

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.

Copy link
Member

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?

Copy link
Member

@bogdandrutu bogdandrutu Jan 18, 2022

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}

Copy link
Member

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.

Copy link
Member

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 :)

Copy link
Member

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!

@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2022

This PR was marked stale due to lack of activity. It will be closed in 14 days.

@github-actions github-actions bot added the Stale label Feb 4, 2022
@github-actions
Copy link
Contributor

Closed as inactive. Feel free to reopen if this PR is still being worked on.

@github-actions github-actions bot closed this Feb 18, 2022
@ringerc
Copy link

ringerc commented Nov 14, 2023

See #2534 (comment) for a workaround

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants