Skip to content

Commit

Permalink
Move config.Parser to configparser.Parser, preparation for ParserProv…
Browse files Browse the repository at this point in the history
…ider interface (open-telemetry#3304)

Idea is that we will eventually move the https://github.com/open-telemetry/opentelemetry-collector/blob/main/service/parserprovider/provider.go#L25 in this package, so everything about config parsing is in this package.

Signed-off-by: Bogdan Drutu bogdandrutu@gmail.com
  • Loading branch information
bogdandrutu authored and dashpole committed Jun 14, 2021
1 parent 6c8186b commit 7a3b3e1
Show file tree
Hide file tree
Showing 25 changed files with 76 additions and 66 deletions.
4 changes: 3 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package config
import (
"errors"
"fmt"

"go.opentelemetry.io/collector/config/configparser"
)

var (
Expand Down Expand Up @@ -173,7 +175,7 @@ type CustomUnmarshable interface {
// Unmarshal is a function that un-marshals a Parser into the unmarshable struct in a custom way.
// componentSection *Parser
// The config for this specific component. May be nil or empty if no config available.
Unmarshal(componentSection *Parser) error
Unmarshal(componentSection *configparser.Parser) error
}

// DataType is the data type that is supported for collection. We currently support
Expand Down
22 changes: 11 additions & 11 deletions config/configloader/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configparser"
)

// These are errors that can be returned by Load(). Note that error codes are not part
Expand Down Expand Up @@ -92,8 +93,7 @@ type pipelineSettings struct {

// Load loads a Config from Parser.
// After loading the config, `Validate()` must be called to validate.
func Load(v *config.Parser, factories component.Factories) (*config.Config, error) {

func Load(v *configparser.Parser, factories component.Factories) (*config.Config, error) {
var cfg config.Config

// Load the config.
Expand Down Expand Up @@ -183,7 +183,7 @@ func loadExtensions(exts map[string]interface{}, factories map[config.Type]compo

// Iterate over extensions and create a config for each.
for key, value := range exts {
componentConfig := config.NewParserFromStringMap(cast.ToStringMap(value))
componentConfig := configparser.NewParserFromStringMap(cast.ToStringMap(value))
expandEnvConfig(componentConfig)

// Decode the key into type and fullName components.
Expand Down Expand Up @@ -240,7 +240,7 @@ func loadService(rawService serviceSettings) (config.Service, error) {
}

// LoadReceiver loads a receiver config from componentConfig using the provided factories.
func LoadReceiver(componentConfig *config.Parser, id config.ComponentID, factory component.ReceiverFactory) (config.Receiver, error) {
func LoadReceiver(componentConfig *configparser.Parser, id config.ComponentID, factory component.ReceiverFactory) (config.Receiver, error) {
// Create the default config for this receiver.
receiverCfg := factory.CreateDefaultConfig()
receiverCfg.SetIDName(id.Name())
Expand All @@ -262,7 +262,7 @@ func loadReceivers(recvs map[string]interface{}, factories map[config.Type]compo

// Iterate over input map and create a config for each.
for key, value := range recvs {
componentConfig := config.NewParserFromStringMap(cast.ToStringMap(value))
componentConfig := configparser.NewParserFromStringMap(cast.ToStringMap(value))
expandEnvConfig(componentConfig)

// Decode the key into type and fullName components.
Expand Down Expand Up @@ -299,7 +299,7 @@ func loadExporters(exps map[string]interface{}, factories map[config.Type]compon

// Iterate over Exporters and create a config for each.
for key, value := range exps {
componentConfig := config.NewParserFromStringMap(cast.ToStringMap(value))
componentConfig := configparser.NewParserFromStringMap(cast.ToStringMap(value))
expandEnvConfig(componentConfig)

// Decode the key into type and fullName components.
Expand Down Expand Up @@ -342,7 +342,7 @@ func loadProcessors(procs map[string]interface{}, factories map[config.Type]comp

// Iterate over processors and create a config for each.
for key, value := range procs {
componentConfig := config.NewParserFromStringMap(cast.ToStringMap(value))
componentConfig := configparser.NewParserFromStringMap(cast.ToStringMap(value))
expandEnvConfig(componentConfig)

// Decode the key into type and fullName components.
Expand Down Expand Up @@ -440,7 +440,7 @@ func parseIDNames(pipelineID config.ComponentID, componentType string, names []s

// expandEnvConfig creates a new viper config with expanded values for all the values (simple, list or map value).
// It does not expand the keys.
func expandEnvConfig(v *config.Parser) {
func expandEnvConfig(v *configparser.Parser) {
for _, k := range v.AllKeys() {
v.Set(k, expandStringValues(v.Get(k)))
}
Expand Down Expand Up @@ -537,7 +537,7 @@ type deprecatedUnmarshaler interface {
Unmarshal(componentViperSection *viper.Viper, intoCfg interface{}) error
}

func unmarshal(componentSection *config.Parser, intoCfg interface{}) error {
func unmarshal(componentSection *configparser.Parser, intoCfg interface{}) error {
if cu, ok := intoCfg.(config.CustomUnmarshable); ok {
return cu.Unmarshal(componentSection)
}
Expand All @@ -546,9 +546,9 @@ func unmarshal(componentSection *config.Parser, intoCfg interface{}) error {
}

// unmarshaler returns an unmarshaling function. It should be removed when deprecatedUnmarshaler is removed.
func unmarshaler(factory component.Factory) func(componentViperSection *config.Parser, intoCfg interface{}) error {
func unmarshaler(factory component.Factory) func(componentViperSection *configparser.Parser, intoCfg interface{}) error {
if _, ok := factory.(deprecatedUnmarshaler); ok {
return func(componentParser *config.Parser, intoCfg interface{}) error {
return func(componentParser *configparser.Parser, intoCfg interface{}) error {
return errors.New("deprecated way to specify custom unmarshaler no longer supported")
}
}
Expand Down
3 changes: 2 additions & 1 deletion config/configloader/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/configparser"
"go.opentelemetry.io/collector/internal/testcomponents"
)

Expand Down Expand Up @@ -455,7 +456,7 @@ func TestLoadEmptyAllSections(t *testing.T) {
}

func loadConfigFile(t *testing.T, fileName string, factories component.Factories) (*config.Config, error) {
v, err := config.NewParserFromFile(fileName)
v, err := configparser.NewParserFromFile(fileName)
require.NoError(t, err)

// Load the config from viper using the given factories.
Expand Down
2 changes: 1 addition & 1 deletion config/parser.go → config/configparser/parser.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 config
package configparser

import (
"fmt"
Expand Down
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 config
package configparser

import (
"testing"
Expand Down
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion config/configtest/configtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configloader"
"go.opentelemetry.io/collector/config/configparser"
)

// LoadConfigFile loads a config from file.
func LoadConfigFile(t *testing.T, fileName string, factories component.Factories) (*config.Config, error) {
// Read yaml config from file.
cp, err := config.NewParserFromFile(fileName)
cp, err := configparser.NewParserFromFile(fileName)
require.NoError(t, err)
// Load the config from viper using the given factories.
cfg, err := configloader.Load(cp, factories)
Expand Down
12 changes: 6 additions & 6 deletions config/internal/configsource/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

"gopkg.in/yaml.v2"

"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configparser"
"go.opentelemetry.io/collector/config/experimental/configsource"
"go.opentelemetry.io/collector/consumer/consumererror"
)
Expand Down Expand Up @@ -176,7 +176,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.Parser) (*Manager, error) {
func NewManager(_ *configparser.Parser) (*Manager, error) {
// TODO: Config sources should be extracted for the config itself, need Factories for that.

return &Manager{
Expand All @@ -190,8 +190,8 @@ func NewManager(_ *config.Parser) (*Manager, error) {
// Resolve inspects the given config.Parser and resolves all config sources referenced
// in the configuration, returning a config.Parser fully resolved. This must be called only
// once per lifetime of a Manager object.
func (m *Manager) Resolve(ctx context.Context, parser *config.Parser) (*config.Parser, error) {
res := config.NewParser()
func (m *Manager) Resolve(ctx context.Context, parser *configparser.Parser) (*configparser.Parser, error) {
res := configparser.NewParser()
allKeys := parser.AllKeys()
for _, k := range allKeys {
value, err := m.expandStringValues(ctx, parser.Get(k))
Expand Down Expand Up @@ -490,8 +490,8 @@ func parseCfgSrc(s string) (cfgSrcName, selector string, params interface{}, err
selector = strings.Trim(parts[0], " ")

if len(parts) > 1 && len(parts[1]) > 0 {
var cp *config.Parser
cp, err = config.NewParserFromBuffer(bytes.NewReader([]byte(parts[1])))
var cp *configparser.Parser
cp, err = configparser.NewParserFromBuffer(bytes.NewReader([]byte(parts[1])))
if err != nil {
return
}
Expand Down
22 changes: 11 additions & 11 deletions config/internal/configsource/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configparser"
"go.opentelemetry.io/collector/config/experimental/configsource"
)

Expand Down Expand Up @@ -54,7 +54,7 @@ func TestConfigSourceManager_Simple(t *testing.T) {
},
}

cp := config.NewParserFromStringMap(originalCfg)
cp := configparser.NewParserFromStringMap(originalCfg)

actualParser, err := manager.Resolve(ctx, cp)
require.NoError(t, err)
Expand Down Expand Up @@ -130,7 +130,7 @@ func TestConfigSourceManager_ResolveErrors(t *testing.T) {
require.NoError(t, err)
manager.configSources = tt.configSourceMap

res, err := manager.Resolve(ctx, config.NewParserFromStringMap(tt.config))
res, err := manager.Resolve(ctx, configparser.NewParserFromStringMap(tt.config))
require.Error(t, err)
require.Nil(t, res)
require.NoError(t, manager.Close(ctx))
Expand All @@ -154,11 +154,11 @@ func TestConfigSourceManager_ArraysAndMaps(t *testing.T) {
}

file := path.Join("testdata", "arrays_and_maps.yaml")
cp, err := config.NewParserFromFile(file)
cp, err := configparser.NewParserFromFile(file)
require.NoError(t, err)

expectedFile := path.Join("testdata", "arrays_and_maps_expected.yaml")
expectedParser, err := config.NewParserFromFile(expectedFile)
expectedParser, err := configparser.NewParserFromFile(expectedFile)
require.NoError(t, err)

actualParser, err := manager.Resolve(ctx, cp)
Expand Down Expand Up @@ -206,11 +206,11 @@ func TestConfigSourceManager_ParamsHandling(t *testing.T) {
}

file := path.Join("testdata", "params_handling.yaml")
cp, err := config.NewParserFromFile(file)
cp, err := configparser.NewParserFromFile(file)
require.NoError(t, err)

expectedFile := path.Join("testdata", "params_handling_expected.yaml")
expectedParser, err := config.NewParserFromFile(expectedFile)
expectedParser, err := configparser.NewParserFromFile(expectedFile)
require.NoError(t, err)

actualParser, err := manager.Resolve(ctx, cp)
Expand Down Expand Up @@ -244,7 +244,7 @@ func TestConfigSourceManager_WatchForUpdate(t *testing.T) {
},
}

cp := config.NewParserFromStringMap(originalCfg)
cp := configparser.NewParserFromStringMap(originalCfg)
_, err = manager.Resolve(ctx, cp)
require.NoError(t, err)

Expand Down Expand Up @@ -300,7 +300,7 @@ func TestConfigSourceManager_MultipleWatchForUpdate(t *testing.T) {
},
}

cp := config.NewParserFromStringMap(originalCfg)
cp := configparser.NewParserFromStringMap(originalCfg)
_, err = manager.Resolve(ctx, cp)
require.NoError(t, err)

Expand Down Expand Up @@ -351,11 +351,11 @@ func TestConfigSourceManager_EnvVarHandling(t *testing.T) {
}

file := path.Join("testdata", "envvar_cfgsrc_mix.yaml")
cp, err := config.NewParserFromFile(file)
cp, err := configparser.NewParserFromFile(file)
require.NoError(t, err)

expectedFile := path.Join("testdata", "envvar_cfgsrc_mix_expected.yaml")
expectedParser, err := config.NewParserFromFile(expectedFile)
expectedParser, err := configparser.NewParserFromFile(expectedFile)
require.NoError(t, err)

actualParser, err := manager.Resolve(ctx, cp)
Expand Down
4 changes: 2 additions & 2 deletions internal/processor/filtermetric/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configparser"
"go.opentelemetry.io/collector/internal/processor/filterset"
"go.opentelemetry.io/collector/internal/processor/filterset/regexp"
)
Expand All @@ -48,7 +48,7 @@ func createConfigWithRegexpOptions(filters []string, rCfg *regexp.Config) *Match

func TestConfig(t *testing.T) {
testFile := path.Join(".", "testdata", "config.yaml")
v, err := config.NewParserFromFile(testFile)
v, err := configparser.NewParserFromFile(testFile)
require.NoError(t, err)
testYamls := map[string]MatchProperties{}
require.NoErrorf(t, v.UnmarshalExact(&testYamls), "unable to unmarshal yaml from file %v", testFile)
Expand Down
4 changes: 2 additions & 2 deletions internal/processor/filterset/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configparser"
"go.opentelemetry.io/collector/internal/processor/filterset/regexp"
)

func readTestdataConfigYamls(t *testing.T, filename string) map[string]*Config {
testFile := path.Join(".", "testdata", filename)
v, err := config.NewParserFromFile(testFile)
v, err := configparser.NewParserFromFile(testFile)
require.NoError(t, err)

cfgs := map[string]*Config{}
Expand Down
4 changes: 2 additions & 2 deletions internal/processor/filterset/regexp/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configparser"
)

func TestConfig(t *testing.T) {
testFile := path.Join(".", "testdata", "config.yaml")
v, err := config.NewParserFromFile(testFile)
v, err := configparser.NewParserFromFile(testFile)
require.NoError(t, err)

actualConfigs := map[string]*Config{}
Expand Down
3 changes: 2 additions & 1 deletion internal/testcomponents/example_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configparser"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/exporter/exporterhelper"
Expand All @@ -37,7 +38,7 @@ type ExampleExporter struct {
}

// Unmarshal a viper data into the config struct
func (cfg *ExampleExporter) Unmarshal(componentParser *config.Parser) error {
func (cfg *ExampleExporter) Unmarshal(componentParser *configparser.Parser) error {
return componentParser.UnmarshalExact(cfg)
}

Expand Down
3 changes: 2 additions & 1 deletion receiver/hostmetricsreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/spf13/cast"

"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configparser"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal"
"go.opentelemetry.io/collector/receiver/scraperhelper"
)
Expand Down Expand Up @@ -48,7 +49,7 @@ func (cfg *Config) Validate() error {
}

// Unmarshal a config.Parser into the config struct.
func (cfg *Config) Unmarshal(componentParser *config.Parser) error {
func (cfg *Config) Unmarshal(componentParser *configparser.Parser) error {
if componentParser == nil {
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion receiver/jaegerreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configgrpc"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configparser"
)

const (
Expand Down Expand Up @@ -96,7 +97,7 @@ func (cfg *Config) Validate() error {
}

// Unmarshal a config.Parser into the config struct.
func (cfg *Config) Unmarshal(componentParser *config.Parser) error {
func (cfg *Config) Unmarshal(componentParser *configparser.Parser) error {
if componentParser == nil || len(componentParser.AllKeys()) == 0 {
return fmt.Errorf("empty config for Jaeger receiver")
}
Expand Down
Loading

0 comments on commit 7a3b3e1

Please sign in to comment.