Skip to content

Commit

Permalink
Move config.Map to its own package which does not depend on any compo…
Browse files Browse the repository at this point in the history
…nent concept

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu committed Apr 21, 2022
1 parent dd4096b commit bf0717e
Show file tree
Hide file tree
Showing 43 changed files with 284 additions and 234 deletions.
5 changes: 4 additions & 1 deletion config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 5 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package config // import "go.opentelemetry.io/collector/config"
import (
"errors"
"fmt"

"go.opentelemetry.io/collector/configmap"
)

var (
Expand Down Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions config/configtest/configtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion config/configunmarshaler/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
51 changes: 51 additions & 0 deletions config/deprecated.go
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions config/experimental/configsource/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion config/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion config/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions config/internal/configsource/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down Expand Up @@ -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: |
Expand Down
26 changes: 13 additions & 13 deletions config/internal/configsource/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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{
Expand All @@ -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
}

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions config/internal/configsource/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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()}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions config/mapconverter/expandmapconverter/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
Expand Down
4 changes: 2 additions & 2 deletions config/mapconverter/expandmapconverter/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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",
Expand Down
Loading

0 comments on commit bf0717e

Please sign in to comment.