Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate configunmarshaler into generic implementation #6801

Merged
merged 2 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .chloggen/generic-configunmarshaler.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: configunmarshaler

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Consolidate package into generic implementation

# One or more tracking issues or pull requests related to the change
issues: [6801]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
8 changes: 4 additions & 4 deletions otelcol/configprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ func (cm *configProvider) Get(ctx context.Context, factories Factories) (*Config
}

return &Config{
Receivers: cfg.Receivers.GetReceivers(),
Processors: cfg.Processors.GetProcessors(),
Exporters: cfg.Exporters.GetExporters(),
Extensions: cfg.Extensions.GetExtensions(),
Receivers: cfg.Receivers.Configs(),
Processors: cfg.Processors.Configs(),
Exporters: cfg.Exporters.Configs(),
Extensions: cfg.Extensions.Configs(),
Service: cfg.Service,
}, nil
}
Expand Down
76 changes: 76 additions & 0 deletions otelcol/internal/configunmarshaler/configs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package configunmarshaler // import "go.opentelemetry.io/collector/otelcol/internal/configunmarshaler"

import (
"fmt"
"reflect"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap"
)

type Configs[F component.Factory] struct {
cfgs map[component.ID]component.Config

factories map[component.Type]F
}

func NewConfigs[F component.Factory](factories map[component.Type]F) *Configs[F] {
return &Configs[F]{factories: factories}
}

func (c *Configs[F]) Unmarshal(conf *confmap.Conf) error {
rawCfgs := make(map[component.ID]map[string]interface{})
if err := conf.Unmarshal(&rawCfgs, confmap.WithErrorUnused()); err != nil {
return err
}

// Prepare resulting map.
c.cfgs = make(map[component.ID]component.Config)
// Iterate over raw configs and create a config for each.
for id, value := range rawCfgs {
// Find factory based on component kind and type that we read from config source.
factory, ok := c.factories[id.Type()]
if !ok {
return errorUnknownType(id, reflect.ValueOf(c.factories).MapKeys())
}

// Create the default config for this component.
cfg := factory.CreateDefaultConfig()

// 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 := component.UnmarshalConfig(confmap.NewFromStringMap(value), cfg); err != nil {
return errorUnmarshalError(id, err)
}

c.cfgs[id] = cfg
}

return nil
}

func (c *Configs[F]) Configs() map[component.ID]component.Config {
return c.cfgs
}

func errorUnknownType(id component.ID, factories []reflect.Value) error {
return fmt.Errorf("unknown type: %q for id: %q (valid values: %v)", id.Type(), id, factories)
}

func errorUnmarshalError(id component.ID, err error) error {
return fmt.Errorf("error reading configuration for %q: %w", id, err)
}
147 changes: 147 additions & 0 deletions otelcol/internal/configunmarshaler/configs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package configunmarshaler

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/exporter/exportertest"
"go.opentelemetry.io/collector/extension/extensiontest"
"go.opentelemetry.io/collector/processor/processortest"
"go.opentelemetry.io/collector/receiver/receivertest"
)

var testKinds = []struct {
kind string
factories map[component.Type]component.Factory
}{
{
kind: "receiver",
factories: map[component.Type]component.Factory{
"nop": receivertest.NewNopFactory(),
},
},
{
kind: "processor",
factories: map[component.Type]component.Factory{
"nop": processortest.NewNopFactory(),
},
},
{
kind: "exporter",
factories: map[component.Type]component.Factory{
"nop": exportertest.NewNopFactory(),
},
},
{
kind: "extension",
factories: map[component.Type]component.Factory{
"nop": extensiontest.NewNopFactory(),
},
},
}

func TestUnmarshal(t *testing.T) {
for _, tk := range testKinds {
t.Run(tk.kind, func(t *testing.T) {
cfgs := NewConfigs(tk.factories)
conf := confmap.NewFromStringMap(map[string]interface{}{
"nop": nil,
"nop/my" + tk.kind: nil,
})
require.NoError(t, cfgs.Unmarshal(conf))

assert.Equal(t, map[component.ID]component.Config{
component.NewID("nop"): tk.factories["nop"].CreateDefaultConfig(),
component.NewIDWithName("nop", "my"+tk.kind): tk.factories["nop"].CreateDefaultConfig(),
}, cfgs.Configs())
})
}
}

func TestUnmarshalError(t *testing.T) {
for _, tk := range testKinds {
t.Run(tk.kind, func(t *testing.T) {
var testCases = []struct {
name string
conf *confmap.Conf
// string that the error must contain
expectedError string
}{
{
name: "invalid-type",
conf: confmap.NewFromStringMap(map[string]interface{}{
"nop": nil,
"/custom": nil,
}),
expectedError: "the part before / should not be empty",
},
{
name: "invalid-name-after-slash",
conf: confmap.NewFromStringMap(map[string]interface{}{
"nop": nil,
"nop/": nil,
}),
expectedError: "the part after / should not be empty",
},
{
name: "unknown-type",
conf: confmap.NewFromStringMap(map[string]interface{}{
"nosuch" + tk.kind: nil,
}),
expectedError: "unknown type: \"nosuch" + tk.kind + "\"",
},
{
name: "duplicate",
conf: confmap.NewFromStringMap(map[string]interface{}{
"nop /my" + tk.kind + " ": nil,
" nop/ my" + tk.kind: nil,
}),
expectedError: "duplicate name",
},
{
name: "invalid-section",
conf: confmap.NewFromStringMap(map[string]interface{}{
"nop": map[string]interface{}{
"unknown_section": tk.kind,
},
}),
expectedError: "error reading configuration for \"nop\"",
},
{
name: "invalid-sub-config",
conf: confmap.NewFromStringMap(map[string]interface{}{
"nop": "tests",
}),
expectedError: "'[nop]' expected a map, got 'string'",
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
cfgs := NewConfigs(tk.factories)
err := cfgs.Unmarshal(tt.conf)
require.Error(t, err)
assert.Contains(t, err.Error(), tt.expectedError)
})
}
})
}
}
30 changes: 0 additions & 30 deletions otelcol/internal/configunmarshaler/error.go

This file was deleted.

72 changes: 0 additions & 72 deletions otelcol/internal/configunmarshaler/exporters.go

This file was deleted.

Loading