Skip to content

Commit

Permalink
Change hostobserver extension to use extensionhelper (#710)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Aug 19, 2020
1 parent ff9afeb commit f7360ee
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cmd/otelcontribcol/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func components() (component.Factories, error) {

extensions := []component.ExtensionFactory{
k8sobserver.NewFactory(),
&hostobserver.Factory{},
hostobserver.NewFactory(),
}

for _, ext := range factories.Extensions {
Expand Down
2 changes: 1 addition & 1 deletion extension/observer/hostobserver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestLoadConfig(t *testing.T) {
factories, err := componenttest.ExampleComponents()
assert.NoError(t, err)

factory := &Factory{}
factory := NewFactory()
factories.Extensions[typeStr] = factory
cfg, err := configtest.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)

Expand Down
24 changes: 10 additions & 14 deletions extension/observer/hostobserver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/extension/extensionhelper"
)

const (
Expand All @@ -29,19 +30,15 @@ const (
defaultCollectionInterval = 10
)

// Factory is the factory for the extension.
type Factory struct {
// NewFactory creates a factory for HostObserver extension.
func NewFactory() component.ExtensionFactory {
return extensionhelper.NewFactory(
typeStr,
createDefaultConfig,
createExtension)
}

var _ component.Factory = (*Factory)(nil)

// Type gets the type of the config created by this factory.
func (f *Factory) Type() configmodels.Type {
return typeStr
}

// CreateDefaultConfig creates the default configuration for the extension.
func (f *Factory) CreateDefaultConfig() configmodels.Extension {
func createDefaultConfig() configmodels.Extension {
return &Config{
ExtensionSettings: configmodels.ExtensionSettings{
TypeVal: typeStr,
Expand All @@ -51,9 +48,8 @@ func (f *Factory) CreateDefaultConfig() configmodels.Extension {
}
}

// CreateExtension creates the extension based on this config.
func (f *Factory) CreateExtension(
ctx context.Context,
func createExtension(
_ context.Context,
params component.ExtensionCreateParams,
cfg configmodels.Extension,
) (component.ServiceExtension, error) {
Expand Down
12 changes: 2 additions & 10 deletions extension/observer/hostobserver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,13 @@ import (
"go.opentelemetry.io/collector/config/configcheck"
)

func TestType(t *testing.T) {
factory := &Factory{}
typ := factory.Type()
require.EqualValues(t, "host_observer", typ)
}

func TestValidConfig(t *testing.T) {
factory := &Factory{}
err := configcheck.ValidateConfig(factory.CreateDefaultConfig())
err := configcheck.ValidateConfig(createDefaultConfig())
require.NoError(t, err)
}

func TestCreateMetricsReceiver(t *testing.T) {
factory := &Factory{}
hostObserver, err := factory.CreateExtension(
hostObserver, err := createExtension(
context.Background(),
component.ExtensionCreateParams{},
&Config{},
Expand Down

0 comments on commit f7360ee

Please sign in to comment.