Skip to content
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
8 changes: 3 additions & 5 deletions exporter/exporterhelper/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ type CreateMetricsExporter func(context.Context, component.ExporterCreateParams,
// CreateMetricsExporter is the equivalent of component.ExporterFactory.CreateLogsExporter()
type CreateLogsExporter func(context.Context, component.ExporterCreateParams, configmodels.Exporter) (component.LogsExporter, error)

// factory is the factory for Jaeger gRPC exporter.
type factory struct {
cfgType configmodels.Type
customUnmarshaler component.CustomUnmarshaler
Expand Down Expand Up @@ -72,14 +71,14 @@ func WithLogs(createLogsExporter CreateLogsExporter) FactoryOption {
}
}

// WithCustomUnmarshaler overrides the default "not available" CustomUnmarshaler.
// WithCustomUnmarshaler implements component.ConfigUnmarshaler.
func WithCustomUnmarshaler(customUnmarshaler component.CustomUnmarshaler) FactoryOption {
return func(o *factory) {
o.customUnmarshaler = customUnmarshaler
}
}

// NewFactory returns a component.ExporterFactory that only supports all types.
// NewFactory returns a component.ExporterFactory.
func NewFactory(
cfgType configmodels.Type,
createDefaultConfig CreateDefaultConfig,
Expand Down Expand Up @@ -150,8 +149,7 @@ type factoryWithUnmarshaler struct {
*factory
}

// CustomUnmarshaler returns a custom unmarshaler for the configuration or nil if
// there is no need for custom unmarshaling.
// Unmarshal un-marshals the config using the provided custom unmarshaler.
func (f *factoryWithUnmarshaler) Unmarshal(componentViperSection *viper.Viper, intoCfg interface{}) error {
return f.customUnmarshaler(componentViperSection, intoCfg)
}
99 changes: 99 additions & 0 deletions extension/extensionhelper/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 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 extensionhelper

import (
"context"

"github.com/spf13/viper"

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

// FactoryOption apply changes to ExporterOptions.
type FactoryOption func(o *factory)

// CreateDefaultConfig is the equivalent of component.ExtensionFactory.CreateDefaultConfig()
type CreateDefaultConfig func() configmodels.Extension

// CreateTraceExtension is the equivalent of component.ExtensionFactory.CreateTraceExtension()
type CreateServiceExtension func(context.Context, component.ExtensionCreateParams, configmodels.Extension) (component.ServiceExtension, error)

type factory struct {
cfgType configmodels.Type
customUnmarshaler component.CustomUnmarshaler
createDefaultConfig CreateDefaultConfig
createServiceExtension CreateServiceExtension
}

// WithCustomUnmarshaler implements component.ConfigUnmarshaler.
func WithCustomUnmarshaler(customUnmarshaler component.CustomUnmarshaler) FactoryOption {
return func(o *factory) {
o.customUnmarshaler = customUnmarshaler
}
}

// NewFactory returns a component.ExtensionFactory.
func NewFactory(
cfgType configmodels.Type,
createDefaultConfig CreateDefaultConfig,
createServiceExtension CreateServiceExtension,
options ...FactoryOption) component.ExtensionFactory {
f := &factory{
cfgType: cfgType,
createDefaultConfig: createDefaultConfig,
createServiceExtension: createServiceExtension,
}
for _, opt := range options {
opt(f)
}
var ret component.ExtensionFactory
if f.customUnmarshaler != nil {
ret = &factoryWithUnmarshaler{f}
} else {
ret = f
}
return ret
}

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

// CreateDefaultConfig creates the default configuration for processor.
func (f *factory) CreateDefaultConfig() configmodels.Extension {
return f.createDefaultConfig()
}

// CreateExtension creates a component.TraceExtension based on this config.
func (f *factory) CreateExtension(
ctx context.Context,
params component.ExtensionCreateParams,
cfg configmodels.Extension) (component.ServiceExtension, error) {
return f.createServiceExtension(ctx, params, cfg)
}

var _ component.ConfigUnmarshaler = (*factoryWithUnmarshaler)(nil)

type factoryWithUnmarshaler struct {
*factory
}

// Unmarshal un-marshals the config using the provided custom unmarshaler.
func (f *factoryWithUnmarshaler) Unmarshal(componentViperSection *viper.Viper, intoCfg interface{}) error {
return f.customUnmarshaler(componentViperSection, intoCfg)
}
91 changes: 91 additions & 0 deletions extension/extensionhelper/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright 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 extensionhelper

import (
"context"
"errors"
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"

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

const typeStr = "test"

var (
defaultCfg = &configmodels.ExtensionSettings{
TypeVal: typeStr,
NameVal: typeStr,
}
nopExtensionInstance = new(nopExtension)
)

func TestNewFactory(t *testing.T) {
factory := NewFactory(
typeStr,
defaultConfig,
createExtension)
assert.EqualValues(t, typeStr, factory.Type())
assert.EqualValues(t, defaultCfg, factory.CreateDefaultConfig())
ext, err := factory.CreateExtension(context.Background(), component.ExtensionCreateParams{}, defaultCfg)
assert.NoError(t, err)
assert.Same(t, nopExtensionInstance, ext)
}

func TestNewFactory_WithConstructors(t *testing.T) {
factory := NewFactory(
typeStr,
defaultConfig,
createExtension,
WithCustomUnmarshaler(customUnmarshaler))
assert.EqualValues(t, typeStr, factory.Type())
assert.EqualValues(t, defaultCfg, factory.CreateDefaultConfig())

fu, ok := factory.(component.ConfigUnmarshaler)
assert.True(t, ok)
assert.Equal(t, errors.New("my error"), fu.Unmarshal(nil, nil))

ext, err := factory.CreateExtension(context.Background(), component.ExtensionCreateParams{}, defaultCfg)
assert.NoError(t, err)
assert.Same(t, nopExtensionInstance, ext)
}

func defaultConfig() configmodels.Extension {
return defaultCfg
}

func createExtension(context.Context, component.ExtensionCreateParams, configmodels.Extension) (component.ServiceExtension, error) {
return nopExtensionInstance, nil
}

func customUnmarshaler(*viper.Viper, interface{}) error {
return errors.New("my error")
}

type nopExtension struct {
}

func (ne *nopExtension) Start(context.Context, component.Host) error {
return nil
}

// Shutdown stops the exporter and is invoked during shutdown.
func (ne *nopExtension) Shutdown(context.Context) error {
return nil
}
2 changes: 1 addition & 1 deletion extension/fluentbitextension/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
21 changes: 9 additions & 12 deletions extension/fluentbitextension/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,23 @@ import (

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

const (
// The value of extension "type" in configuration.
typeStr = "fluentbit"
)

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

// 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 @@ -45,9 +44,7 @@ func (f *Factory) CreateDefaultConfig() configmodels.Extension {
}
}

// CreateExtension creates the extension based on this config.
func (f *Factory) CreateExtension(_ context.Context, params component.ExtensionCreateParams, cfg configmodels.Extension) (component.ServiceExtension, error) {
func createExtension(_ context.Context, params component.ExtensionCreateParams, cfg configmodels.Extension) (component.ServiceExtension, error) {
config := cfg.(*Config)

return newProcessManager(config, params.Logger), nil
}
16 changes: 4 additions & 12 deletions extension/fluentbitextension/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,8 @@ import (
"go.opentelemetry.io/collector/config/configmodels"
)

func TestFactory_Type(t *testing.T) {
factory := Factory{}
require.Equal(t, configmodels.Type(typeStr), factory.Type())
}

func TestFactory_CreateDefaultConfig(t *testing.T) {
factory := Factory{}
cfg := factory.CreateDefaultConfig()
cfg := createDefaultConfig()
assert.Equal(t, &Config{
ExtensionSettings: configmodels.ExtensionSettings{
NameVal: typeStr,
Expand All @@ -44,16 +38,14 @@ func TestFactory_CreateDefaultConfig(t *testing.T) {
cfg)

assert.NoError(t, configcheck.ValidateConfig(cfg))
ext, err := factory.CreateExtension(context.Background(), component.ExtensionCreateParams{Logger: zap.NewNop()}, cfg)
ext, err := createExtension(context.Background(), component.ExtensionCreateParams{Logger: zap.NewNop()}, cfg)
require.NoError(t, err)
require.NotNil(t, ext)
}

func TestFactory_CreateExtension(t *testing.T) {
factory := Factory{}
cfg := factory.CreateDefaultConfig().(*Config)

ext, err := factory.CreateExtension(context.Background(), component.ExtensionCreateParams{Logger: zap.NewNop()}, cfg)
cfg := createDefaultConfig().(*Config)
ext, err := createExtension(context.Background(), component.ExtensionCreateParams{Logger: zap.NewNop()}, cfg)
require.NoError(t, err)
require.NotNil(t, ext)
}
2 changes: 1 addition & 1 deletion extension/healthcheckextension/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
20 changes: 9 additions & 11 deletions extension/healthcheckextension/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,23 @@ import (

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

const (
// The value of extension "type" in configuration.
typeStr = "health_check"
)

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

// 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 @@ -48,8 +47,7 @@ func (f *Factory) CreateDefaultConfig() configmodels.Extension {
}
}

// CreateExtension creates the extension based on this config.
func (f *Factory) CreateExtension(_ context.Context, params component.ExtensionCreateParams, cfg configmodels.Extension) (component.ServiceExtension, error) {
func createExtension(_ context.Context, params component.ExtensionCreateParams, cfg configmodels.Extension) (component.ServiceExtension, error) {
config := cfg.(*Config)

// The runtime settings are global to the application, so while in principle it
Expand Down
Loading