-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
[config] Default config env variable expansion #2231
[config] Default config env variable expansion #2231
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2231 +/- ##
=======================================
Coverage 91.97% 91.98%
=======================================
Files 271 271
Lines 15656 15672 +16
=======================================
+ Hits 14400 14416 +16
Misses 854 854
Partials 402 402
Continue to review full report at Codecov.
|
@@ -305,6 +306,7 @@ func LoadReceiver(componentConfig *viper.Viper, typeStr configmodels.Type, fullN | |||
// Create the default config for this receiver. | |||
receiverCfg := factory.CreateDefaultConfig() | |||
receiverCfg.SetName(fullName) | |||
expandEnvLoadedConfig(receiverCfg) |
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.
Shouldn't this be done in loadReceivers
instead of here? It looks odd to me that this is different that for the rest of component kinds
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 is because receivers are handled differently since #658, it's by design. Consider this solved.
@KSerrania is there an issue open for this? Was it discussed with maintainers? If not then this may need to wait. The bar for new core features is high now, we are focused on making a stable release right now. |
Hi @tigrannajaryan, This is a feature we'd like to use in our exporter, but it is not urgent: it can wait until the work needed to prepare the stable release is done. This hasn't been discussed with maintainers yet, I meant to open this PR as a means to open the discussion while providing a concrete example on how to do this. I can also open an issue if we want to discuss the proposal and its implementation separately. |
@KSerrania I like the change. To make sure we are not breaking existing code can you please check that no existing component uses the |
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.
Can be merged once we have a confirmation that we are not breaking existing default configs.
config/config_test.go
Outdated
@@ -571,3 +571,227 @@ func loadConfigFile(t *testing.T, fileName string, factories component.Factories | |||
} | |||
return cfg, ValidateConfig(cfg, zap.NewNop()) | |||
} | |||
|
|||
type NestedConfig struct { |
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.
Nit: this does not need be exported.
config/config_test.go
Outdated
NestedIntValue int | ||
} | ||
|
||
type TestConfig struct { |
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.
Same here.
I searched, in all go files, for instances of In opentelemetry-collector, no such occurrences appear in default configs. In opentelemetry-collector-contrib, there's one such instance in a config object (https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/471b7c69266e29de1bfbd99d6ae3c09195547b1d/receiver/dockerstatsreceiver/docker_test.go#L43-L45), but it's in a test, not a default config. To double-check, I then searched for all Finally, I checked all Therefore, I am reasonably sure that this shouldn't break existing default configs. Notes: For opentelemetry-collector, I did the search on commit 2962d95 (latest master as of this morning). For opentelemetry-collector-contrib, I did the search on commit 471b7c6 (latest master as of this morning). I think this feature should be added to developer docs somewhere, to make sure contributors do not get surprised by this in the future. What would be a good place to document this? |
@KSerrania thanks for the thorough search.
There is currently no good place with high visibility that I can recommend. Ideally we would have a "How to create a new component" document and could add a note there, but no such doc exists currently. However, I think it is OK for now not document it. If they accidentally use the |
Uses the feature added in open-telemetry/opentelemetry-collector#2231 to allow users to set config options with environment vairables. Allowed environment variables: - `DD_API_KEY` -> `Config.API.Key` - `DD_SITE` -> `Config.API.Site` - `DD_HOST` -> `Config.TagsConfig.Hostname` - `DD_ENV` -> `Config.TagsConfig.Env` - `DD_SERVICE` -> `Config.TagsConfig.Service` - `DD_VERSION` -> `Config.TagsConfig.Version` - `DD_TAGS` -> `Config.TagsConfig.EnvVarTags` (replaces `Config.TagsConfig.Tags` if not set) - `DD_URL` -> `Config.Metrics.TCPAddr.Endpoint` - `DD_APM_URL` -> `Config.Traces.TCPAddr.Endpoint` For `DD_TAGS`, since `Config.TagsConfig.Tags` is a map, and the env variable replacement only operates on string fields, a new placeholder string option `Config.TagsConfig.EnvVarTags` was created to temporarily get the environment variable content. **Testing:** Added unit tests, and tested in our exporter E2E test environment **Documentation:** Updated the example config to explain which environment variables can be used.
Description:
Adds the ability for components to use environment variables in string fields in the default configuration returned by
CreateDefaultConfig
. These fields get expanded when the component is loaded, in the same way the fields of configuration yaml files are ($FOO
is replaced by the value of theFOO
environment variable,$$FOO
is replaced by$FOO
,$$$FOO
is replaced by$
followed by the contents ofFOO
).For instance, if
CreateDefaultConfig
of a component returns:and the
DD_ENV
environment variable is set toprod
, then the resulting struct will contain:Note: The default config is expanded before it's merged with the user-provided config, so as to not mess with the latter.
Link to tracking Issue: n/a
Testing:
Added unit tests to check that the variable expansion works, and that it doesn't crash in edge cases (unexported private fields that can't be modified, uninitialised config object).
Tested behavior with the Datadog exporter.
Documentation: n/a
TODO: add documentation about the feature (where should I add this?).