From 92ab6bb326c35b7804637c683aaf5b35cdd9873b Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Wed, 4 Aug 2021 18:07:54 -0700 Subject: [PATCH] Add ConfigUnmarshaler interface to allow mutations on the parsed Config Signed-off-by: Bogdan Drutu --- config/configtest/configtest.go | 6 +-- .../defaultunmarshaler.go} | 50 +++++++++++-------- .../defaultunmarshaler_test.go} | 12 ++--- .../doc.go | 4 +- .../testdata/duplicate-exporter.yaml | 0 .../testdata/duplicate-extension.yaml | 0 .../testdata/duplicate-pipeline.yaml | 0 .../testdata/duplicate-processor.yaml | 0 .../testdata/duplicate-receiver.yaml | 0 .../testdata/empty-all-sections.yaml | 0 .../testdata/empty-config.yaml | 0 .../invalid-exporter-name-after-slash.yaml | 0 .../testdata/invalid-exporter-section.yaml | 0 .../testdata/invalid-exporter-sub-config.yaml | 0 .../testdata/invalid-exporter-type.yaml | 0 .../invalid-extension-name-after-slash.yaml | 0 .../testdata/invalid-extension-section.yaml | 0 .../invalid-extension-sub-config.yaml | 0 .../testdata/invalid-extension-type.yaml | 0 .../invalid-pipeline-name-after-slash.yaml | 0 .../testdata/invalid-pipeline-section.yaml | 0 .../testdata/invalid-pipeline-sub-config.yaml | 0 .../testdata/invalid-pipeline-type.yaml | 0 .../invalid-processor-name-after-slash.yaml | 0 .../testdata/invalid-processor-section.yaml | 0 .../invalid-processor-sub-config.yaml | 0 .../testdata/invalid-processor-type.yaml | 0 .../invalid-receiver-name-after-slash.yaml | 0 .../testdata/invalid-receiver-section.yaml | 0 .../testdata/invalid-receiver-sub-config.yaml | 0 .../testdata/invalid-receiver-type.yaml | 0 .../testdata/invalid-sequence-value.yaml | 0 .../invalid-service-extensions-section.yaml | 0 .../testdata/invalid-service-section.yaml | 0 .../testdata/invalid-top-level-section.yaml | 0 .../testdata/simple-config-with-all-env.yaml | 0 .../simple-config-with-escaped-env.yaml | 0 .../testdata/simple-config-with-no-env.yaml | 0 .../simple-config-with-partial-env.yaml | 0 .../testdata/unknown-exporter-type.yaml | 0 .../testdata/unknown-extension-type.yaml | 0 .../testdata/unknown-pipeline-type.yaml | 0 .../testdata/unknown-processor-type.yaml | 0 .../testdata/unknown-receiver-type.yaml | 0 .../testdata/valid-config.yaml | 0 config/configunmarshaler/unmarshaler.go | 27 ++++++++++ service/collector.go | 32 +++++++----- service/collector_test.go | 12 +++-- .../builder/pipelines_builder_test.go | 2 +- service/parserprovider/default_test.go | 10 ++-- service/settings.go | 5 ++ 51 files changed, 104 insertions(+), 56 deletions(-) rename config/{configloader/config.go => configunmarshaler/defaultunmarshaler.go} (87%) rename config/{configloader/config_test.go => configunmarshaler/defaultunmarshaler_test.go} (99%) rename config/{configloader => configunmarshaler}/doc.go (86%) rename config/{configloader => configunmarshaler}/testdata/duplicate-exporter.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/duplicate-extension.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/duplicate-pipeline.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/duplicate-processor.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/duplicate-receiver.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/empty-all-sections.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/empty-config.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-exporter-name-after-slash.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-exporter-section.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-exporter-sub-config.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-exporter-type.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-extension-name-after-slash.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-extension-section.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-extension-sub-config.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-extension-type.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-pipeline-name-after-slash.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-pipeline-section.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-pipeline-sub-config.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-pipeline-type.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-processor-name-after-slash.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-processor-section.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-processor-sub-config.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-processor-type.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-receiver-name-after-slash.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-receiver-section.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-receiver-sub-config.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-receiver-type.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-sequence-value.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-service-extensions-section.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-service-section.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/invalid-top-level-section.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/simple-config-with-all-env.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/simple-config-with-escaped-env.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/simple-config-with-no-env.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/simple-config-with-partial-env.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/unknown-exporter-type.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/unknown-extension-type.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/unknown-pipeline-type.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/unknown-processor-type.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/unknown-receiver-type.yaml (100%) rename config/{configloader => configunmarshaler}/testdata/valid-config.yaml (100%) create mode 100644 config/configunmarshaler/unmarshaler.go diff --git a/config/configtest/configtest.go b/config/configtest/configtest.go index 21a26528c69..c27ecc96d4b 100644 --- a/config/configtest/configtest.go +++ b/config/configtest/configtest.go @@ -17,8 +17,8 @@ package configtest import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config" - "go.opentelemetry.io/collector/config/configloader" "go.opentelemetry.io/collector/config/configparser" + "go.opentelemetry.io/collector/config/configunmarshaler" ) // LoadConfig loads a config from file, and does NOT validate the configuration. @@ -28,8 +28,8 @@ func LoadConfig(fileName string, factories component.Factories) (*config.Config, if err != nil { return nil, err } - // Load the config using the given factories. - return configloader.Load(cp, factories) + // Unmarshal the config using the given factories. + return configunmarshaler.NewDefault().Unmarshal(cp, factories) } // LoadConfigAndValidate loads a config from the file, and validates the configuration. diff --git a/config/configloader/config.go b/config/configunmarshaler/defaultunmarshaler.go similarity index 87% rename from config/configloader/config.go rename to config/configunmarshaler/defaultunmarshaler.go index 3b1416ecf55..cf6a38a2874 100644 --- a/config/configloader/config.go +++ b/config/configunmarshaler/defaultunmarshaler.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package configloader +package configunmarshaler import ( "fmt" @@ -26,8 +26,8 @@ import ( "go.opentelemetry.io/collector/config/configparser" ) -// These are errors that can be returned by Load(). Note that error codes are not part -// of Load()'s public API, they are for internal unit testing only. +// These are errors that can be returned by Unmarshal(). Note that error codes are not part +// of Unmarshal()'s public API, they are for internal unit testing only. type configErrorCode int const ( @@ -89,12 +89,20 @@ type pipelineSettings struct { Exporters []string `mapstructure:"exporters"` } -// Load loads a Config from Parser. -// After loading the config, `Validate()` must be called to validate. -func Load(v *configparser.Parser, factories component.Factories) (*config.Config, error) { +type defaultUnmarshaler struct{} + +// NewDefault returns a default ConfigUnmarshaler that unmarshalls every configuration +// using the custom unmarshaler if present or default to strict +func NewDefault() ConfigUnmarshaler { + return &defaultUnmarshaler{} +} + +// Unmarshal the Config from a Parser. +// After the config is unmarshalled, `Validate()` must be called to validate. +func (*defaultUnmarshaler) Unmarshal(v *configparser.Parser, factories component.Factories) (*config.Config, error) { var cfg config.Config - // Load the config. + // Unmarshal the config. // Struct to validate top level sections. var rawCfg configSettings @@ -111,34 +119,34 @@ func Load(v *configparser.Parser, factories component.Factories) (*config.Config // Start with the service extensions. - extensions, err := loadExtensions(cast.ToStringMap(v.Get(extensionsKeyName)), factories.Extensions) + extensions, err := unmarshalExtensions(cast.ToStringMap(v.Get(extensionsKeyName)), factories.Extensions) if err != nil { return nil, err } cfg.Extensions = extensions - // Load data components (receivers, exporters, and processors). + // Unmarshal data components (receivers, exporters, and processors). - receivers, err := loadReceivers(cast.ToStringMap(v.Get(receiversKeyName)), factories.Receivers) + receivers, err := unmarshalReceivers(cast.ToStringMap(v.Get(receiversKeyName)), factories.Receivers) if err != nil { return nil, err } cfg.Receivers = receivers - exporters, err := loadExporters(cast.ToStringMap(v.Get(exportersKeyName)), factories.Exporters) + exporters, err := unmarshalExporters(cast.ToStringMap(v.Get(exportersKeyName)), factories.Exporters) if err != nil { return nil, err } cfg.Exporters = exporters - processors, err := loadProcessors(cast.ToStringMap(v.Get(processorsKeyName)), factories.Processors) + processors, err := unmarshalProcessors(cast.ToStringMap(v.Get(processorsKeyName)), factories.Processors) if err != nil { return nil, err } cfg.Processors = processors - // Load the service and its data pipelines. - service, err := loadService(rawCfg.Service) + // Unmarshal the service and its data pipelines. + service, err := unmarshalService(rawCfg.Service) if err != nil { return nil, err } @@ -175,7 +183,7 @@ func errorDuplicateName(component string, id config.ComponentID) error { } } -func loadExtensions(exts map[string]interface{}, factories map[config.Type]component.ExtensionFactory) (config.Extensions, error) { +func unmarshalExtensions(exts map[string]interface{}, factories map[config.Type]component.ExtensionFactory) (config.Extensions, error) { // Prepare resulting map. extensions := make(config.Extensions) @@ -217,7 +225,7 @@ func loadExtensions(exts map[string]interface{}, factories map[config.Type]compo return extensions, nil } -func loadService(rawService serviceSettings) (config.Service, error) { +func unmarshalService(rawService serviceSettings) (config.Service, error) { var ret config.Service ret.Extensions = make([]config.ComponentID, 0, len(rawService.Extensions)) for _, extIDStr := range rawService.Extensions { @@ -230,7 +238,7 @@ func loadService(rawService serviceSettings) (config.Service, error) { // Process the pipelines first so in case of error on them it can be properly // reported. - pipelines, err := loadPipelines(rawService.Pipelines) + pipelines, err := unmarshalPipelines(rawService.Pipelines) ret.Pipelines = pipelines return ret, err @@ -252,7 +260,7 @@ func LoadReceiver(componentConfig *configparser.Parser, id config.ComponentID, f return receiverCfg, nil } -func loadReceivers(recvs map[string]interface{}, factories map[config.Type]component.ReceiverFactory) (config.Receivers, error) { +func unmarshalReceivers(recvs map[string]interface{}, factories map[config.Type]component.ReceiverFactory) (config.Receivers, error) { // Prepare resulting map. receivers := make(config.Receivers) @@ -289,7 +297,7 @@ func loadReceivers(recvs map[string]interface{}, factories map[config.Type]compo return receivers, nil } -func loadExporters(exps map[string]interface{}, factories map[config.Type]component.ExporterFactory) (config.Exporters, error) { +func unmarshalExporters(exps map[string]interface{}, factories map[config.Type]component.ExporterFactory) (config.Exporters, error) { // Prepare resulting map. exporters := make(config.Exporters) @@ -331,7 +339,7 @@ func loadExporters(exps map[string]interface{}, factories map[config.Type]compon return exporters, nil } -func loadProcessors(procs map[string]interface{}, factories map[config.Type]component.ProcessorFactory) (config.Processors, error) { +func unmarshalProcessors(procs map[string]interface{}, factories map[config.Type]component.ProcessorFactory) (config.Processors, error) { // Prepare resulting map. processors := make(config.Processors) @@ -373,7 +381,7 @@ func loadProcessors(procs map[string]interface{}, factories map[config.Type]comp return processors, nil } -func loadPipelines(pipelinesConfig map[string]pipelineSettings) (config.Pipelines, error) { +func unmarshalPipelines(pipelinesConfig map[string]pipelineSettings) (config.Pipelines, error) { // Prepare resulting map. pipelines := make(config.Pipelines) diff --git a/config/configloader/config_test.go b/config/configunmarshaler/defaultunmarshaler_test.go similarity index 99% rename from config/configloader/config_test.go rename to config/configunmarshaler/defaultunmarshaler_test.go index 857ad571a6e..2ab58f051d1 100644 --- a/config/configloader/config_test.go +++ b/config/configunmarshaler/defaultunmarshaler_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package configloader +package configunmarshaler import ( "os" @@ -33,7 +33,7 @@ func TestDecodeConfig(t *testing.T) { factories, err := testcomponents.ExampleComponents() assert.NoError(t, err) - // Load the config + // Unmarshal the config cfg, err := loadConfigFile(t, path.Join(".", "testdata", "valid-config.yaml"), factories) require.NoError(t, err, "Unable to load config") @@ -186,7 +186,7 @@ func TestSimpleConfig(t *testing.T) { factories, err := testcomponents.ExampleComponents() assert.NoError(t, err) - // Load the config + // Unmarshal the config cfg, err := loadConfigFile(t, path.Join(".", "testdata", test.name+".yaml"), factories) require.NoError(t, err, "Unable to load config") @@ -276,7 +276,7 @@ func TestEscapedEnvVars(t *testing.T) { factories, err := testcomponents.ExampleComponents() assert.NoError(t, err) - // Load the config + // Unmarshal the config cfg, err := loadConfigFile(t, path.Join(".", "testdata", "simple-config-with-escaped-env.yaml"), factories) require.NoError(t, err, "Unable to load config") @@ -459,8 +459,8 @@ func loadConfigFile(t *testing.T, fileName string, factories component.Factories v, err := configparser.NewParserFromFile(fileName) require.NoError(t, err) - // Load the config from the configparser.Parser using the given factories. - return Load(v, factories) + // Unmarshal the config from the configparser.Parser using the given factories. + return NewDefault().Unmarshal(v, factories) } type nestedConfig struct { diff --git a/config/configloader/doc.go b/config/configunmarshaler/doc.go similarity index 86% rename from config/configloader/doc.go rename to config/configunmarshaler/doc.go index 1f08d22730b..7ca58b87eb8 100644 --- a/config/configloader/doc.go +++ b/config/configunmarshaler/doc.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package configloader implements configuration loading from a config.Parser. +// Package configunmarshaler implements configuration unmarshalling from a config.Parser. // The implementation relies on registered factories that allow creating // default configuration for each type of receiver/exporter/processor. -package configloader +package configunmarshaler diff --git a/config/configloader/testdata/duplicate-exporter.yaml b/config/configunmarshaler/testdata/duplicate-exporter.yaml similarity index 100% rename from config/configloader/testdata/duplicate-exporter.yaml rename to config/configunmarshaler/testdata/duplicate-exporter.yaml diff --git a/config/configloader/testdata/duplicate-extension.yaml b/config/configunmarshaler/testdata/duplicate-extension.yaml similarity index 100% rename from config/configloader/testdata/duplicate-extension.yaml rename to config/configunmarshaler/testdata/duplicate-extension.yaml diff --git a/config/configloader/testdata/duplicate-pipeline.yaml b/config/configunmarshaler/testdata/duplicate-pipeline.yaml similarity index 100% rename from config/configloader/testdata/duplicate-pipeline.yaml rename to config/configunmarshaler/testdata/duplicate-pipeline.yaml diff --git a/config/configloader/testdata/duplicate-processor.yaml b/config/configunmarshaler/testdata/duplicate-processor.yaml similarity index 100% rename from config/configloader/testdata/duplicate-processor.yaml rename to config/configunmarshaler/testdata/duplicate-processor.yaml diff --git a/config/configloader/testdata/duplicate-receiver.yaml b/config/configunmarshaler/testdata/duplicate-receiver.yaml similarity index 100% rename from config/configloader/testdata/duplicate-receiver.yaml rename to config/configunmarshaler/testdata/duplicate-receiver.yaml diff --git a/config/configloader/testdata/empty-all-sections.yaml b/config/configunmarshaler/testdata/empty-all-sections.yaml similarity index 100% rename from config/configloader/testdata/empty-all-sections.yaml rename to config/configunmarshaler/testdata/empty-all-sections.yaml diff --git a/config/configloader/testdata/empty-config.yaml b/config/configunmarshaler/testdata/empty-config.yaml similarity index 100% rename from config/configloader/testdata/empty-config.yaml rename to config/configunmarshaler/testdata/empty-config.yaml diff --git a/config/configloader/testdata/invalid-exporter-name-after-slash.yaml b/config/configunmarshaler/testdata/invalid-exporter-name-after-slash.yaml similarity index 100% rename from config/configloader/testdata/invalid-exporter-name-after-slash.yaml rename to config/configunmarshaler/testdata/invalid-exporter-name-after-slash.yaml diff --git a/config/configloader/testdata/invalid-exporter-section.yaml b/config/configunmarshaler/testdata/invalid-exporter-section.yaml similarity index 100% rename from config/configloader/testdata/invalid-exporter-section.yaml rename to config/configunmarshaler/testdata/invalid-exporter-section.yaml diff --git a/config/configloader/testdata/invalid-exporter-sub-config.yaml b/config/configunmarshaler/testdata/invalid-exporter-sub-config.yaml similarity index 100% rename from config/configloader/testdata/invalid-exporter-sub-config.yaml rename to config/configunmarshaler/testdata/invalid-exporter-sub-config.yaml diff --git a/config/configloader/testdata/invalid-exporter-type.yaml b/config/configunmarshaler/testdata/invalid-exporter-type.yaml similarity index 100% rename from config/configloader/testdata/invalid-exporter-type.yaml rename to config/configunmarshaler/testdata/invalid-exporter-type.yaml diff --git a/config/configloader/testdata/invalid-extension-name-after-slash.yaml b/config/configunmarshaler/testdata/invalid-extension-name-after-slash.yaml similarity index 100% rename from config/configloader/testdata/invalid-extension-name-after-slash.yaml rename to config/configunmarshaler/testdata/invalid-extension-name-after-slash.yaml diff --git a/config/configloader/testdata/invalid-extension-section.yaml b/config/configunmarshaler/testdata/invalid-extension-section.yaml similarity index 100% rename from config/configloader/testdata/invalid-extension-section.yaml rename to config/configunmarshaler/testdata/invalid-extension-section.yaml diff --git a/config/configloader/testdata/invalid-extension-sub-config.yaml b/config/configunmarshaler/testdata/invalid-extension-sub-config.yaml similarity index 100% rename from config/configloader/testdata/invalid-extension-sub-config.yaml rename to config/configunmarshaler/testdata/invalid-extension-sub-config.yaml diff --git a/config/configloader/testdata/invalid-extension-type.yaml b/config/configunmarshaler/testdata/invalid-extension-type.yaml similarity index 100% rename from config/configloader/testdata/invalid-extension-type.yaml rename to config/configunmarshaler/testdata/invalid-extension-type.yaml diff --git a/config/configloader/testdata/invalid-pipeline-name-after-slash.yaml b/config/configunmarshaler/testdata/invalid-pipeline-name-after-slash.yaml similarity index 100% rename from config/configloader/testdata/invalid-pipeline-name-after-slash.yaml rename to config/configunmarshaler/testdata/invalid-pipeline-name-after-slash.yaml diff --git a/config/configloader/testdata/invalid-pipeline-section.yaml b/config/configunmarshaler/testdata/invalid-pipeline-section.yaml similarity index 100% rename from config/configloader/testdata/invalid-pipeline-section.yaml rename to config/configunmarshaler/testdata/invalid-pipeline-section.yaml diff --git a/config/configloader/testdata/invalid-pipeline-sub-config.yaml b/config/configunmarshaler/testdata/invalid-pipeline-sub-config.yaml similarity index 100% rename from config/configloader/testdata/invalid-pipeline-sub-config.yaml rename to config/configunmarshaler/testdata/invalid-pipeline-sub-config.yaml diff --git a/config/configloader/testdata/invalid-pipeline-type.yaml b/config/configunmarshaler/testdata/invalid-pipeline-type.yaml similarity index 100% rename from config/configloader/testdata/invalid-pipeline-type.yaml rename to config/configunmarshaler/testdata/invalid-pipeline-type.yaml diff --git a/config/configloader/testdata/invalid-processor-name-after-slash.yaml b/config/configunmarshaler/testdata/invalid-processor-name-after-slash.yaml similarity index 100% rename from config/configloader/testdata/invalid-processor-name-after-slash.yaml rename to config/configunmarshaler/testdata/invalid-processor-name-after-slash.yaml diff --git a/config/configloader/testdata/invalid-processor-section.yaml b/config/configunmarshaler/testdata/invalid-processor-section.yaml similarity index 100% rename from config/configloader/testdata/invalid-processor-section.yaml rename to config/configunmarshaler/testdata/invalid-processor-section.yaml diff --git a/config/configloader/testdata/invalid-processor-sub-config.yaml b/config/configunmarshaler/testdata/invalid-processor-sub-config.yaml similarity index 100% rename from config/configloader/testdata/invalid-processor-sub-config.yaml rename to config/configunmarshaler/testdata/invalid-processor-sub-config.yaml diff --git a/config/configloader/testdata/invalid-processor-type.yaml b/config/configunmarshaler/testdata/invalid-processor-type.yaml similarity index 100% rename from config/configloader/testdata/invalid-processor-type.yaml rename to config/configunmarshaler/testdata/invalid-processor-type.yaml diff --git a/config/configloader/testdata/invalid-receiver-name-after-slash.yaml b/config/configunmarshaler/testdata/invalid-receiver-name-after-slash.yaml similarity index 100% rename from config/configloader/testdata/invalid-receiver-name-after-slash.yaml rename to config/configunmarshaler/testdata/invalid-receiver-name-after-slash.yaml diff --git a/config/configloader/testdata/invalid-receiver-section.yaml b/config/configunmarshaler/testdata/invalid-receiver-section.yaml similarity index 100% rename from config/configloader/testdata/invalid-receiver-section.yaml rename to config/configunmarshaler/testdata/invalid-receiver-section.yaml diff --git a/config/configloader/testdata/invalid-receiver-sub-config.yaml b/config/configunmarshaler/testdata/invalid-receiver-sub-config.yaml similarity index 100% rename from config/configloader/testdata/invalid-receiver-sub-config.yaml rename to config/configunmarshaler/testdata/invalid-receiver-sub-config.yaml diff --git a/config/configloader/testdata/invalid-receiver-type.yaml b/config/configunmarshaler/testdata/invalid-receiver-type.yaml similarity index 100% rename from config/configloader/testdata/invalid-receiver-type.yaml rename to config/configunmarshaler/testdata/invalid-receiver-type.yaml diff --git a/config/configloader/testdata/invalid-sequence-value.yaml b/config/configunmarshaler/testdata/invalid-sequence-value.yaml similarity index 100% rename from config/configloader/testdata/invalid-sequence-value.yaml rename to config/configunmarshaler/testdata/invalid-sequence-value.yaml diff --git a/config/configloader/testdata/invalid-service-extensions-section.yaml b/config/configunmarshaler/testdata/invalid-service-extensions-section.yaml similarity index 100% rename from config/configloader/testdata/invalid-service-extensions-section.yaml rename to config/configunmarshaler/testdata/invalid-service-extensions-section.yaml diff --git a/config/configloader/testdata/invalid-service-section.yaml b/config/configunmarshaler/testdata/invalid-service-section.yaml similarity index 100% rename from config/configloader/testdata/invalid-service-section.yaml rename to config/configunmarshaler/testdata/invalid-service-section.yaml diff --git a/config/configloader/testdata/invalid-top-level-section.yaml b/config/configunmarshaler/testdata/invalid-top-level-section.yaml similarity index 100% rename from config/configloader/testdata/invalid-top-level-section.yaml rename to config/configunmarshaler/testdata/invalid-top-level-section.yaml diff --git a/config/configloader/testdata/simple-config-with-all-env.yaml b/config/configunmarshaler/testdata/simple-config-with-all-env.yaml similarity index 100% rename from config/configloader/testdata/simple-config-with-all-env.yaml rename to config/configunmarshaler/testdata/simple-config-with-all-env.yaml diff --git a/config/configloader/testdata/simple-config-with-escaped-env.yaml b/config/configunmarshaler/testdata/simple-config-with-escaped-env.yaml similarity index 100% rename from config/configloader/testdata/simple-config-with-escaped-env.yaml rename to config/configunmarshaler/testdata/simple-config-with-escaped-env.yaml diff --git a/config/configloader/testdata/simple-config-with-no-env.yaml b/config/configunmarshaler/testdata/simple-config-with-no-env.yaml similarity index 100% rename from config/configloader/testdata/simple-config-with-no-env.yaml rename to config/configunmarshaler/testdata/simple-config-with-no-env.yaml diff --git a/config/configloader/testdata/simple-config-with-partial-env.yaml b/config/configunmarshaler/testdata/simple-config-with-partial-env.yaml similarity index 100% rename from config/configloader/testdata/simple-config-with-partial-env.yaml rename to config/configunmarshaler/testdata/simple-config-with-partial-env.yaml diff --git a/config/configloader/testdata/unknown-exporter-type.yaml b/config/configunmarshaler/testdata/unknown-exporter-type.yaml similarity index 100% rename from config/configloader/testdata/unknown-exporter-type.yaml rename to config/configunmarshaler/testdata/unknown-exporter-type.yaml diff --git a/config/configloader/testdata/unknown-extension-type.yaml b/config/configunmarshaler/testdata/unknown-extension-type.yaml similarity index 100% rename from config/configloader/testdata/unknown-extension-type.yaml rename to config/configunmarshaler/testdata/unknown-extension-type.yaml diff --git a/config/configloader/testdata/unknown-pipeline-type.yaml b/config/configunmarshaler/testdata/unknown-pipeline-type.yaml similarity index 100% rename from config/configloader/testdata/unknown-pipeline-type.yaml rename to config/configunmarshaler/testdata/unknown-pipeline-type.yaml diff --git a/config/configloader/testdata/unknown-processor-type.yaml b/config/configunmarshaler/testdata/unknown-processor-type.yaml similarity index 100% rename from config/configloader/testdata/unknown-processor-type.yaml rename to config/configunmarshaler/testdata/unknown-processor-type.yaml diff --git a/config/configloader/testdata/unknown-receiver-type.yaml b/config/configunmarshaler/testdata/unknown-receiver-type.yaml similarity index 100% rename from config/configloader/testdata/unknown-receiver-type.yaml rename to config/configunmarshaler/testdata/unknown-receiver-type.yaml diff --git a/config/configloader/testdata/valid-config.yaml b/config/configunmarshaler/testdata/valid-config.yaml similarity index 100% rename from config/configloader/testdata/valid-config.yaml rename to config/configunmarshaler/testdata/valid-config.yaml diff --git a/config/configunmarshaler/unmarshaler.go b/config/configunmarshaler/unmarshaler.go new file mode 100644 index 00000000000..123ab4c96ad --- /dev/null +++ b/config/configunmarshaler/unmarshaler.go @@ -0,0 +1,27 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package configunmarshaler + +import ( + "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/config" + "go.opentelemetry.io/collector/config/configparser" +) + +// ConfigUnmarshaler is the interface that unmarshalls the collector configuration from the configparser.Parser. +type ConfigUnmarshaler interface { + // Unmarshal the configuration from the given parser and factories. + Unmarshal(v *configparser.Parser, factories component.Factories) (*config.Config, error) +} diff --git a/service/collector.go b/service/collector.go index 83596c2e4a6..7ad04497f9d 100644 --- a/service/collector.go +++ b/service/collector.go @@ -35,8 +35,8 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config/configcheck" - "go.opentelemetry.io/collector/config/configloader" "go.opentelemetry.io/collector/config/configtelemetry" + "go.opentelemetry.io/collector/config/configunmarshaler" "go.opentelemetry.io/collector/config/experimental/configsource" "go.opentelemetry.io/collector/consumer/consumererror" "go.opentelemetry.io/collector/extension/ballastextension" @@ -81,7 +81,8 @@ type Collector struct { factories component.Factories - parserProvider parserprovider.ParserProvider + parserProvider parserprovider.ParserProvider + configUnmarshaler configunmarshaler.ConfigUnmarshaler // shutdownChan is used to terminate the collector. shutdownChan chan struct{} @@ -102,14 +103,26 @@ func New(set CollectorSettings) (*Collector, error) { } col := &Collector{ - info: set.BuildInfo, - factories: set.Factories, - stateChannel: make(chan State, Closed+1), + info: set.BuildInfo, + factories: set.Factories, + stateChannel: make(chan State, Closed+1), + parserProvider: set.ParserProvider, + configUnmarshaler: set.ConfigUnmarshaler, // We use a negative in the settings not to break the existing // behavior. Internally, allowGracefulShutodwn is more readable. allowGracefulShutodwn: !set.DisableGracefulShutdown, } + if col.parserProvider == nil { + // use default provider. + col.parserProvider = parserprovider.Default() + } + + if col.configUnmarshaler == nil { + // use default provider. + col.configUnmarshaler = configunmarshaler.NewDefault() + } + rootCmd := &cobra.Command{ Use: set.BuildInfo.Command, Version: set.BuildInfo.Version, @@ -147,13 +160,6 @@ func New(set CollectorSettings) (*Collector, error) { rootCmd.Flags().AddGoFlagSet(flagSet) col.rootCmd = rootCmd - parserProvider := set.ParserProvider - if parserProvider == nil { - // use default provider. - parserProvider = parserprovider.Default() - } - col.parserProvider = parserProvider - return col, nil } @@ -240,7 +246,7 @@ func (col *Collector) setupConfigurationComponents(ctx context.Context) error { return fmt.Errorf("cannot load configuration's parser: %w", err) } - cfg, err := configloader.Load(cp, col.factories) + cfg, err := col.configUnmarshaler.Unmarshal(cp, col.factories) if err != nil { return fmt.Errorf("cannot load configuration: %w", err) } diff --git a/service/collector_test.go b/service/collector_test.go index 5d21c251f42..ee9c3603e97 100644 --- a/service/collector_test.go +++ b/service/collector_test.go @@ -36,6 +36,7 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config/configparser" + "go.opentelemetry.io/collector/config/configunmarshaler" "go.opentelemetry.io/collector/service/defaultcomponents" "go.opentelemetry.io/collector/service/internal/builder" "go.opentelemetry.io/collector/service/parserprovider" @@ -329,11 +330,12 @@ func TestCollector_reloadService(t *testing.T) { } col := Collector{ - logger: zap.NewNop(), - tracerProvider: trace.NewNoopTracerProvider(), - parserProvider: tt.parserProvider, - factories: factories, - service: tt.service, + logger: zap.NewNop(), + tracerProvider: trace.NewNoopTracerProvider(), + parserProvider: tt.parserProvider, + configUnmarshaler: configunmarshaler.NewDefault(), + factories: factories, + service: tt.service, } err := col.reloadService(ctx) diff --git a/service/internal/builder/pipelines_builder_test.go b/service/internal/builder/pipelines_builder_test.go index 6468595fd5d..ec3add189cd 100644 --- a/service/internal/builder/pipelines_builder_test.go +++ b/service/internal/builder/pipelines_builder_test.go @@ -181,7 +181,7 @@ func testPipeline(t *testing.T, pipelineName string, exporterIDs []config.Compon factories, err := testcomponents.ExampleComponents() assert.NoError(t, err) cfg, err := configtest.LoadConfigAndValidate("testdata/pipelines_builder.yaml", factories) - // Load the config + // Unmarshal the config require.Nil(t, err) // BuildProcessors the pipeline diff --git a/service/parserprovider/default_test.go b/service/parserprovider/default_test.go index d177861d53c..b0e26fb9027 100644 --- a/service/parserprovider/default_test.go +++ b/service/parserprovider/default_test.go @@ -24,8 +24,8 @@ import ( "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/config" - "go.opentelemetry.io/collector/config/configloader" "go.opentelemetry.io/collector/config/configparser" + "go.opentelemetry.io/collector/config/configunmarshaler" "go.opentelemetry.io/collector/processor/attributesprocessor" "go.opentelemetry.io/collector/processor/batchprocessor" "go.opentelemetry.io/collector/receiver/jaegerreceiver" @@ -50,7 +50,7 @@ func TestDefault(t *testing.T) { require.NoError(t, err) require.NotNil(t, cp) var cfg *config.Config - cfg, err = configloader.Load(cp, factories) + cfg, err = configunmarshaler.NewDefault().Unmarshal(cp, factories) require.Error(t, err) require.Nil(t, cfg) @@ -70,7 +70,7 @@ func TestDefault(t *testing.T) { require.NoError(t, err) require.NotNil(t, cp) var cfg *config.Config - cfg, err = configloader.Load(cp, factories) + cfg, err = configunmarshaler.NewDefault().Unmarshal(cp, factories) require.NoError(t, err) assert.NotNil(t, cfg) err = cfg.Validate() @@ -106,7 +106,7 @@ func TestDefault(t *testing.T) { require.NoError(t, err) require.NotNil(t, cp) var cfg *config.Config - cfg, err = configloader.Load(cp, factories) + cfg, err = configunmarshaler.NewDefault().Unmarshal(cp, factories) require.NoError(t, err) require.NotNil(t, cfg) err = cfg.Validate() @@ -146,7 +146,7 @@ func TestDefault_ComponentDoesNotExist(t *testing.T) { cp, err := pl.Get() require.NoError(t, err) require.NotNil(t, cp) - cfg, err := configloader.Load(cp, factories) + cfg, err := configunmarshaler.NewDefault().Unmarshal(cp, factories) require.NoError(t, err) require.NotNil(t, cfg) } diff --git a/service/settings.go b/service/settings.go index e9867be4d47..227615024cd 100644 --- a/service/settings.go +++ b/service/settings.go @@ -21,6 +21,7 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config" + "go.opentelemetry.io/collector/config/configunmarshaler" "go.opentelemetry.io/collector/service/parserprovider" ) @@ -68,6 +69,10 @@ type CollectorSettings struct { // properties supplied via --set command line flag. ParserProvider parserprovider.ParserProvider + // ConfigUnmarshaler unmarshalls the configuration's Parser into the service configuration. + // If it is not provided a default unmarshaler is used. + ConfigUnmarshaler configunmarshaler.ConfigUnmarshaler + // LoggingOptions provides a way to change behavior of zap logging. LoggingOptions []zap.Option }