From 6ab3c983bbfbda567f4372774abc7238680fb22f Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Mon, 24 Jan 2022 05:49:49 -0800 Subject: [PATCH] Enable end users to provide multiple files for config location (#4727) Restriction (compatible with `--set`): Per flag entry can be defined only one location, to define multiple locations use multiple flag entries: ```bash ./otelcol --cofig=file:/path/to/first/file --config=file:/path/to/second/file ``` --- CHANGELOG.md | 2 ++ service/collector_test.go | 6 +++--- service/config_provider.go | 4 ++-- service/flags.go | 13 ++++++------- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 585f466826c..021e3250bf8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ - Deprecate `configtelemetry.Level.Set()` (#4700) - Remove support to some arches and platforms from `ocb` (opentelemetry-collector-builder) (#4710) - Remove deprecated legacy path ("v1/trace") support for otlp http receiver (#4720) +- Change the `service.NewDefaultConfigProvider` to accept a slice of location strings (#4727). ## 🧰 Bug fixes 🧰 @@ -33,6 +34,7 @@ - Move `compression.go` into `confighttp.go` to internalize functions in `compression.go` file. (#4651) - create `configcompression` package to manage compression methods in `confighttp` and `configgrpc` - Add support for cgroupv2 memory limit (#4654) +- Enable end users to provide multiple files for config location (#4727) ## 🧰 Bug fixes 🧰 diff --git a/service/collector_test.go b/service/collector_test.go index 893b0eb8337..600333d6d76 100644 --- a/service/collector_test.go +++ b/service/collector_test.go @@ -54,7 +54,7 @@ func TestCollector_StartAsGoRoutine(t *testing.T) { set := CollectorSettings{ BuildInfo: component.NewDefaultBuildInfo(), Factories: factories, - ConfigProvider: NewDefaultConfigProvider(path.Join("testdata", "otelcol-config.yaml"), nil), + ConfigProvider: NewDefaultConfigProvider([]string{path.Join("testdata", "otelcol-config.yaml")}, nil), } col, err := New(set) require.NoError(t, err) @@ -97,7 +97,7 @@ func TestCollector_Start(t *testing.T) { BuildInfo: component.NewDefaultBuildInfo(), Factories: factories, ConfigProvider: NewDefaultConfigProvider( - path.Join("testdata", "otelcol-config.yaml"), + []string{path.Join("testdata", "otelcol-config.yaml")}, []string{"service.telemetry.metrics.address=localhost:" + strconv.FormatUint(uint64(metricsPort), 10)}), LoggingOptions: []zap.Option{zap.Hooks(hook)}, }) @@ -157,7 +157,7 @@ func TestCollector_ReportError(t *testing.T) { col, err := New(CollectorSettings{ BuildInfo: component.NewDefaultBuildInfo(), Factories: factories, - ConfigProvider: NewDefaultConfigProvider(path.Join("testdata", "otelcol-config.yaml"), nil), + ConfigProvider: NewDefaultConfigProvider([]string{path.Join("testdata", "otelcol-config.yaml")}, nil), }) require.NoError(t, err) diff --git a/service/config_provider.go b/service/config_provider.go index a477f536b9a..34bc8dc9602 100644 --- a/service/config_provider.go +++ b/service/config_provider.go @@ -103,9 +103,9 @@ func NewConfigProvider( // NewDefaultConfigProvider returns the default ConfigProvider, and it creates configuration from a file // defined by the given configFile and overwrites fields using properties. -func NewDefaultConfigProvider(configFileName string, properties []string) ConfigProvider { +func NewDefaultConfigProvider(configLocations []string, properties []string) ConfigProvider { return NewConfigProvider( - []string{configFileName}, + configLocations, map[string]configmapprovider.Provider{ "file": configmapprovider.NewFile(), "env": configmapprovider.NewEnv(), diff --git a/service/flags.go b/service/flags.go index 1b9ff637a18..8c9f75cccab 100644 --- a/service/flags.go +++ b/service/flags.go @@ -22,10 +22,8 @@ import ( ) var ( - defaultConfig = "" - // Command-line flag that control the configuration file. - configFlag = &defaultConfig + configFlag = new(stringArrayValue) setFlag = new(stringArrayValue) ) @@ -39,14 +37,15 @@ func (s *stringArrayValue) Set(val string) error { } func (s *stringArrayValue) String() string { - return "[" + strings.Join(s.values, ",") + "]" + return "[" + strings.Join(s.values, ", ") + "]" } func flags() *flag.FlagSet { flagSet := new(flag.FlagSet) featuregate.Flags(flagSet) - configFlag = flagSet.String("config", defaultConfig, "Path to the config file") + flagSet.Var(configFlag, "config", "Locations to the config file(s), note that only a"+ + " single location can be set per flag entry e.g. `-config=file:/path/to/first --config=file:path/to/second`.") flagSet.Var(setFlag, "set", "Set arbitrary component config property. The component has to be defined in the config file and the flag"+ @@ -56,8 +55,8 @@ func flags() *flag.FlagSet { return flagSet } -func getConfigFlag() string { - return *configFlag +func getConfigFlag() []string { + return configFlag.values } func getSetFlag() []string {