diff --git a/pkg/stanza/operator/input/windows/config.go b/pkg/stanza/operator/input/windows/config.go new file mode 100644 index 000000000000..3fe9e71ee352 --- /dev/null +++ b/pkg/stanza/operator/input/windows/config.go @@ -0,0 +1,38 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package windows // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/windows" + +import ( + "time" + + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper" +) + +const operatorType = "windows_eventlog_input" + +// NewConfig will return an event log config with default values. +func NewConfig() *Config { + return NewConfigWithID(operatorType) +} + +// NewConfig will return an event log config with default values. +func NewConfigWithID(operatorID string) *Config { + return &Config{ + InputConfig: helper.NewInputConfig(operatorID, operatorType), + MaxReads: 100, + StartAt: "end", + PollInterval: 1 * time.Second, + } +} + +// Config is the configuration of a windows event log operator. +type Config struct { + helper.InputConfig `mapstructure:",squash"` + Channel string `mapstructure:"channel"` + MaxReads int `mapstructure:"max_reads,omitempty"` + StartAt string `mapstructure:"start_at,omitempty"` + PollInterval time.Duration `mapstructure:"poll_interval,omitempty"` + Raw bool `mapstructure:"raw,omitempty"` + ExcludeProviders []string `mapstructure:"exclude_providers,omitempty"` +} diff --git a/pkg/stanza/operator/input/windows/operator.go b/pkg/stanza/operator/input/windows/operator.go index c34a0db6086d..8900862eef50 100644 --- a/pkg/stanza/operator/input/windows/operator.go +++ b/pkg/stanza/operator/input/windows/operator.go @@ -12,44 +12,15 @@ import ( "sync" "time" - "go.uber.org/zap" - "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper" + "go.uber.org/zap" ) -const operatorType = "windows_eventlog_input" - func init() { operator.Register(operatorType, func() operator.Builder { return NewConfig() }) } -// NewConfig will return an event log config with default values. -func NewConfig() *Config { - return NewConfigWithID(operatorType) -} - -// NewConfig will return an event log config with default values. -func NewConfigWithID(operatorID string) *Config { - return &Config{ - InputConfig: helper.NewInputConfig(operatorID, operatorType), - MaxReads: 100, - StartAt: "end", - PollInterval: 1 * time.Second, - } -} - -// Config is the configuration of a windows event log operator. -type Config struct { - helper.InputConfig `mapstructure:",squash"` - Channel string `mapstructure:"channel"` - MaxReads int `mapstructure:"max_reads,omitempty"` - StartAt string `mapstructure:"start_at,omitempty"` - PollInterval time.Duration `mapstructure:"poll_interval,omitempty"` - Raw bool `mapstructure:"raw,omitempty"` - ExcludeProviders []string `mapstructure:"exclude_providers,omitempty"` -} - // Build will build a windows event log operator. func (c *Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) { inputOperator, err := c.InputConfig.Build(logger) diff --git a/receiver/windowseventlogreceiver/factory_test.go b/receiver/windowseventlogreceiver/factory_test.go index a39ea31c1e4b..e3218cbce507 100644 --- a/receiver/windowseventlogreceiver/factory_test.go +++ b/receiver/windowseventlogreceiver/factory_test.go @@ -4,9 +4,15 @@ package windowseventlogreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowseventlogreceiver" import ( + "context" + "runtime" "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/consumer/consumertest" + "go.opentelemetry.io/collector/receiver/receivertest" "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowseventlogreceiver/internal/metadata" ) @@ -17,3 +23,31 @@ func TestNewFactory(t *testing.T) { require.EqualValues(t, metadata.Type, factory.Type()) }) } + +func TestCreateDefaultConfig(t *testing.T) { + factory := NewFactory() + cfg := factory.CreateDefaultConfig() + require.NotNil(t, cfg, "failed to create default config") +} + +func TestCreateAndShutdown(t *testing.T) { + factory := NewFactory() + defaultConfig := factory.CreateDefaultConfig() + cfg := defaultConfig.(*WindowsLogConfig) // This cast should work on all platforms. + cfg.InputConfig.Channel = "Application" // Must be explicitly set to a valid channel. + + ctx := context.Background() + settings := receivertest.NewNopCreateSettings() + sink := new(consumertest.LogsSink) + receiver, err := factory.CreateLogsReceiver(ctx, settings, cfg, sink) + + if runtime.GOOS != "windows" { + assert.Error(t, err) + assert.IsType(t, component.ErrDataTypeIsNotSupported, err) + assert.Nil(t, receiver) + } else { + assert.NoError(t, err) + require.NotNil(t, receiver) + require.NoError(t, receiver.Shutdown(ctx)) + } +} diff --git a/receiver/windowseventlogreceiver/receiver.go b/receiver/windowseventlogreceiver/receiver.go new file mode 100644 index 000000000000..40b236d8b55b --- /dev/null +++ b/receiver/windowseventlogreceiver/receiver.go @@ -0,0 +1,30 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package windowseventlogreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowseventlogreceiver" + +import ( + "go.opentelemetry.io/collector/component" + + "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/consumerretry" + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/adapter" + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator" + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/windows" +) + +// createDefaultConfig creates a config with type and version +func createDefaultConfig() component.Config { + return &WindowsLogConfig{ + BaseConfig: adapter.BaseConfig{ + Operators: []operator.Config{}, + RetryOnFailure: consumerretry.NewDefaultConfig(), + }, + InputConfig: *windows.NewConfig(), + } +} + +// WindowsLogConfig defines configuration for the windowseventlog receiver +type WindowsLogConfig struct { + InputConfig windows.Config `mapstructure:",squash"` + adapter.BaseConfig `mapstructure:",squash"` +} diff --git a/receiver/windowseventlogreceiver/receiver_others.go b/receiver/windowseventlogreceiver/receiver_others.go index e5eb1c6d041b..51891aac0802 100644 --- a/receiver/windowseventlogreceiver/receiver_others.go +++ b/receiver/windowseventlogreceiver/receiver_others.go @@ -14,8 +14,6 @@ import ( "go.opentelemetry.io/collector/consumer" "go.opentelemetry.io/collector/receiver" - "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/adapter" - "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator" "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowseventlogreceiver/internal/metadata" ) @@ -27,14 +25,6 @@ func newFactoryAdapter() receiver.Factory { receiver.WithLogs(createLogsReceiver, metadata.LogsStability)) } -func createDefaultConfig() component.Config { - return &WindowsLogConfig{ - BaseConfig: adapter.BaseConfig{ - Operators: []operator.Config{}, - }, - } -} - func createLogsReceiver( _ context.Context, _ receiver.CreateSettings, @@ -43,8 +33,3 @@ func createLogsReceiver( ) (receiver.Logs, error) { return nil, fmt.Errorf("windows eventlog receiver is only supported on Windows") } - -// WindowsLogConfig defines configuration for the windowseventlog receiver -type WindowsLogConfig struct { - adapter.BaseConfig `mapstructure:",squash"` -} diff --git a/receiver/windowseventlogreceiver/receiver_windows.go b/receiver/windowseventlogreceiver/receiver_windows.go index e3e8d5e9742e..0ce0aec9e4ae 100644 --- a/receiver/windowseventlogreceiver/receiver_windows.go +++ b/receiver/windowseventlogreceiver/receiver_windows.go @@ -10,10 +10,8 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/receiver" - "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/consumerretry" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/adapter" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator" - "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/windows" "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowseventlogreceiver/internal/metadata" ) @@ -33,13 +31,7 @@ func (f ReceiverType) Type() component.Type { // CreateDefaultConfig creates a config with type and version func (f ReceiverType) CreateDefaultConfig() component.Config { - return &WindowsLogConfig{ - BaseConfig: adapter.BaseConfig{ - Operators: []operator.Config{}, - RetryOnFailure: consumerretry.NewDefaultConfig(), - }, - InputConfig: *windows.NewConfig(), - } + return createDefaultConfig() } // BaseConfig gets the base config from config, for now @@ -51,9 +43,3 @@ func (f ReceiverType) BaseConfig(cfg component.Config) adapter.BaseConfig { func (f ReceiverType) InputConfig(cfg component.Config) operator.Config { return operator.NewConfig(&cfg.(*WindowsLogConfig).InputConfig) } - -// WindowsLogConfig defines configuration for the windowseventlog receiver -type WindowsLogConfig struct { - InputConfig windows.Config `mapstructure:",squash"` - adapter.BaseConfig `mapstructure:",squash"` -}