From bf0717ece41a692f104da014ce7c7f6bcbb00550 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Wed, 20 Apr 2022 14:06:42 -0700 Subject: [PATCH] Move config.Map to its own package which does not depend on any component concept Signed-off-by: Bogdan Drutu --- config/common.go | 5 +- config/config.go | 8 +- config/configtest/configtest.go | 6 +- config/configunmarshaler/doc.go | 2 +- config/deprecated.go | 51 +++++++++++ config/experimental/configsource/component.go | 4 +- config/exporter.go | 2 +- config/extension.go | 2 +- config/internal/configsource/README.md | 8 +- config/internal/configsource/manager.go | 26 +++--- config/internal/configsource/manager_test.go | 20 ++--- .../mapconverter/expandmapconverter/expand.go | 8 +- .../expandmapconverter/expand_test.go | 4 +- .../properties.go | 14 +-- .../properties_test.go | 8 +- .../mapprovider/envmapprovider/mapprovider.go | 14 +-- .../envmapprovider/mapprovider_test.go | 4 +- .../filemapprovider/mapprovider.go | 16 ++-- .../filemapprovider/mapprovider_test.go | 6 +- .../yamlmapprovider/mapprovider.go | 14 +-- config/processor.go | 2 +- config/receiver.go | 2 +- {config => configmap}/configmap.go | 88 +++++++++---------- {config => configmap}/configmap_test.go | 14 +-- .../mapprovider.go => configmap/provider.go | 32 +++---- .../provider_test.go | 6 +- .../testdata/basic_types.yaml | 0 {config => configmap}/testdata/config.yaml | 0 .../testdata/embedded_keys.yaml | 0 go.sum | 1 - .../configunmarshaler/defaultunmarshaler.go | 21 ++--- .../defaultunmarshaler_test.go | 2 +- internal/testcomponents/example_exporter.go | 5 +- model/go.mod | 8 +- model/go.sum | 2 - .../cmd/pdatagen/internal/common_structs.go | 2 +- receiver/otlpreceiver/config.go | 7 +- service/collector_test.go | 4 +- service/collector_windows.go | 4 +- service/command.go | 4 +- service/config_provider.go | 45 +++++----- service/config_provider_test.go | 45 +++++----- service/internal/builder/exporters_builder.go | 2 +- 43 files changed, 284 insertions(+), 234 deletions(-) create mode 100644 config/deprecated.go rename {config => configmap}/configmap.go (73%) rename {config => configmap}/configmap_test.go (94%) rename config/mapprovider.go => configmap/provider.go (85%) rename config/mapprovider_test.go => configmap/provider_test.go (95%) rename {config => configmap}/testdata/basic_types.yaml (100%) rename {config => configmap}/testdata/config.yaml (100%) rename {config => configmap}/testdata/embedded_keys.yaml (100%) diff --git a/config/common.go b/config/common.go index b2e582356c4..317baef8727 100644 --- a/config/common.go +++ b/config/common.go @@ -13,8 +13,11 @@ // limitations under the License. package config // import "go.opentelemetry.io/collector/config" +import ( + "go.opentelemetry.io/collector/configmap" +) -func unmarshal(componentSection *Map, intoCfg interface{}) error { +func unmarshal(componentSection *configmap.ConfigMap, intoCfg interface{}) error { if cu, ok := intoCfg.(Unmarshallable); ok { return cu.Unmarshal(componentSection) } diff --git a/config/config.go b/config/config.go index eace63ab25f..83658414804 100644 --- a/config/config.go +++ b/config/config.go @@ -17,6 +17,8 @@ package config // import "go.opentelemetry.io/collector/config" import ( "errors" "fmt" + + "go.opentelemetry.io/collector/configmap" ) var ( @@ -159,7 +161,7 @@ type validatable interface { // Unmarshallable defines an optional interface for custom configuration unmarshalling. // A configuration struct can implement this interface to override the default unmarshalling. type Unmarshallable interface { - // Unmarshal is a function that unmarshals a config.Map into the struct in a custom way. - // The config.Map for this specific component may be nil or empty if no config available. - Unmarshal(component *Map) error + // Unmarshal is a function that unmarshals a configmap.ConfigMap into the struct in a custom way. + // The configmap.ConfigMap for this specific component may be nil or empty if no config available. + Unmarshal(component *configmap.ConfigMap) error } diff --git a/config/configtest/configtest.go b/config/configtest/configtest.go index 99189b7708c..5c5df691a65 100644 --- a/config/configtest/configtest.go +++ b/config/configtest/configtest.go @@ -23,15 +23,15 @@ import ( "go.uber.org/multierr" - "go.opentelemetry.io/collector/config" "go.opentelemetry.io/collector/config/mapprovider/filemapprovider" + "go.opentelemetry.io/collector/configmap" ) // The regular expression for valid config field tag. var configFieldTagRegExp = regexp.MustCompile("^[a-z0-9][a-z0-9_]*$") -// LoadConfigMap loads a config.Map from file, and does NOT validate the configuration. -func LoadConfigMap(fileName string) (*config.Map, error) { +// LoadConfigMap loads a configmap.ConfigMap from file, and does NOT validate the configuration. +func LoadConfigMap(fileName string) (*configmap.ConfigMap, error) { ret, err := filemapprovider.New().Retrieve(context.Background(), "file:"+fileName, nil) if err != nil { return nil, err diff --git a/config/configunmarshaler/doc.go b/config/configunmarshaler/doc.go index d2a55f05223..28b6378dc81 100644 --- a/config/configunmarshaler/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 configunmarshaler implements configuration unmarshalling from a config.Map. +// Package configunmarshaler implements configuration unmarshalling from a configmap.Map. // The implementation relies on registered factories that allow creating // default configuration for each type of receiver/exporter/processor. package configunmarshaler // import "go.opentelemetry.io/collector/config/configunmarshaler" diff --git a/config/deprecated.go b/config/deprecated.go new file mode 100644 index 00000000000..247b41c35b2 --- /dev/null +++ b/config/deprecated.go @@ -0,0 +1,51 @@ +// 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 config // import "go.opentelemetry.io/collector/config" + +import ( + "go.opentelemetry.io/collector/configmap" +) + +const ( + // Deprecated: [v0.50.0] use configmap.KeyDelimiter + KeyDelimiter = configmap.KeyDelimiter +) + +// Deprecated: [v0.50.0] use configmap.KeyDelimiter +type MapConverterFunc = configmap.ConverterFunc + +// Deprecated: [v0.50.0] use configmap.ConfigMap +type Map = configmap.ConfigMap + +// Deprecated: [v0.50.0] use configmap.New +var NewMap = configmap.New + +// Deprecated: [v0.50.0] use configmap.NewFromStringMap +var NewMapFromStringMap = configmap.NewFromStringMap + +// Deprecated: [v0.50.0] use configmap.Provider +type MapProvider = configmap.Provider + +// Deprecated: [v0.50.0] use configmap.Retrieved +type Retrieved = configmap.Retrieved + +// Deprecated: [v0.50.0] use configmap.WatcherFunc +type WatcherFunc = configmap.WatcherFunc + +// Deprecated: [v0.50.0] use configmap.CloseFunc +type CloseFunc = configmap.CloseFunc + +// Deprecated: [v0.50.0] use configmap.ChangeEvent +type ChangeEvent = configmap.ChangeEvent diff --git a/config/experimental/configsource/component.go b/config/experimental/configsource/component.go index 9bb7945e569..b415f123c7a 100644 --- a/config/experimental/configsource/component.go +++ b/config/experimental/configsource/component.go @@ -18,7 +18,7 @@ import ( "context" "errors" - "go.opentelemetry.io/collector/config" + "go.opentelemetry.io/collector/configmap" ) // ErrSessionClosed is returned by WatchForUpdate functions when its parent Session @@ -54,7 +54,7 @@ type ConfigSource interface { // // The selector is a string that is required on all invocations, the params are optional. Each // implementation handles the generic params according to their requirements. - Retrieve(ctx context.Context, selector string, paramsConfigMap *config.Map) (Retrieved, error) + Retrieve(ctx context.Context, selector string, paramsConfigMap *configmap.ConfigMap) (Retrieved, error) // Close signals that the configuration for which it was used to retrieve values is no longer in use // and the object should close and release any watchers that it may have created. diff --git a/config/exporter.go b/config/exporter.go index c13c7db2837..547f9095413 100644 --- a/config/exporter.go +++ b/config/exporter.go @@ -25,7 +25,7 @@ type Exporter interface { // UnmarshalExporter helper function to unmarshal an Exporter config. // It checks if the config implements Unmarshallable and uses that if available, -// otherwise uses Map.UnmarshalExact, erroring if a field is nonexistent. +// otherwise uses ConfigMap.UnmarshalExact, erroring if a field is nonexistent. func UnmarshalExporter(cfgMap *Map, cfg Exporter) error { return unmarshal(cfgMap, cfg) } diff --git a/config/extension.go b/config/extension.go index 24f6cac7d24..d877a259fdf 100644 --- a/config/extension.go +++ b/config/extension.go @@ -25,7 +25,7 @@ type Extension interface { // UnmarshalExtension helper function to unmarshal an Extension config. // It checks if the config implements Unmarshallable and uses that if available, -// otherwise uses Map.UnmarshalExact, erroring if a field is nonexistent. +// otherwise uses ConfigMap.UnmarshalExact, erroring if a field is nonexistent. func UnmarshalExtension(cfgMap *Map, cfg Extension) error { return unmarshal(cfgMap, cfg) } diff --git a/config/internal/configsource/README.md b/config/internal/configsource/README.md index 15038c26203..ae56321b9b7 100644 --- a/config/internal/configsource/README.md +++ b/config/internal/configsource/README.md @@ -12,13 +12,13 @@ Below is an overview of its usage, which consists of two separate phases: 2. Watching for Updates ## Configuration Processing -The `configsource.Manager` receives as input a set of config source factories and a local `config.Map` that -will be used to generate the resulting, or effective, configuration also in the form of a `config.Map`, +The `configsource.Manager` receives as input a set of config source factories and a local `configmap.Map` that +will be used to generate the resulting, or effective, configuration also in the form of a `configmap.Map`, that can be used by code that is oblivious to the usage of config sources. ```terminal +-----------------------------------------------------+ -| config.Map | +| configmap.Map | |-----------------------------------------------------| | | | logical YAML config: | @@ -67,7 +67,7 @@ that can be used by code that is oblivious to the usage of config sources. | v +-----------------------------------------------------+ - | config.Map | + | configmap.Map | |-----------------------------------------------------| | | | logica YAML config: | diff --git a/config/internal/configsource/manager.go b/config/internal/configsource/manager.go index d2d5f53195b..07a8dbccb33 100644 --- a/config/internal/configsource/manager.go +++ b/config/internal/configsource/manager.go @@ -27,8 +27,8 @@ import ( "go.uber.org/multierr" "gopkg.in/yaml.v2" - "go.opentelemetry.io/collector/config" "go.opentelemetry.io/collector/config/experimental/configsource" + "go.opentelemetry.io/collector/configmap" ) const ( @@ -181,7 +181,7 @@ type Manager struct { // NewManager creates a new instance of a Manager to be used to inject data from // ConfigSource objects into a configuration and watch for updates on the injected // data. -func NewManager(_ *config.Map) (*Manager, error) { +func NewManager(_ *configmap.ConfigMap) (*Manager, error) { // TODO: Config sources should be extracted for the config itself, need Factories for that. return &Manager{ @@ -190,18 +190,18 @@ func NewManager(_ *config.Map) (*Manager, error) { }, nil } -// Resolve inspects the given config.Map and resolves all config sources referenced -// in the configuration, returning a config.Map in which all env vars and config sources on +// Resolve inspects the given configmap.ConfigMap and resolves all config sources referenced +// in the configuration, returning a configmap.ConfigMap in which all env vars and config sources on // the given input config map are resolved to actual literal values of the env vars or config sources. // This method must be called only once per lifetime of a Manager object. -func (m *Manager) Resolve(ctx context.Context, configMap *config.Map) (*config.Map, error) { - res := config.NewMap() +func (m *Manager) Resolve(ctx context.Context, configMap *configmap.ConfigMap) (*configmap.ConfigMap, error) { + res := configmap.New() allKeys := configMap.AllKeys() for _, k := range allKeys { if strings.HasPrefix(k, configSourcesKey) { // Remove everything under the config_sources section. The `config_sources` section // is read when loading the config sources used in the configuration, but it is not - // part of the resulting configuration returned via *config.Map. + // part of the resulting configuration returned via *configmap.ConfigMap. continue } @@ -285,7 +285,7 @@ func (m *Manager) Close(ctx context.Context) error { // parseConfigValue takes the value of a "config node" and process it recursively. The processing consists // in transforming invocations of config sources and/or environment variables into literal data that can be -// used directly from a `config.Map` object. +// used directly from a `configmap.ConfigMap` object. func (m *Manager) parseConfigValue(ctx context.Context, value interface{}) (interface{}, error) { switch v := value.(type) { case string: @@ -509,11 +509,11 @@ func newErrUnknownConfigSource(cfgSrcName string) error { // parseCfgSrcInvocation parses the original string in the configuration that has a config source // retrieve operation and return its "logical components": the config source name, the selector, and -// a config.Map to be used in this invocation of the config source. See Test_parseCfgSrcInvocation +// a configmap.ConfigMap to be used in this invocation of the config source. See Test_parseCfgSrcInvocation // for some examples of input and output. // The caller should check for error explicitly since it is possible for the // other values to have been partially set. -func parseCfgSrcInvocation(s string) (cfgSrcName, selector string, paramsConfigMap *config.Map, err error) { +func parseCfgSrcInvocation(s string) (cfgSrcName, selector string, paramsConfigMap *configmap.ConfigMap, err error) { parts := strings.SplitN(s, string(configSourceNameDelimChar), 2) if len(parts) != 2 { err = fmt.Errorf("invalid config source syntax at %q, it must have at least the config source name and a selector", s) @@ -534,7 +534,7 @@ func parseCfgSrcInvocation(s string) (cfgSrcName, selector string, paramsConfigM if err = yaml.Unmarshal([]byte(parts[1]), &data); err != nil { return } - paramsConfigMap = config.NewMapFromStringMap(data) + paramsConfigMap = configmap.NewFromStringMap(data) } default: @@ -556,7 +556,7 @@ func parseCfgSrcInvocation(s string) (cfgSrcName, selector string, paramsConfigM return cfgSrcName, selector, paramsConfigMap, err } -func parseParamsAsURLQuery(s string) (*config.Map, error) { +func parseParamsAsURLQuery(s string) (*configmap.ConfigMap, error) { values, err := url.ParseQuery(s) if err != nil { return nil, err @@ -587,7 +587,7 @@ func parseParamsAsURLQuery(s string) (*config.Map, error) { params[k] = elemSlice } } - return config.NewMapFromStringMap(params), err + return configmap.NewFromStringMap(params), err } // osExpandEnv replicate the internal behavior of os.ExpandEnv when handling env diff --git a/config/internal/configsource/manager_test.go b/config/internal/configsource/manager_test.go index 477deb61c99..057a057a632 100644 --- a/config/internal/configsource/manager_test.go +++ b/config/internal/configsource/manager_test.go @@ -24,9 +24,9 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.opentelemetry.io/collector/config" "go.opentelemetry.io/collector/config/configtest" "go.opentelemetry.io/collector/config/experimental/configsource" + "go.opentelemetry.io/collector/configmap" ) func TestConfigSourceManager_Simple(t *testing.T) { @@ -52,7 +52,7 @@ func TestConfigSourceManager_Simple(t *testing.T) { }, } - cp := config.NewMapFromStringMap(originalCfg) + cp := configmap.NewFromStringMap(originalCfg) res, err := manager.Resolve(ctx, cp) require.NoError(t, err) @@ -85,7 +85,7 @@ func TestConfigSourceManager_ResolveRemoveConfigSourceSection(t *testing.T) { "tstcfgsrc": &testConfigSource{}, }) - res, err := manager.Resolve(context.Background(), config.NewMapFromStringMap(cfg)) + res, err := manager.Resolve(context.Background(), configmap.NewFromStringMap(cfg)) require.NoError(t, err) require.NotNil(t, res) @@ -125,7 +125,7 @@ func TestConfigSourceManager_ResolveErrors(t *testing.T) { t.Run(tt.name, func(t *testing.T) { manager := newManager(tt.configSourceMap) - res, err := manager.Resolve(ctx, config.NewMapFromStringMap(tt.config)) + res, err := manager.Resolve(ctx, configmap.NewFromStringMap(tt.config)) require.Error(t, err) require.Nil(t, res) require.NoError(t, manager.Close(ctx)) @@ -230,7 +230,7 @@ func TestConfigSourceManager_ParamsHandling(t *testing.T) { } // Set OnRetrieve to check if the parameters were parsed as expected. - tstCfgSrc.OnRetrieve = func(ctx context.Context, selector string, paramsConfigMap *config.Map) error { + tstCfgSrc.OnRetrieve = func(ctx context.Context, selector string, paramsConfigMap *configmap.ConfigMap) error { paramsValue := (interface{})(nil) if paramsConfigMap != nil { paramsValue = paramsConfigMap.ToStringMap() @@ -280,7 +280,7 @@ func TestConfigSourceManager_WatchForUpdate(t *testing.T) { }, } - cp := config.NewMapFromStringMap(originalCfg) + cp := configmap.NewFromStringMap(originalCfg) _, err := manager.Resolve(ctx, cp) require.NoError(t, err) @@ -334,7 +334,7 @@ func TestConfigSourceManager_MultipleWatchForUpdate(t *testing.T) { }, } - cp := config.NewMapFromStringMap(originalCfg) + cp := configmap.NewFromStringMap(originalCfg) _, err := manager.Resolve(ctx, cp) require.NoError(t, err) @@ -368,7 +368,7 @@ func TestConfigSourceManager_EnvVarHandling(t *testing.T) { } // Intercept "params_key" and create an entry with the params themselves. - tstCfgSrc.OnRetrieve = func(ctx context.Context, selector string, paramsConfigMap *config.Map) error { + tstCfgSrc.OnRetrieve = func(ctx context.Context, selector string, paramsConfigMap *configmap.ConfigMap) error { if selector == "params_key" { tstCfgSrc.ValueMap[selector] = valueEntry{Value: paramsConfigMap.ToStringMap()} } @@ -633,7 +633,7 @@ type testConfigSource struct { ErrOnRetrieve error ErrOnClose error - OnRetrieve func(ctx context.Context, selector string, paramsConfigMap *config.Map) error + OnRetrieve func(ctx context.Context, selector string, paramsConfigMap *configmap.ConfigMap) error } type valueEntry struct { @@ -643,7 +643,7 @@ type valueEntry struct { var _ configsource.ConfigSource = (*testConfigSource)(nil) -func (t *testConfigSource) Retrieve(ctx context.Context, selector string, paramsConfigMap *config.Map) (configsource.Retrieved, error) { +func (t *testConfigSource) Retrieve(ctx context.Context, selector string, paramsConfigMap *configmap.ConfigMap) (configsource.Retrieved, error) { if t.OnRetrieve != nil { if err := t.OnRetrieve(ctx, selector, paramsConfigMap); err != nil { return nil, err diff --git a/config/mapconverter/expandmapconverter/expand.go b/config/mapconverter/expandmapconverter/expand.go index 8fe624fdb12..b86503b86fc 100644 --- a/config/mapconverter/expandmapconverter/expand.go +++ b/config/mapconverter/expandmapconverter/expand.go @@ -18,14 +18,14 @@ import ( "context" "os" - "go.opentelemetry.io/collector/config" + "go.opentelemetry.io/collector/configmap" ) -// New returns a config.MapConverterFunc, that expands all environment variables for a given config.Map. +// New returns a configmap.ConverterFunc, that expands all environment variables for a given configmap.ConfigMap. // // Notice: This API is experimental. -func New() config.MapConverterFunc { - return func(_ context.Context, cfgMap *config.Map) error { +func New() configmap.ConverterFunc { + return func(_ context.Context, cfgMap *configmap.ConfigMap) error { for _, k := range cfgMap.AllKeys() { cfgMap.Set(k, expandStringValues(cfgMap.Get(k))) } diff --git a/config/mapconverter/expandmapconverter/expand_test.go b/config/mapconverter/expandmapconverter/expand_test.go index 5bacde7fecd..aa04a7ef5c7 100644 --- a/config/mapconverter/expandmapconverter/expand_test.go +++ b/config/mapconverter/expandmapconverter/expand_test.go @@ -22,8 +22,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.opentelemetry.io/collector/config" "go.opentelemetry.io/collector/config/configtest" + "go.opentelemetry.io/collector/configmap" ) func TestNewExpandConverter(t *testing.T) { @@ -66,7 +66,7 @@ func TestNewExpandConverter_EscapedMaps(t *testing.T) { const receiverExtraMapValue = "some map value" t.Setenv("MAP_VALUE", receiverExtraMapValue) - cfgMap := config.NewMapFromStringMap( + cfgMap := configmap.NewFromStringMap( map[string]interface{}{ "test_string_map": map[string]interface{}{ "recv": "$MAP_VALUE", diff --git a/config/mapconverter/overwritepropertiesmapconverter/properties.go b/config/mapconverter/overwritepropertiesmapconverter/properties.go index 93e230fda2a..a0e26545c10 100644 --- a/config/mapconverter/overwritepropertiesmapconverter/properties.go +++ b/config/mapconverter/overwritepropertiesmapconverter/properties.go @@ -22,22 +22,22 @@ import ( "github.com/knadh/koanf/maps" "github.com/magiconair/properties" - "go.opentelemetry.io/collector/config" + "go.opentelemetry.io/collector/configmap" ) -// New returns a config.MapConverterFunc, that overrides all the given properties into the +// New returns a configmap.ConverterFunc, that overrides all the given properties into the // input map. // // Properties must follow the Java properties format, key-value list separated by equal sign with a "." // as key delimiter. // ["processors.batch.timeout=2s", "processors.batch/foo.timeout=3s"] -func New(properties []string) config.MapConverterFunc { - return func(_ context.Context, cfgMap *config.Map) error { +func New(properties []string) configmap.ConverterFunc { + return func(_ context.Context, cfgMap *configmap.ConfigMap) error { return convert(properties, cfgMap) } } -func convert(propsStr []string, cfgMap *config.Map) error { +func convert(propsStr []string, cfgMap *configmap.ConfigMap) error { if len(propsStr) == 0 { return nil } @@ -55,7 +55,7 @@ func convert(propsStr []string, cfgMap *config.Map) error { return err } - // Create a map manually instead of using properties.Map() to not expand the env vars. + // Create a map manually instead of using properties.ConfigMap() to not expand the env vars. parsed := make(map[string]interface{}, props.Len()) for _, key := range props.Keys() { value, _ := props.Get(key) @@ -63,5 +63,5 @@ func convert(propsStr []string, cfgMap *config.Map) error { } prop := maps.Unflatten(parsed, ".") - return cfgMap.Merge(config.NewMapFromStringMap(prop)) + return cfgMap.Merge(configmap.NewFromStringMap(prop)) } diff --git a/config/mapconverter/overwritepropertiesmapconverter/properties_test.go b/config/mapconverter/overwritepropertiesmapconverter/properties_test.go index cf18cca7eb9..dc37a9aad76 100644 --- a/config/mapconverter/overwritepropertiesmapconverter/properties_test.go +++ b/config/mapconverter/overwritepropertiesmapconverter/properties_test.go @@ -21,12 +21,12 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.opentelemetry.io/collector/config" + "go.opentelemetry.io/collector/configmap" ) func TestOverwritePropertiesConverter_Empty(t *testing.T) { pmp := New(nil) - cfgMap := config.NewMapFromStringMap(map[string]interface{}{"foo": "bar"}) + cfgMap := configmap.NewFromStringMap(map[string]interface{}{"foo": "bar"}) assert.NoError(t, pmp(context.Background(), cfgMap)) assert.Equal(t, map[string]interface{}{"foo": "bar"}, cfgMap.ToStringMap()) } @@ -40,7 +40,7 @@ func TestOverwritePropertiesConverter(t *testing.T) { } pmp := New(props) - cfgMap := config.NewMap() + cfgMap := configmap.New() require.NoError(t, pmp(context.Background(), cfgMap)) keys := cfgMap.AllKeys() assert.Len(t, keys, 4) @@ -52,6 +52,6 @@ func TestOverwritePropertiesConverter(t *testing.T) { func TestOverwritePropertiesConverter_InvalidProperty(t *testing.T) { pmp := New([]string{"=2s"}) - cfgMap := config.NewMap() + cfgMap := configmap.New() assert.Error(t, pmp(context.Background(), cfgMap)) } diff --git a/config/mapprovider/envmapprovider/mapprovider.go b/config/mapprovider/envmapprovider/mapprovider.go index e1917f82314..93b286d6f7f 100644 --- a/config/mapprovider/envmapprovider/mapprovider.go +++ b/config/mapprovider/envmapprovider/mapprovider.go @@ -22,33 +22,33 @@ import ( "gopkg.in/yaml.v2" - "go.opentelemetry.io/collector/config" + "go.opentelemetry.io/collector/configmap" ) const schemeName = "env" type mapProvider struct{} -// New returns a new config.MapProvider that reads the configuration from the given environment variable. +// New returns a new configmap.Provider that reads the configuration from the given environment variable. // // This Provider supports "env" scheme, and can be called with a selector: // `env:NAME_OF_ENVIRONMENT_VARIABLE` -func New() config.MapProvider { +func New() configmap.Provider { return &mapProvider{} } -func (emp *mapProvider) Retrieve(_ context.Context, uri string, _ config.WatcherFunc) (config.Retrieved, error) { +func (emp *mapProvider) Retrieve(_ context.Context, uri string, _ configmap.WatcherFunc) (configmap.Retrieved, error) { if !strings.HasPrefix(uri, schemeName+":") { - return config.Retrieved{}, fmt.Errorf("%v uri is not supported by %v provider", uri, schemeName) + return configmap.Retrieved{}, fmt.Errorf("%v uri is not supported by %v provider", uri, schemeName) } content := os.Getenv(uri[len(schemeName)+1:]) var data map[string]interface{} if err := yaml.Unmarshal([]byte(content), &data); err != nil { - return config.Retrieved{}, fmt.Errorf("unable to parse yaml: %w", err) + return configmap.Retrieved{}, fmt.Errorf("unable to parse yaml: %w", err) } - return config.NewRetrievedFromMap(config.NewMapFromStringMap(data)), nil + return configmap.NewRetrievedFromMap(configmap.NewFromStringMap(data)), nil } func (*mapProvider) Scheme() string { diff --git a/config/mapprovider/envmapprovider/mapprovider_test.go b/config/mapprovider/envmapprovider/mapprovider_test.go index 1c04c18e504..c17ee719902 100644 --- a/config/mapprovider/envmapprovider/mapprovider_test.go +++ b/config/mapprovider/envmapprovider/mapprovider_test.go @@ -23,7 +23,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.opentelemetry.io/collector/config" + "go.opentelemetry.io/collector/configmap" ) const envSchemePrefix = schemeName + ":" @@ -64,7 +64,7 @@ func TestEnv(t *testing.T) { require.NoError(t, err) retMap, err := ret.AsMap() assert.NoError(t, err) - expectedMap := config.NewMapFromStringMap(map[string]interface{}{ + expectedMap := configmap.NewFromStringMap(map[string]interface{}{ "processors::batch": nil, "exporters::otlp::endpoint": "localhost:4317", }) diff --git a/config/mapprovider/filemapprovider/mapprovider.go b/config/mapprovider/filemapprovider/mapprovider.go index 51c03f4aac8..03373893fef 100644 --- a/config/mapprovider/filemapprovider/mapprovider.go +++ b/config/mapprovider/filemapprovider/mapprovider.go @@ -23,14 +23,14 @@ import ( "gopkg.in/yaml.v2" - "go.opentelemetry.io/collector/config" + "go.opentelemetry.io/collector/configmap" ) const schemeName = "file" type mapProvider struct{} -// New returns a new config.MapProvider that reads the configuration from a file. +// New returns a new configmap.Provider that reads the configuration from a file. // // This Provider supports "file" scheme, and can be called with a "uri" that follows: // file-uri = "file:" local-path @@ -43,27 +43,27 @@ type mapProvider struct{} // `file:/path/to/file` - absolute path (unix, windows) // `file:c:/path/to/file` - absolute path including drive-letter (windows) // `file:c:\path\to\file` - absolute path including drive-letter (windows) -func New() config.MapProvider { +func New() configmap.Provider { return &mapProvider{} } -func (fmp *mapProvider) Retrieve(_ context.Context, uri string, _ config.WatcherFunc) (config.Retrieved, error) { +func (fmp *mapProvider) Retrieve(_ context.Context, uri string, _ configmap.WatcherFunc) (configmap.Retrieved, error) { if !strings.HasPrefix(uri, schemeName+":") { - return config.Retrieved{}, fmt.Errorf("%v uri is not supported by %v provider", uri, schemeName) + return configmap.Retrieved{}, fmt.Errorf("%v uri is not supported by %v provider", uri, schemeName) } // Clean the path before using it. content, err := ioutil.ReadFile(filepath.Clean(uri[len(schemeName)+1:])) if err != nil { - return config.Retrieved{}, fmt.Errorf("unable to read the file %v: %w", uri, err) + return configmap.Retrieved{}, fmt.Errorf("unable to read the file %v: %w", uri, err) } var data map[string]interface{} if err = yaml.Unmarshal(content, &data); err != nil { - return config.Retrieved{}, fmt.Errorf("unable to parse yaml: %w", err) + return configmap.Retrieved{}, fmt.Errorf("unable to parse yaml: %w", err) } - return config.NewRetrievedFromMap(config.NewMapFromStringMap(data)), nil + return configmap.NewRetrievedFromMap(configmap.NewFromStringMap(data)), nil } func (*mapProvider) Scheme() string { diff --git a/config/mapprovider/filemapprovider/mapprovider_test.go b/config/mapprovider/filemapprovider/mapprovider_test.go index 443ee7972cc..7b5581d497e 100644 --- a/config/mapprovider/filemapprovider/mapprovider_test.go +++ b/config/mapprovider/filemapprovider/mapprovider_test.go @@ -23,7 +23,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.opentelemetry.io/collector/config" + "go.opentelemetry.io/collector/configmap" ) const fileSchemePrefix = schemeName + ":" @@ -66,7 +66,7 @@ func TestRelativePath(t *testing.T) { require.NoError(t, err) retMap, err := ret.AsMap() assert.NoError(t, err) - expectedMap := config.NewMapFromStringMap(map[string]interface{}{ + expectedMap := configmap.NewFromStringMap(map[string]interface{}{ "processors::batch": nil, "exporters::otlp::endpoint": "localhost:4317", }) @@ -80,7 +80,7 @@ func TestAbsolutePath(t *testing.T) { require.NoError(t, err) retMap, err := ret.AsMap() assert.NoError(t, err) - expectedMap := config.NewMapFromStringMap(map[string]interface{}{ + expectedMap := configmap.NewFromStringMap(map[string]interface{}{ "processors::batch": nil, "exporters::otlp::endpoint": "localhost:4317", }) diff --git a/config/mapprovider/yamlmapprovider/mapprovider.go b/config/mapprovider/yamlmapprovider/mapprovider.go index 9b2aaf5d4d8..b03eaa2ce5f 100644 --- a/config/mapprovider/yamlmapprovider/mapprovider.go +++ b/config/mapprovider/yamlmapprovider/mapprovider.go @@ -21,14 +21,14 @@ import ( "gopkg.in/yaml.v2" - "go.opentelemetry.io/collector/config" + "go.opentelemetry.io/collector/configmap" ) const schemeName = "yaml" type mapProvider struct{} -// New returns a new config.MapProvider that allows to provide yaml bytes. +// New returns a new configmap.Provider that allows to provide yaml bytes. // // This Provider supports "yaml" scheme, and can be called with a "uri" that follows: // bytes-uri = "yaml:" yaml-bytes @@ -36,21 +36,21 @@ type mapProvider struct{} // Examples: // `yaml:processors::batch::timeout: 2s` // `yaml:processors::batch/foo::timeout: 3s` -func New() config.MapProvider { +func New() configmap.Provider { return &mapProvider{} } -func (s *mapProvider) Retrieve(_ context.Context, uri string, _ config.WatcherFunc) (config.Retrieved, error) { +func (s *mapProvider) Retrieve(_ context.Context, uri string, _ configmap.WatcherFunc) (configmap.Retrieved, error) { if !strings.HasPrefix(uri, schemeName+":") { - return config.Retrieved{}, fmt.Errorf("%v uri is not supported by %v provider", uri, schemeName) + return configmap.Retrieved{}, fmt.Errorf("%v uri is not supported by %v provider", uri, schemeName) } var data map[string]interface{} if err := yaml.Unmarshal([]byte(uri[len(schemeName)+1:]), &data); err != nil { - return config.Retrieved{}, fmt.Errorf("unable to parse yaml: %w", err) + return configmap.Retrieved{}, fmt.Errorf("unable to parse yaml: %w", err) } - return config.NewRetrievedFromMap(config.NewMapFromStringMap(data)), nil + return configmap.NewRetrievedFromMap(configmap.NewFromStringMap(data)), nil } func (*mapProvider) Scheme() string { diff --git a/config/processor.go b/config/processor.go index bde13f68736..c9fe408ce0b 100644 --- a/config/processor.go +++ b/config/processor.go @@ -25,7 +25,7 @@ type Processor interface { // UnmarshalProcessor helper function to unmarshal a Processor config. // It checks if the config implements Unmarshallable and uses that if available, -// otherwise uses Map.UnmarshalExact, erroring if a field is nonexistent. +// otherwise uses ConfigMap.UnmarshalExact, erroring if a field is nonexistent. func UnmarshalProcessor(cfgMap *Map, cfg Processor) error { return unmarshal(cfgMap, cfg) } diff --git a/config/receiver.go b/config/receiver.go index 6974efbc46b..e5f63ee36c1 100644 --- a/config/receiver.go +++ b/config/receiver.go @@ -25,7 +25,7 @@ type Receiver interface { // UnmarshalReceiver helper function to unmarshal a Receiver config. // It checks if the config implements Unmarshallable and uses that if available, -// otherwise uses Map.UnmarshalExact, erroring if a field is nonexistent. +// otherwise uses ConfigMap.UnmarshalExact, erroring if a field is nonexistent. func UnmarshalReceiver(cfgMap *Map, cfg Receiver) error { return unmarshal(cfgMap, cfg) } diff --git a/config/configmap.go b/configmap/configmap.go similarity index 73% rename from config/configmap.go rename to configmap/configmap.go index 2fcef916836..3d27a6616fb 100644 --- a/config/configmap.go +++ b/configmap/configmap.go @@ -12,10 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -package config // import "go.opentelemetry.io/collector/config" +package configmap // import "go.opentelemetry.io/collector/configmap" import ( "context" + "encoding" "fmt" "reflect" @@ -30,38 +31,38 @@ const ( KeyDelimiter = "::" ) -// MapConverterFunc is a converter function for the config.Map that allows distributions +// ConverterFunc is a converter function for the configmap.ConfigMap that allows distributions // (in the future components as well) to build backwards compatible config converters. -type MapConverterFunc func(context.Context, *Map) error +type ConverterFunc func(context.Context, *ConfigMap) error -// NewMap creates a new empty config.Map instance. -func NewMap() *Map { - return &Map{k: koanf.New(KeyDelimiter)} +// New creates a new empty configmap.ConfigMap instance. +func New() *ConfigMap { + return &ConfigMap{k: koanf.New(KeyDelimiter)} } -// NewMapFromStringMap creates a config.Map from a map[string]interface{}. -func NewMapFromStringMap(data map[string]interface{}) *Map { - p := NewMap() +// NewFromStringMap creates a configmap.ConfigMap from a map[string]interface{}. +func NewFromStringMap(data map[string]interface{}) *ConfigMap { + p := New() // Cannot return error because the koanf instance is empty. _ = p.k.Load(confmap.Provider(data, KeyDelimiter), nil) return p } -// Map represents the raw configuration map for the OpenTelemetry Collector. -// The config.Map can be unmarshalled into the Collector's config using the "configunmarshaler" package. -type Map struct { +// ConfigMap represents the raw configuration map for the OpenTelemetry Collector. +// The configmap.ConfigMap can be unmarshalled into the Collector's config using the "configunmarshaler" package. +type ConfigMap struct { k *koanf.Koanf } // AllKeys returns all keys holding a value, regardless of where they are set. // Nested keys are returned with a KeyDelimiter separator. -func (l *Map) AllKeys() []string { +func (l *ConfigMap) AllKeys() []string { return l.k.Keys() } // Unmarshal unmarshalls the config into a struct. // Tags on the fields of the structure must be properly set. -func (l *Map) Unmarshal(rawVal interface{}) error { +func (l *ConfigMap) Unmarshal(rawVal interface{}) error { decoder, err := mapstructure.NewDecoder(decoderConfig(rawVal)) if err != nil { return err @@ -70,7 +71,7 @@ func (l *Map) Unmarshal(rawVal interface{}) error { } // UnmarshalExact unmarshalls the config into a struct, erroring if a field is nonexistent. -func (l *Map) UnmarshalExact(rawVal interface{}) error { +func (l *ConfigMap) UnmarshalExact(rawVal interface{}) error { dc := decoderConfig(rawVal) dc.ErrorUnused = true decoder, err := mapstructure.NewDecoder(dc) @@ -81,12 +82,12 @@ func (l *Map) UnmarshalExact(rawVal interface{}) error { } // Get can retrieve any value given the key to use. -func (l *Map) Get(key string) interface{} { +func (l *ConfigMap) Get(key string) interface{} { return l.k.Get(key) } // Set sets the value for the key. -func (l *Map) Set(key string, value interface{}) { +func (l *ConfigMap) Set(key string, value interface{}) { // koanf doesn't offer a direct setting mechanism so merging is required. merged := koanf.New(KeyDelimiter) _ = merged.Load(confmap.Provider(map[string]interface{}{key: value}, KeyDelimiter), nil) @@ -96,34 +97,34 @@ func (l *Map) Set(key string, value interface{}) { // IsSet checks to see if the key has been set in any of the data locations. // IsSet is case-insensitive for a key. -func (l *Map) IsSet(key string) bool { +func (l *ConfigMap) IsSet(key string) bool { return l.k.Exists(key) } // Merge merges the input given configuration into the existing config. // Note that the given map may be modified. -func (l *Map) Merge(in *Map) error { +func (l *ConfigMap) Merge(in *ConfigMap) error { return l.k.Merge(in.k) } -// Sub returns new Map instance representing a sub-config of this instance. -// It returns an error is the sub-config is not a map[string]interface{} (use Get()), and an empty Map if none exists. -func (l *Map) Sub(key string) (*Map, error) { +// Sub returns new ConfigMap instance representing a sub-config of this instance. +// It returns an error is the sub-config is not a map[string]interface{} (use Get()), and an empty ConfigMap if none exists. +func (l *ConfigMap) Sub(key string) (*ConfigMap, error) { // Code inspired by the koanf "Cut" func, but returns an error instead of empty map for unsupported sub-config type. data := l.Get(key) if data == nil { - return NewMap(), nil + return New(), nil } if v, ok := data.(map[string]interface{}); ok { - return NewMapFromStringMap(v), nil + return NewFromStringMap(v), nil } return nil, fmt.Errorf("unexpected sub-config value kind for key:%s value:%v kind:%v)", key, data, reflect.TypeOf(data).Kind()) } // ToStringMap creates a map[string]interface{} from a Parser. -func (l *Map) ToStringMap() map[string]interface{} { +func (l *ConfigMap) ToStringMap() map[string]interface{} { return maps.Unflatten(l.k.All(), KeyDelimiter) } @@ -140,22 +141,14 @@ func decoderConfig(result interface{}) *mapstructure.DecoderConfig { WeaklyTypedInput: true, DecodeHook: mapstructure.ComposeDecodeHookFunc( expandNilStructPointersFunc, - stringToSliceHookFunc, + mapstructure.StringToSliceHookFunc(","), mapStringToMapComponentIDHookFunc, - stringToTimeDurationHookFunc, - textUnmarshallerHookFunc, + mapstructure.StringToTimeDurationHookFunc(), + mapstructure.TextUnmarshallerHookFunc(), ), } } -var ( - stringToSliceHookFunc = mapstructure.StringToSliceHookFunc(",") - stringToTimeDurationHookFunc = mapstructure.StringToTimeDurationHookFunc() - textUnmarshallerHookFunc = mapstructure.TextUnmarshallerHookFunc() - - componentIDType = reflect.TypeOf(NewComponentID("foo")) -) - // In cases where a config has a mapping of something to a struct pointers // we want nil values to resolve to a pointer to the zero value of the // underlying struct just as we want nil values of a mapping of something @@ -201,20 +194,25 @@ var mapStringToMapComponentIDHookFunc = func(f reflect.Type, t reflect.Type, dat return data, nil } - if t.Kind() != reflect.Map || t.Key() != componentIDType { + if t.Kind() != reflect.Map { return data, nil } - m := make(map[ComponentID]interface{}) - for k, v := range data.(map[string]interface{}) { - id, err := NewComponentIDFromString(k) - if err != nil { + if _, ok := reflect.New(t.Key()).Interface().(encoding.TextUnmarshaler); !ok { + return data, nil + } + + m := reflect.MakeMap(reflect.MapOf(t.Key(), reflect.TypeOf(true))) + for k := range data.(map[string]interface{}) { + tKey := reflect.New(t.Key()) + if err := tKey.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(k)); err != nil { return nil, err } - if _, ok := m[id]; ok { - return nil, fmt.Errorf("duplicate name %q after trimming spaces %v", k, id) + + if m.MapIndex(reflect.Indirect(tKey)).IsValid() { + return nil, fmt.Errorf("duplicate name %q after trimming spaces %v", k, tKey) } - m[id] = v + m.SetMapIndex(reflect.Indirect(tKey), reflect.ValueOf(true)) } - return m, nil + return data, nil } diff --git a/config/configmap_test.go b/configmap/configmap_test.go similarity index 94% rename from config/configmap_test.go rename to configmap/configmap_test.go index dc54cef6b19..b65d48ef657 100644 --- a/config/configmap_test.go +++ b/configmap/configmap_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package config +package configmap import ( "fmt" @@ -26,7 +26,7 @@ import ( ) func TestToStringMap_WithSet(t *testing.T) { - parser := NewMap() + parser := New() parser.Set("key::embedded", int64(123)) assert.Equal(t, map[string]interface{}{"key": map[string]interface{}{"embedded": int64(123)}}, parser.ToStringMap()) } @@ -109,8 +109,8 @@ func TestToStringMap(t *testing.T) { } } -// newMapFromFile creates a new config.Map by reading the given file. -func newMapFromFile(fileName string) (*Map, error) { +// newMapFromFile creates a new configmap.ConfigMap by reading the given file. +func newMapFromFile(fileName string) (*ConfigMap, error) { content, err := ioutil.ReadFile(filepath.Clean(fileName)) if err != nil { return nil, fmt.Errorf("unable to read the file %v: %w", fileName, err) @@ -121,7 +121,7 @@ func newMapFromFile(fileName string) (*Map, error) { return nil, fmt.Errorf("unable to parse yaml: %w", err) } - return NewMapFromStringMap(data), nil + return NewFromStringMap(data), nil } func TestExpandNilStructPointersFunc(t *testing.T) { @@ -132,7 +132,7 @@ func TestExpandNilStructPointersFunc(t *testing.T) { "struct": nil, }, } - parser := NewMapFromStringMap(stringMap) + parser := NewFromStringMap(stringMap) cfg := &TestConfig{} assert.Nil(t, cfg.Struct) assert.NoError(t, parser.UnmarshalExact(cfg)) @@ -152,7 +152,7 @@ func TestExpandNilStructPointersFunc_DefaultNotNilConfigNil(t *testing.T) { "struct": nil, }, } - parser := NewMapFromStringMap(stringMap) + parser := NewFromStringMap(stringMap) varBool := true s1 := &Struct{Name: "s1"} s2 := &Struct{Name: "s2"} diff --git a/config/mapprovider.go b/configmap/provider.go similarity index 85% rename from config/mapprovider.go rename to configmap/provider.go index d7372c45b30..cc1a63e7456 100644 --- a/config/mapprovider.go +++ b/configmap/provider.go @@ -12,28 +12,28 @@ // See the License for the specific language governing permissions and // limitations under the License. -package config // import "go.opentelemetry.io/collector/config" +package configmap // import "go.opentelemetry.io/collector/configmap" import ( "context" ) -// MapProvider is an interface that helps to retrieve a config map and watch for any +// Provider is an interface that helps to retrieve a config map and watch for any // changes to the config map. Implementations may load the config from a file, // a database or any other source. // // The typical usage is the following: // -// r, err := mapProvider.Retrieve("file:/path/to/config") -// // Use r.Map; wait for watcher to be called. +// r, err := provider.Retrieve("file:/path/to/config") +// // Use r.ConfigMap; wait for watcher to be called. // r.Close() -// r, err = mapProvider.Retrieve("file:/path/to/config") -// // Use r.Map; wait for watcher to be called. +// r, err = provider.Retrieve("file:/path/to/config") +// // Use r.ConfigMap; wait for watcher to be called. // r.Close() // // repeat retrieve/wait/close cycle until it is time to shut down the Collector process. // // ... -// mapProvider.Shutdown() -type MapProvider interface { +// provider.Shutdown() +type Provider interface { // Retrieve goes to the configuration source and retrieves the selected data which // contains the value to be injected in the configuration and the corresponding watcher that // will be used to monitor for updates of the retrieved value. @@ -82,7 +82,7 @@ type ChangeEvent struct { // Retrieved holds the result of a call to the Retrieve method of a Provider object. type Retrieved struct { // Deprecated: [v0.50.0] Use NewRetrievedFromMap to initialize, and Retrieved.AsMap to access. - *Map + *ConfigMap // Deprecated: [v0.50.0] Use NewRetrievedFromMap to initialize, and Retrieved.Close to access. CloseFunc } @@ -102,22 +102,22 @@ func WithRetrievedClose(closeFunc CloseFunc) RetrievedOption { } } -// NewRetrievedFromMap returns a new Retrieved instance that contains a Map data. -// * cfgMap the Map that will be merged to the given map in the MergeTo. +// NewRetrievedFromMap returns a new Retrieved instance that contains a ConfigMap data. +// * cfgMap the ConfigMap that will be merged to the given map in the MergeTo. // * CloseFunc specifies a function to be invoked when the configuration for which it was // used to retrieve values is no longer in use and should close and release any watchers // that it may have created. -func NewRetrievedFromMap(cfgMap *Map, opts ...RetrievedOption) Retrieved { +func NewRetrievedFromMap(cfgMap *ConfigMap, opts ...RetrievedOption) Retrieved { set := retrievedSettings{} for _, opt := range opts { opt(&set) } - return Retrieved{Map: cfgMap, CloseFunc: set.closeFunc} + return Retrieved{ConfigMap: cfgMap, CloseFunc: set.closeFunc} } -// AsMap returns the retrieved configuration parsed as a Map. -func (r Retrieved) AsMap() (*Map, error) { - return r.Map, nil +// AsMap returns the retrieved configuration parsed as a ConfigMap. +func (r Retrieved) AsMap() (*ConfigMap, error) { + return r.ConfigMap, nil } // Close and release any watchers that MapProvider.Retrieve may have created. diff --git a/config/mapprovider_test.go b/configmap/provider_test.go similarity index 95% rename from config/mapprovider_test.go rename to configmap/provider_test.go index ece23f2502f..665d2397001 100644 --- a/config/mapprovider_test.go +++ b/configmap/provider_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package config +package configmap import ( "context" @@ -24,7 +24,7 @@ import ( ) func TestNewRetrievedFromMap(t *testing.T) { - cfgMap := NewMap() + cfgMap := New() ret := NewRetrievedFromMap(cfgMap) retMap, err := ret.AsMap() require.NoError(t, err) @@ -34,7 +34,7 @@ func TestNewRetrievedFromMap(t *testing.T) { func TestNewRetrievedFromMapWithOptions(t *testing.T) { want := errors.New("my error") - cfgMap := NewMap() + cfgMap := New() ret := NewRetrievedFromMap(cfgMap, WithRetrievedClose(func(context.Context) error { return want })) retMap, err := ret.AsMap() require.NoError(t, err) diff --git a/config/testdata/basic_types.yaml b/configmap/testdata/basic_types.yaml similarity index 100% rename from config/testdata/basic_types.yaml rename to configmap/testdata/basic_types.yaml diff --git a/config/testdata/config.yaml b/configmap/testdata/config.yaml similarity index 100% rename from config/testdata/config.yaml rename to configmap/testdata/config.yaml diff --git a/config/testdata/embedded_keys.yaml b/configmap/testdata/embedded_keys.yaml similarity index 100% rename from config/testdata/embedded_keys.yaml rename to configmap/testdata/embedded_keys.yaml diff --git a/go.sum b/go.sum index 356466cc27d..a6ef92ab7ea 100644 --- a/go.sum +++ b/go.sum @@ -67,7 +67,6 @@ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kB github.com/cenkalti/backoff/v4 v4.1.3 h1:cFAlzYUlVYDysBEH2T5hyJZMh3+5+WCBvSnK6Q8UtC4= github.com/cenkalti/backoff/v4 v4.1.3/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= -github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= diff --git a/internal/configunmarshaler/defaultunmarshaler.go b/internal/configunmarshaler/defaultunmarshaler.go index 4de43165c3d..37f183fc0d4 100644 --- a/internal/configunmarshaler/defaultunmarshaler.go +++ b/internal/configunmarshaler/defaultunmarshaler.go @@ -23,12 +23,13 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config" "go.opentelemetry.io/collector/config/configtelemetry" + "go.opentelemetry.io/collector/configmap" ) -// ConfigUnmarshaler is the interface that unmarshalls the collector configuration from the config.Map. +// ConfigUnmarshaler is the interface that unmarshalls the collector configuration from the configmap.ConfigMap. type ConfigUnmarshaler interface { // Unmarshal the configuration from the given parser and factories. - Unmarshal(v *config.Map, factories component.Factories) (*config.Config, error) + Unmarshal(v *configmap.ConfigMap, factories component.Factories) (*config.Config, error) } // These are errors that can be returned by Unmarshal(). Note that error codes are not part @@ -89,9 +90,9 @@ func NewDefault() ConfigUnmarshaler { return &defaultUnmarshaler{} } -// Unmarshal the Config from a config.Map. +// Unmarshal the Config from a configmap.ConfigMap. // After the config is unmarshalled, `Validate()` must be called to validate. -func (*defaultUnmarshaler) Unmarshal(v *config.Map, factories component.Factories) (*config.Config, error) { +func (*defaultUnmarshaler) Unmarshal(v *configmap.ConfigMap, factories component.Factories) (*config.Config, error) { var cfg config.Config // Unmarshal top level sections and validate. @@ -160,7 +161,7 @@ func unmarshalExtensions(exts map[config.ComponentID]map[string]interface{}, fac // Now that the default config struct is created we can Unmarshal into it, // and it will apply user-defined config on top of the default. - if err := config.UnmarshalExtension(config.NewMapFromStringMap(value), extensionCfg); err != nil { + if err := config.UnmarshalExtension(configmap.NewFromStringMap(value), extensionCfg); err != nil { return nil, errorUnmarshalError(extensionsKeyName, id, err) } @@ -189,7 +190,7 @@ func unmarshalService(srvRaw map[string]interface{}) (config.Service, error) { }, } - if err := config.NewMapFromStringMap(srvRaw).UnmarshalExact(&srv); err != nil { + if err := configmap.NewFromStringMap(srvRaw).UnmarshalExact(&srv); err != nil { return srv, fmt.Errorf("error reading service configuration: %w", err) } @@ -209,7 +210,7 @@ func defaultServiceTelemetryMetricsSettings() config.ServiceTelemetryMetrics { } // LoadReceiver loads a receiver config from componentConfig using the provided factories. -func LoadReceiver(componentConfig *config.Map, id config.ComponentID, factory component.ReceiverFactory) (config.Receiver, error) { +func LoadReceiver(componentConfig *configmap.ConfigMap, id config.ComponentID, factory component.ReceiverFactory) (config.Receiver, error) { // Create the default config for this receiver. receiverCfg := factory.CreateDefaultConfig() receiverCfg.SetIDName(id.Name()) @@ -235,7 +236,7 @@ func unmarshalReceivers(recvs map[config.ComponentID]map[string]interface{}, fac return nil, errorUnknownType(receiversKeyName, id, reflect.ValueOf(factories).MapKeys()) } - receiverCfg, err := LoadReceiver(config.NewMapFromStringMap(value), id, factory) + receiverCfg, err := LoadReceiver(configmap.NewFromStringMap(value), id, factory) if err != nil { // LoadReceiver already wraps the error. return nil, err @@ -265,7 +266,7 @@ func unmarshalExporters(exps map[config.ComponentID]map[string]interface{}, fact // Now that the default config struct is created we can Unmarshal into it, // and it will apply user-defined config on top of the default. - if err := config.UnmarshalExporter(config.NewMapFromStringMap(value), exporterCfg); err != nil { + if err := config.UnmarshalExporter(configmap.NewFromStringMap(value), exporterCfg); err != nil { return nil, errorUnmarshalError(exportersKeyName, id, err) } @@ -293,7 +294,7 @@ func unmarshalProcessors(procs map[config.ComponentID]map[string]interface{}, fa // Now that the default config struct is created we can Unmarshal into it, // and it will apply user-defined config on top of the default. - if err := config.UnmarshalProcessor(config.NewMapFromStringMap(value), processorCfg); err != nil { + if err := config.UnmarshalProcessor(configmap.NewFromStringMap(value), processorCfg); err != nil { return nil, errorUnmarshalError(processorsKeyName, id, err) } diff --git a/internal/configunmarshaler/defaultunmarshaler_test.go b/internal/configunmarshaler/defaultunmarshaler_test.go index 55a6d8c8071..f645c4afe7c 100644 --- a/internal/configunmarshaler/defaultunmarshaler_test.go +++ b/internal/configunmarshaler/defaultunmarshaler_test.go @@ -229,7 +229,7 @@ func loadConfigFile(t *testing.T, fileName string, factories component.Factories cm, err := configtest.LoadConfigMap(fileName) require.NoError(t, err) - // Unmarshal the config from the config.Map using the given factories. + // Unmarshal the config from the configmap.ConfigMap using the given factories. return NewDefault().Unmarshal(cm, factories) } diff --git a/internal/testcomponents/example_exporter.go b/internal/testcomponents/example_exporter.go index c0cc6d0e5bf..c07fba1f0f5 100644 --- a/internal/testcomponents/example_exporter.go +++ b/internal/testcomponents/example_exporter.go @@ -19,6 +19,7 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config" + "go.opentelemetry.io/collector/configmap" "go.opentelemetry.io/collector/consumer" "go.opentelemetry.io/collector/pdata/plog" "go.opentelemetry.io/collector/pdata/pmetric" @@ -37,8 +38,8 @@ type ExampleExporter struct { ExtraListSetting []string `mapstructure:"extra_list"` } -// Unmarshal a config.Map data into the config struct -func (cfg *ExampleExporter) Unmarshal(componentParser *config.Map) error { +// Unmarshal a configmap.ConfigMap data into the config struct +func (cfg *ExampleExporter) Unmarshal(componentParser *configmap.ConfigMap) error { return componentParser.UnmarshalExact(cfg) } diff --git a/model/go.mod b/model/go.mod index bc39903dd55..57bbadf1ae1 100644 --- a/model/go.mod +++ b/model/go.mod @@ -2,23 +2,17 @@ module go.opentelemetry.io/collector/model go 1.17 -require ( - github.com/stretchr/testify v1.7.1 - go.opentelemetry.io/collector/pdata v0.49.0 -) +require go.opentelemetry.io/collector/pdata v0.49.0 require ( - github.com/davecgh/go-spew v1.1.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.2 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/net v0.0.0-20201021035429-f5854403a974 // indirect golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f // indirect golang.org/x/text v0.3.3 // indirect google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect google.golang.org/grpc v1.45.0 // indirect google.golang.org/protobuf v1.28.0 // indirect - gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect ) replace go.opentelemetry.io/collector/pdata => ../pdata diff --git a/model/go.sum b/model/go.sum index f86009915ff..896ed53f124 100644 --- a/model/go.sum +++ b/model/go.sum @@ -57,7 +57,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= @@ -139,7 +138,6 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0 google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw= google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/pdata/internal/cmd/pdatagen/internal/common_structs.go b/pdata/internal/cmd/pdatagen/internal/common_structs.go index 0888af1d8f9..a4cc454cdac 100644 --- a/pdata/internal/cmd/pdatagen/internal/common_structs.go +++ b/pdata/internal/cmd/pdatagen/internal/common_structs.go @@ -51,7 +51,7 @@ var scope = &messageValueStruct{ // This will not be generated by this class. // Defined here just to be available as returned message for the fields. var mapStruct = &sliceOfPtrs{ - structName: "Map", + structName: "ConfigMap", element: attributeKeyValue, } diff --git a/receiver/otlpreceiver/config.go b/receiver/otlpreceiver/config.go index b6b694298f7..f743c0997e5 100644 --- a/receiver/otlpreceiver/config.go +++ b/receiver/otlpreceiver/config.go @@ -20,6 +20,7 @@ import ( "go.opentelemetry.io/collector/config" "go.opentelemetry.io/collector/config/configgrpc" "go.opentelemetry.io/collector/config/confighttp" + "go.opentelemetry.io/collector/configmap" ) const ( @@ -54,8 +55,8 @@ func (cfg *Config) Validate() error { return nil } -// Unmarshal a config.Map into the config struct. -func (cfg *Config) Unmarshal(componentParser *config.Map) error { +// Unmarshal a configmap.ConfigMap into the config struct. +func (cfg *Config) Unmarshal(componentParser *configmap.ConfigMap) error { if componentParser == nil || len(componentParser.AllKeys()) == 0 { return fmt.Errorf("empty config for OTLP receiver") } @@ -65,7 +66,7 @@ func (cfg *Config) Unmarshal(componentParser *config.Map) error { return err } - // next manually search for protocols in the config.Map, if a protocol is not present it means it is disabled. + // next manually search for protocols in the configmap.ConfigMap, if a protocol is not present it means it is disabled. protocols, err := componentParser.Sub(protocolsFieldName) if err != nil { return err diff --git a/service/collector_test.go b/service/collector_test.go index d54088d17a2..b50a8952c60 100644 --- a/service/collector_test.go +++ b/service/collector_test.go @@ -36,8 +36,8 @@ import ( "go.uber.org/zap/zapcore" "go.opentelemetry.io/collector/component" - "go.opentelemetry.io/collector/config" "go.opentelemetry.io/collector/config/mapconverter/overwritepropertiesmapconverter" + "go.opentelemetry.io/collector/configmap" "go.opentelemetry.io/collector/internal/testcomponents" "go.opentelemetry.io/collector/internal/testutil" "go.opentelemetry.io/collector/service/featuregate" @@ -109,7 +109,7 @@ func testCollectorStartHelper(t *testing.T) { cfgSet := newDefaultConfigProviderSettings([]string{ filepath.Join("testdata", "otelcol-config.yaml"), }) - cfgSet.MapConverters = append([]config.MapConverterFunc{ + cfgSet.MapConverters = append([]configmap.ConverterFunc{ overwritepropertiesmapconverter.New( []string{"service.telemetry.metrics.address=localhost:" + portAsString(metricsPort)}, )}, diff --git a/service/collector_windows.go b/service/collector_windows.go index 09acce845a7..0d97579b4f8 100644 --- a/service/collector_windows.go +++ b/service/collector_windows.go @@ -29,8 +29,8 @@ import ( "golang.org/x/sys/windows/svc" "golang.org/x/sys/windows/svc/eventlog" - "go.opentelemetry.io/collector/config" "go.opentelemetry.io/collector/config/mapconverter/overwritepropertiesmapconverter" + "go.opentelemetry.io/collector/configmap" "go.opentelemetry.io/collector/service/featuregate" ) @@ -144,7 +144,7 @@ func newWithWindowsEventLogCore(set CollectorSettings, elog *eventlog.Log) (*Col cfgSet := newDefaultConfigProviderSettings(getConfigFlag()) // Append the "overwrite properties converter" as the first converter. cfgSet.MapConverters = append( - []config.MapConverterFunc{overwritepropertiesmapconverter.New(getSetFlag())}, + []configmap.ConverterFunc{overwritepropertiesmapconverter.New(getSetFlag())}, cfgSet.MapConverters...) set.ConfigProvider, err = NewConfigProvider(cfgSet) if err != nil { diff --git a/service/command.go b/service/command.go index 89976c242d7..ae79324d574 100644 --- a/service/command.go +++ b/service/command.go @@ -17,8 +17,8 @@ package service // import "go.opentelemetry.io/collector/service" import ( "github.com/spf13/cobra" - "go.opentelemetry.io/collector/config" "go.opentelemetry.io/collector/config/mapconverter/overwritepropertiesmapconverter" + "go.opentelemetry.io/collector/configmap" "go.opentelemetry.io/collector/service/featuregate" ) @@ -35,7 +35,7 @@ func NewCommand(set CollectorSettings) *cobra.Command { cfgSet := newDefaultConfigProviderSettings(getConfigFlag()) // Append the "overwrite properties converter" as the first converter. cfgSet.MapConverters = append( - []config.MapConverterFunc{overwritepropertiesmapconverter.New(getSetFlag())}, + []configmap.ConverterFunc{overwritepropertiesmapconverter.New(getSetFlag())}, cfgSet.MapConverters...) set.ConfigProvider, err = NewConfigProvider(cfgSet) if err != nil { diff --git a/service/config_provider.go b/service/config_provider.go index cbe74c917e0..80409fca790 100644 --- a/service/config_provider.go +++ b/service/config_provider.go @@ -30,6 +30,7 @@ import ( "go.opentelemetry.io/collector/config/mapprovider/envmapprovider" "go.opentelemetry.io/collector/config/mapprovider/filemapprovider" "go.opentelemetry.io/collector/config/mapprovider/yamlmapprovider" + "go.opentelemetry.io/collector/configmap" "go.opentelemetry.io/collector/internal/configunmarshaler" ) @@ -69,27 +70,27 @@ type ConfigProvider interface { type configProvider struct { locations []string - configMapProviders map[string]config.MapProvider - configMapConverters []config.MapConverterFunc + configMapProviders map[string]configmap.Provider + configMapConverters []configmap.ConverterFunc configUnmarshaler configunmarshaler.ConfigUnmarshaler sync.Mutex - closer config.CloseFunc + closer configmap.CloseFunc watcher chan error } // ConfigProviderSettings are the settings to configure the behavior of the ConfigProvider. type ConfigProviderSettings struct { - // Locations from where the config.Map is retrieved, and merged in the given order. + // Locations from where the configmap.ConfigMap is retrieved, and merged in the given order. // It is required to have at least one location. Locations []string - // MapProviders is a map of pairs . - // It is required to have at least one config.MapProvider. - MapProviders map[string]config.MapProvider + // MapProviders is a map of pairs . + // It is required to have at least one configmap.Provider. + MapProviders map[string]configmap.Provider - // MapConverters is a slice of config.MapConverterFunc. - MapConverters []config.MapConverterFunc + // MapConverters is a slice of configmap.ConverterFunc. + MapConverters []configmap.ConverterFunc // Deprecated: [v0.50.0] because providing custom ConfigUnmarshaler is not necessary since users can wrap/implement // ConfigProvider if needed to change the resulted config. This functionality will be kept for at least 2 minor versions, @@ -101,15 +102,15 @@ func newDefaultConfigProviderSettings(locations []string) ConfigProviderSettings return ConfigProviderSettings{ Locations: locations, MapProviders: makeConfigMapProviderMap(filemapprovider.New(), envmapprovider.New(), yamlmapprovider.New()), - MapConverters: []config.MapConverterFunc{expandmapconverter.New()}, + MapConverters: []configmap.ConverterFunc{expandmapconverter.New()}, Unmarshaler: configunmarshaler.NewDefault(), } } // NewConfigProvider returns a new ConfigProvider that provides the configuration: -// * Retrieve the config.Map by merging all retrieved maps from the given `locations` in order. -// * Then applies all the config.MapConverterFunc in the given order. -// * Then unmarshals the config.Map into the service Config. +// * Retrieve the configmap.ConfigMap by merging all retrieved maps from the given `locations` in order. +// * Then applies all the configmap.ConverterFunc in the given order. +// * Then unmarshals the configmap.ConfigMap into the service Config. func NewConfigProvider(set ConfigProviderSettings) (ConfigProvider, error) { if len(set.Locations) == 0 { return nil, fmt.Errorf("cannot create ConfigProvider: no Locations") @@ -122,11 +123,11 @@ func NewConfigProvider(set ConfigProviderSettings) (ConfigProvider, error) { // Safe copy, ensures the slices and maps cannot be changed from the caller. locationsCopy := make([]string, len(set.Locations)) copy(locationsCopy, set.Locations) - mapProvidersCopy := make(map[string]config.MapProvider, len(set.MapProviders)) + mapProvidersCopy := make(map[string]configmap.Provider, len(set.MapProviders)) for k, v := range set.MapProviders { mapProvidersCopy[k] = v } - mapConvertersCopy := make([]config.MapConverterFunc, len(set.MapConverters)) + mapConvertersCopy := make([]configmap.ConverterFunc, len(set.MapConverters)) copy(mapConvertersCopy, set.MapConverters) return &configProvider{ @@ -154,7 +155,7 @@ func (cm *configProvider) Get(ctx context.Context, factories component.Factories // Apply all converters. for _, cfgMapConv := range cm.configMapConverters { if err = cfgMapConv(ctx, retMap); err != nil { - return nil, fmt.Errorf("cannot convert the config.Map: %w", err) + return nil, fmt.Errorf("cannot convert the configmap.ConfigMap: %w", err) } } @@ -174,7 +175,7 @@ func (cm *configProvider) Watch() <-chan error { return cm.watcher } -func (cm *configProvider) onChange(event *config.ChangeEvent) { +func (cm *configProvider) onChange(event *configmap.ChangeEvent) { // TODO: Remove check for configsource.ErrSessionClosed when providers updated to not call onChange when closed. if event.Error != configsource.ErrSessionClosed { cm.watcher <- event.Error @@ -204,9 +205,9 @@ func (cm *configProvider) Shutdown(ctx context.Context) error { // https://tools.ietf.org/id/draft-kerwin-file-scheme-07.html#syntax var driverLetterRegexp = regexp.MustCompile("^[A-z]:") -func (cm *configProvider) mergeRetrieve(ctx context.Context) (*config.Map, config.CloseFunc, error) { - var closers []config.CloseFunc - retCfgMap := config.NewMap() +func (cm *configProvider) mergeRetrieve(ctx context.Context) (*configmap.ConfigMap, configmap.CloseFunc, error) { + var closers []configmap.CloseFunc + retCfgMap := configmap.New() for _, location := range cm.locations { // For backwards compatibility: // - empty url scheme means "file". @@ -244,8 +245,8 @@ func (cm *configProvider) mergeRetrieve(ctx context.Context) (*config.Map, confi }, nil } -func makeConfigMapProviderMap(providers ...config.MapProvider) map[string]config.MapProvider { - ret := make(map[string]config.MapProvider, len(providers)) +func makeConfigMapProviderMap(providers ...configmap.Provider) map[string]configmap.Provider { + ret := make(map[string]configmap.Provider, len(providers)) for _, provider := range providers { ret[provider.Scheme()] = provider } diff --git a/service/config_provider_test.go b/service/config_provider_test.go index 3bcd4d9e895..42f8615365b 100644 --- a/service/config_provider_test.go +++ b/service/config_provider_test.go @@ -30,31 +30,32 @@ import ( "go.opentelemetry.io/collector/config/configtest" "go.opentelemetry.io/collector/config/experimental/configsource" "go.opentelemetry.io/collector/config/mapprovider/filemapprovider" + "go.opentelemetry.io/collector/configmap" "go.opentelemetry.io/collector/internal/configunmarshaler" ) type mockProvider struct { scheme string - retM *config.Map + retM *configmap.ConfigMap errR error errS error errW error errC error } -func (m *mockProvider) Retrieve(_ context.Context, _ string, watcher config.WatcherFunc) (config.Retrieved, error) { +func (m *mockProvider) Retrieve(_ context.Context, _ string, watcher configmap.WatcherFunc) (configmap.Retrieved, error) { if m.errR != nil { - return config.Retrieved{}, m.errR + return configmap.Retrieved{}, m.errR } if m.retM == nil { - return config.NewRetrievedFromMap(config.NewMap()), nil + return configmap.NewRetrievedFromMap(configmap.New()), nil } if watcher != nil { - watcher(&config.ChangeEvent{Error: m.errW}) + watcher(&configmap.ChangeEvent{Error: m.errW}) } - return config.NewRetrievedFromMap( + return configmap.NewRetrievedFromMap( m.retM, - config.WithRetrievedClose(func(ctx context.Context) error { return m.errC })), nil + configmap.WithRetrievedClose(func(ctx context.Context) error { return m.errC })), nil } func (m *mockProvider) Scheme() string { @@ -72,7 +73,7 @@ type errConfigUnmarshaler struct { err error } -func (ecu *errConfigUnmarshaler) Unmarshal(*config.Map, component.Factories) (*config.Config, error) { +func (ecu *errConfigUnmarshaler) Unmarshal(*configmap.ConfigMap, component.Factories) (*config.Config, error) { return nil, ecu.err } @@ -83,8 +84,8 @@ func TestConfigProvider_Errors(t *testing.T) { tests := []struct { name string locations []string - parserProvider []config.MapProvider - cfgMapConverters []config.MapConverterFunc + parserProvider []configmap.Provider + cfgMapConverters []configmap.ConverterFunc configUnmarshaler configunmarshaler.ConfigUnmarshaler expectNewErr bool expectWatchErr bool @@ -93,14 +94,14 @@ func TestConfigProvider_Errors(t *testing.T) { { name: "retrieve_err", locations: []string{"mock:", "not_supported:"}, - parserProvider: []config.MapProvider{&mockProvider{}}, + parserProvider: []configmap.Provider{&mockProvider{}}, configUnmarshaler: configunmarshaler.NewDefault(), expectNewErr: true, }, { name: "retrieve_err", locations: []string{"mock:", "err:"}, - parserProvider: []config.MapProvider{ + parserProvider: []configmap.Provider{ &mockProvider{}, &mockProvider{scheme: "err", errR: errors.New("retrieve_err")}, }, @@ -110,32 +111,32 @@ func TestConfigProvider_Errors(t *testing.T) { { name: "converter_err", locations: []string{"mock:", filepath.Join("testdata", "otelcol-nop.yaml")}, - parserProvider: []config.MapProvider{&mockProvider{}, filemapprovider.New()}, - cfgMapConverters: []config.MapConverterFunc{func(context.Context, *config.Map) error { return errors.New("converter_err") }}, + parserProvider: []configmap.Provider{&mockProvider{}, filemapprovider.New()}, + cfgMapConverters: []configmap.ConverterFunc{func(context.Context, *configmap.ConfigMap) error { return errors.New("converter_err") }}, configUnmarshaler: configunmarshaler.NewDefault(), expectNewErr: true, }, { name: "unmarshal_err", locations: []string{"mock:", filepath.Join("testdata", "otelcol-nop.yaml")}, - parserProvider: []config.MapProvider{&mockProvider{}, filemapprovider.New()}, + parserProvider: []configmap.Provider{&mockProvider{}, filemapprovider.New()}, configUnmarshaler: &errConfigUnmarshaler{err: errors.New("unmarshal_err")}, expectNewErr: true, }, { name: "validation_err", locations: []string{"mock:", filepath.Join("testdata", "otelcol-invalid.yaml")}, - parserProvider: []config.MapProvider{&mockProvider{}, filemapprovider.New()}, + parserProvider: []configmap.Provider{&mockProvider{}, filemapprovider.New()}, configUnmarshaler: configunmarshaler.NewDefault(), expectNewErr: true, }, { name: "watch_err", locations: []string{"mock:", "err:"}, - parserProvider: func() []config.MapProvider { + parserProvider: func() []configmap.Provider { cfgMap, err := configtest.LoadConfigMap(filepath.Join("testdata", "otelcol-nop.yaml")) require.NoError(t, err) - return []config.MapProvider{&mockProvider{}, &mockProvider{scheme: "err", retM: cfgMap, errW: errors.New("watch_err")}} + return []configmap.Provider{&mockProvider{}, &mockProvider{scheme: "err", retM: cfgMap, errW: errors.New("watch_err")}} }(), configUnmarshaler: configunmarshaler.NewDefault(), expectWatchErr: true, @@ -143,10 +144,10 @@ func TestConfigProvider_Errors(t *testing.T) { { name: "close_err", locations: []string{"mock:", "err:"}, - parserProvider: func() []config.MapProvider { + parserProvider: func() []configmap.Provider { cfgMap, err := configtest.LoadConfigMap(filepath.Join("testdata", "otelcol-nop.yaml")) require.NoError(t, err) - return []config.MapProvider{ + return []configmap.Provider{ &mockProvider{}, &mockProvider{scheme: "err", retM: cfgMap, errC: errors.New("close_err")}, } @@ -194,7 +195,7 @@ func TestConfigProvider_Errors(t *testing.T) { func TestConfigProvider(t *testing.T) { factories, errF := componenttest.NopFactories() require.NoError(t, errF) - mapProvider := func() config.MapProvider { + mapProvider := func() configmap.Provider { cfgMap, err := configtest.LoadConfigMap(filepath.Join("testdata", "otelcol-nop.yaml")) require.NoError(t, err) return &mockProvider{retM: cfgMap} @@ -248,7 +249,7 @@ func TestConfigProviderNoWatcher(t *testing.T) { func TestConfigProvider_ShutdownClosesWatch(t *testing.T) { factories, errF := componenttest.NopFactories() require.NoError(t, errF) - mapProvider := func() config.MapProvider { + mapProvider := func() configmap.Provider { // Use fakeRetrieved with nil errors to have Watchable interface implemented. cfgMap, err := configtest.LoadConfigMap(filepath.Join("testdata", "otelcol-nop.yaml")) require.NoError(t, err) diff --git a/service/internal/builder/exporters_builder.go b/service/internal/builder/exporters_builder.go index 5576ac4009a..9a72f66e3c7 100644 --- a/service/internal/builder/exporters_builder.go +++ b/service/internal/builder/exporters_builder.go @@ -123,7 +123,7 @@ func (exps Exporters) ToMapByDataType() map[config.DataType]map[config.Component return exportersMap } -// Map of config.DataType to the id of the Pipeline that requires the data type. +// ConfigMap of config.DataType to the id of the Pipeline that requires the data type. type dataTypeRequirements map[config.DataType]config.ComponentID // Data type requirements for all exporters.