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

Remove usage of collector experimental code #2267

Merged
merged 1 commit into from
Nov 16, 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
6 changes: 2 additions & 4 deletions internal/configprovider/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"context"
"fmt"

expcfg "go.opentelemetry.io/collector/config/experimental/config"
"go.opentelemetry.io/collector/config/experimental/configsource"
"go.uber.org/zap"
)

Expand All @@ -31,8 +29,8 @@ type (
)

// Build builds the ConfigSource objects according to the given ConfigSettings.
func Build(ctx context.Context, configSourcesSettings map[string]expcfg.Source, params CreateParams, factories Factories) (map[string]configsource.ConfigSource, error) {
cfgSources := make(map[string]configsource.ConfigSource, len(configSourcesSettings))
func Build(ctx context.Context, configSourcesSettings map[string]Source, params CreateParams, factories Factories) (map[string]ConfigSource, error) {
cfgSources := make(map[string]ConfigSource, len(configSourcesSettings))
for fullName, cfgSrcSettings := range configSourcesSettings {
// If we have the setting we also have the factory.
factory, ok := factories[cfgSrcSettings.ID().Type()]
Expand Down
32 changes: 15 additions & 17 deletions internal/configprovider/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
expcfg "go.opentelemetry.io/collector/config/experimental/config"
"go.opentelemetry.io/collector/config/experimental/configsource"
"go.uber.org/zap"
)

Expand All @@ -39,27 +37,27 @@ func TestConfigSourceBuild(t *testing.T) {
}

tests := []struct {
configSettings map[string]expcfg.Source
configSettings map[string]Source
factories Factories
expectedCfgSources map[string]configsource.ConfigSource
expectedCfgSources map[string]ConfigSource
wantErr error
name string
}{
{
name: "unknown_config_source",
configSettings: map[string]expcfg.Source{
configSettings: map[string]Source{
"tstcfgsrc": &mockCfgSrcSettings{
SourceSettings: expcfg.NewSourceSettings(component.NewIDWithName("unknown_config_source", "tstcfgsrc")),
SourceSettings: NewSourceSettings(component.NewIDWithName("unknown_config_source", "tstcfgsrc")),
},
},
factories: testFactories,
wantErr: &errUnknownType{},
},
{
name: "creation_error",
configSettings: map[string]expcfg.Source{
configSettings: map[string]Source{
"tstcfgsrc": &mockCfgSrcSettings{
SourceSettings: expcfg.NewSourceSettings(component.NewID("tstcfgsrc")),
SourceSettings: NewSourceSettings(component.NewID("tstcfgsrc")),
},
},
factories: Factories{
Expand All @@ -71,9 +69,9 @@ func TestConfigSourceBuild(t *testing.T) {
},
{
name: "factory_return_nil",
configSettings: map[string]expcfg.Source{
configSettings: map[string]Source{
"tstcfgsrc": &mockCfgSrcSettings{
SourceSettings: expcfg.NewSourceSettings(component.NewID("tstcfgsrc")),
SourceSettings: NewSourceSettings(component.NewID("tstcfgsrc")),
},
},
factories: Factories{
Expand All @@ -83,20 +81,20 @@ func TestConfigSourceBuild(t *testing.T) {
},
{
name: "base_case",
configSettings: map[string]expcfg.Source{
configSettings: map[string]Source{
"tstcfgsrc/named": &mockCfgSrcSettings{
SourceSettings: expcfg.NewSourceSettings(component.NewIDWithName("tstcfgsrc", "named")),
SourceSettings: NewSourceSettings(component.NewIDWithName("tstcfgsrc", "named")),
Endpoint: "some_endpoint",
Token: "some_token",
},
},
factories: testFactories,
expectedCfgSources: map[string]configsource.ConfigSource{
expectedCfgSources: map[string]ConfigSource{
"tstcfgsrc/named": &testConfigSource{
ValueMap: map[string]valueEntry{
"tstcfgsrc/named": {
Value: &mockCfgSrcSettings{
SourceSettings: expcfg.NewSourceSettings(component.NewIDWithName("tstcfgsrc", "named")),
SourceSettings: NewSourceSettings(component.NewIDWithName("tstcfgsrc", "named")),
Endpoint: "some_endpoint",
Token: "some_token",
},
Expand Down Expand Up @@ -124,13 +122,13 @@ func (m *mockNilCfgSrcFactory) Type() component.Type {

var _ (Factory) = (*mockNilCfgSrcFactory)(nil)

func (m *mockNilCfgSrcFactory) CreateDefaultConfig() expcfg.Source {
func (m *mockNilCfgSrcFactory) CreateDefaultConfig() Source {
return &mockCfgSrcSettings{
SourceSettings: expcfg.NewSourceSettings(component.NewID("tstcfgsrc")),
SourceSettings: NewSourceSettings(component.NewID("tstcfgsrc")),
Endpoint: "default_endpoint",
}
}

func (m *mockNilCfgSrcFactory) CreateConfigSource(context.Context, CreateParams, expcfg.Source) (configsource.ConfigSource, error) {
func (m *mockNilCfgSrcFactory) CreateConfigSource(context.Context, CreateParams, Source) (ConfigSource, error) {
return nil, nil
}
88 changes: 88 additions & 0 deletions internal/configprovider/component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright Splunk, Inc.
// 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 configprovider

import (
"context"
"errors"

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

// ErrSessionClosed is returned by WatchForUpdate functions when its parent Session
// object is closed.
// This error can be wrapped with additional information. Callers trying to identify this
// specific error must use errors.Is.
var ErrSessionClosed = errors.New("parent session was closed")

// ErrValueUpdated is returned by WatchForUpdate functions when the value being watched
// was changed or expired and needs to be retrieved again.
// This error can be wrapped with additional information. Callers trying to identify this
// specific error must use errors.Is.
var ErrValueUpdated = errors.New("configuration must retrieve the updated value")

// ConfigSource is the interface to be implemented by objects used by the collector
// to retrieve external configuration information.
//
// ConfigSource object will be used to retrieve full configuration or data to be
// injected into a configuration.
//
// The ConfigSource object should use its creation according to the source needs:
// lock resources, open connections, etc. An implementation, for instance,
// can use the creation time to prevent torn configurations, by acquiring a lock
// (or some other mechanism) that prevents concurrent changes to the configuration
// during time that data is being retrieved from the source.
//
// The code managing the ConfigSource instance must guarantee that the object is not used concurrently.
type ConfigSource 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. The retrieved value is selected
// according to the selector and the params passed in the call to Retrieve.
//
// 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 *confmap.Conf) (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.
// This method must be called when the configuration session ends, either in case of success or error.
Close(ctx context.Context) error
}

// Retrieved holds the result of a call to the Retrieve method of a Session object.
type Retrieved interface {
// Value is the retrieved data that will be injected on the configuration.
Value() interface{}
}

// Watchable is an optional interface that Retrieved can implement if the given source
// supports monitoring for updates.
type Watchable interface {
// WatchForUpdate is used to monitor for updates on the retrieved value.
// It must not return until one of the following happens:
//
// 1. An update is detected for the monitored value. In this case the function should
// return ErrValueUpdated or an error wrapping it.
//
// 2. The parent Session object is closed, in which case the method should return
// ErrSessionClosed or an error wrapping it.
//
// 3. An error happens while watching for updates. The method should not return
// on first instances of transient errors, optionally there should be
// configurable thresholds to control for how long such errors can be ignored.
WatchForUpdate() error
}
62 changes: 62 additions & 0 deletions internal/configprovider/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright Splunk, Inc.
// 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 configprovider

import (
"go.opentelemetry.io/collector/component"
)

// SourceSettings defines common settings of a Source configuration.
// Specific config sources can embed this struct and extend it with more fields if needed.
// When embedded it must be with `mapstructure:",squash"` tag.
type SourceSettings struct {
id component.ID `mapstructure:"-"`
}

// ID returns the ID of the component that this configuration belongs to.
func (s *SourceSettings) ID() component.ID {
return s.id
}

// SetIDName updates the name part of the ID for the component that this configuration belongs to.
func (s *SourceSettings) SetIDName(idName string) {
s.id = component.NewIDWithName(s.id.Type(), idName)
}

// NewSourceSettings return a new config.SourceSettings struct with the given ComponentID.
func NewSourceSettings(id component.ID) SourceSettings {
return SourceSettings{id}
}

// Source is the configuration of a config source. Specific config sources must implement this
// interface and will typically embed SourceSettings struct or a struct that extends it.
type Source interface {
// TODO: While config sources are experimental and not in the config package they can't
// reference the private interfaces config.identifiable and config.validatable.
// Defining the required methods of the interfaces temporarily here.

// From config.identifiable:

// ID returns the ID of the component that this configuration belongs to.
ID() component.ID
// SetIDName updates the name part of the ID for the component that this configuration belongs to.
SetIDName(idName string)

// From config.validatable:

// Validate validates the configuration and returns an error if invalid.
Validate() error
}
5 changes: 2 additions & 3 deletions internal/configprovider/config_source_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/experimental/configsource"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/provider/fileprovider"
"go.uber.org/zap"
Expand Down Expand Up @@ -75,7 +74,7 @@ func TestConfigSourceConfigMapProvider(t *testing.T) {
name: "manager_resolve_error",
parserProvider: fileprovider.New(),
configLocation: []string{"file:" + path.Join("testdata", "manager_resolve_error.yaml")},
wantErr: fmt.Errorf("error not wrappedProviders by specific error type: %w", configsource.ErrSessionClosed),
wantErr: fmt.Errorf("error not wrappedProviders by specific error type: %w", ErrSessionClosed),
},
{
name: "multiple_config_success",
Expand Down Expand Up @@ -147,7 +146,7 @@ func TestConfigSourceConfigMapProvider(t *testing.T) {
assert.NoError(t, closeErr)

wg.Wait()
assert.Equal(t, configsource.ErrSessionClosed, watchForUpdatedError)
assert.Equal(t, ErrSessionClosed, watchForUpdatedError)
})
}
}
Expand Down
6 changes: 2 additions & 4 deletions internal/configprovider/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"context"

"go.opentelemetry.io/collector/component"
expcfg "go.opentelemetry.io/collector/config/experimental/config"
"go.opentelemetry.io/collector/config/experimental/configsource"
"go.uber.org/zap"
)

Expand All @@ -45,10 +43,10 @@ type Factory interface {
// The object returned by this method needs to pass the checks implemented by
// 'configcheck.ValidateConfig'. It is recommended to have such check in the
// tests of any implementation of the Factory interface.
CreateDefaultConfig() expcfg.Source
CreateDefaultConfig() Source

// CreateConfigSource creates a configuration source based on the given config.
CreateConfigSource(ctx context.Context, params CreateParams, cfg expcfg.Source) (configsource.ConfigSource, error)
CreateConfigSource(ctx context.Context, params CreateParams, cfg Source) (ConfigSource, error)

// Type gets the type of the component created by this factory.
Type() component.Type
Expand Down
14 changes: 5 additions & 9 deletions internal/configprovider/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,18 @@

package configprovider

import (
"go.opentelemetry.io/collector/config/experimental/configsource"
)

type retrieved struct {
value any
}

// NewRetrieved is a helper that implements the Retrieved interface.
func NewRetrieved(value any) configsource.Retrieved {
func NewRetrieved(value any) Retrieved {
return &retrieved{
value,
}
}

var _ configsource.Retrieved = (*retrieved)(nil)
var _ Retrieved = (*retrieved)(nil)

func (r *retrieved) Value() any {
return r.value
Expand All @@ -42,7 +38,7 @@ type watchableRetrieved struct {
}

// NewWatchableRetrieved is a helper that implements the Watchable interface.
func NewWatchableRetrieved(value any, watchForUpdateFn func() error) configsource.Retrieved {
func NewWatchableRetrieved(value any, watchForUpdateFn func() error) Retrieved {
return &watchableRetrieved{
retrieved: retrieved{
value: value,
Expand All @@ -51,8 +47,8 @@ func NewWatchableRetrieved(value any, watchForUpdateFn func() error) configsourc
}
}

var _ configsource.Watchable = (*watchableRetrieved)(nil)
var _ configsource.Retrieved = (*watchableRetrieved)(nil)
var _ Watchable = (*watchableRetrieved)(nil)
var _ Retrieved = (*watchableRetrieved)(nil)

func (r *watchableRetrieved) Value() any {
return r.retrieved.value
Expand Down
Loading