diff --git a/pkg/stanza/operator/input/journald/config.go b/pkg/stanza/operator/input/journald/config.go new file mode 100644 index 000000000000..7f2afec5bcac --- /dev/null +++ b/pkg/stanza/operator/input/journald/config.go @@ -0,0 +1,41 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package journald // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/journald" + +import ( + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper" +) + +const operatorType = "journald_input" + +// NewConfig creates a new input config with default values +func NewConfig() *Config { + return NewConfigWithID(operatorType) +} + +// NewConfigWithID creates a new input config with default values +func NewConfigWithID(operatorID string) *Config { + return &Config{ + InputConfig: helper.NewInputConfig(operatorID, operatorType), + StartAt: "end", + Priority: "info", + } +} + +// Config is the configuration of a journald input operator +type Config struct { + helper.InputConfig `mapstructure:",squash"` + + Directory *string `mapstructure:"directory,omitempty"` + Files []string `mapstructure:"files,omitempty"` + StartAt string `mapstructure:"start_at,omitempty"` + Units []string `mapstructure:"units,omitempty"` + Priority string `mapstructure:"priority,omitempty"` + Matches []MatchConfig `mapstructure:"matches,omitempty"` + Identifiers []string `mapstructure:"identifiers,omitempty"` + Grep string `mapstructure:"grep,omitempty"` + Dmesg bool `mapstructure:"dmesg,omitempty"` +} + +type MatchConfig map[string]string diff --git a/pkg/stanza/operator/input/journald/journald.go b/pkg/stanza/operator/input/journald/journald.go index 1a2315af9dc4..7a9fe0433ff4 100644 --- a/pkg/stanza/operator/input/journald/journald.go +++ b/pkg/stanza/operator/input/journald/journald.go @@ -28,44 +28,12 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper" ) -const operatorType = "journald_input" const waitDuration = 1 * time.Second func init() { operator.Register(operatorType, func() operator.Builder { return NewConfig() }) } -// NewConfig creates a new input config with default values -func NewConfig() *Config { - return NewConfigWithID(operatorType) -} - -// NewConfigWithID creates a new input config with default values -func NewConfigWithID(operatorID string) *Config { - return &Config{ - InputConfig: helper.NewInputConfig(operatorID, operatorType), - StartAt: "end", - Priority: "info", - } -} - -// Config is the configuration of a journald input operator -type Config struct { - helper.InputConfig `mapstructure:",squash"` - - Directory *string `mapstructure:"directory,omitempty"` - Files []string `mapstructure:"files,omitempty"` - StartAt string `mapstructure:"start_at,omitempty"` - Units []string `mapstructure:"units,omitempty"` - Priority string `mapstructure:"priority,omitempty"` - Matches []MatchConfig `mapstructure:"matches,omitempty"` - Identifiers []string `mapstructure:"identifiers,omitempty"` - Grep string `mapstructure:"grep,omitempty"` - Dmesg bool `mapstructure:"dmesg,omitempty"` -} - -type MatchConfig map[string]string - // Build will build a journald input operator from the supplied configuration func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) { inputOperator, err := c.InputConfig.Build(logger) diff --git a/pkg/stanza/operator/input/journald/journald_nonlinux.go b/pkg/stanza/operator/input/journald/journald_nonlinux.go new file mode 100644 index 000000000000..072e5aba69f4 --- /dev/null +++ b/pkg/stanza/operator/input/journald/journald_nonlinux.go @@ -0,0 +1,19 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +//go:build !linux +// +build !linux + +package journald // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/journald" + +import ( + "errors" + + "go.uber.org/zap" + + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator" +) + +func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) { + return nil, errors.New("journald input operator is only supported on linux") +} diff --git a/receiver/journaldreceiver/config.go b/receiver/journaldreceiver/config.go new file mode 100644 index 000000000000..dec38676fe4e --- /dev/null +++ b/receiver/journaldreceiver/config.go @@ -0,0 +1,50 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package journaldreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/journaldreceiver" + +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/journald" + "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/journaldreceiver/internal/metadata" +) + +// createDefaultConfig creates a config with type and version +func createDefaultConfig() component.Config { + return &JournaldConfig{ + BaseConfig: adapter.BaseConfig{ + Operators: []operator.Config{}, + RetryOnFailure: consumerretry.NewDefaultConfig(), + }, + InputConfig: *journald.NewConfig(), + } +} + +// ReceiverType implements adapter.LogReceiverType +// to create a journald receiver +type ReceiverType struct{} + +// Type is the receiver type +func (f ReceiverType) Type() component.Type { + return metadata.Type +} + +// BaseConfig gets the base config from config, for now +func (f ReceiverType) BaseConfig(cfg component.Config) adapter.BaseConfig { + return cfg.(*JournaldConfig).BaseConfig +} + +// JournaldConfig defines configuration for the journald receiver +type JournaldConfig struct { + adapter.BaseConfig `mapstructure:",squash"` + InputConfig journald.Config `mapstructure:",squash"` +} + +// InputConfig unmarshals the input operator +func (f ReceiverType) InputConfig(cfg component.Config) operator.Config { + return operator.NewConfig(&cfg.(*JournaldConfig).InputConfig) +} diff --git a/receiver/journaldreceiver/factory_test.go b/receiver/journaldreceiver/factory_test.go index 8781423b721c..292276e6d4d6 100644 --- a/receiver/journaldreceiver/factory_test.go +++ b/receiver/journaldreceiver/factory_test.go @@ -4,9 +4,15 @@ package journaldreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/journaldreceiver" 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/journaldreceiver/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.(*JournaldConfig) // This cast should work on all platforms. + cfg.InputConfig.Dmesg = true // Setting this property just to confirm availability on all platforms. + + ctx := context.Background() + settings := receivertest.NewNopCreateSettings() + sink := new(consumertest.LogsSink) + receiver, err := factory.CreateLogsReceiver(ctx, settings, cfg, sink) + + if runtime.GOOS == "linux" { + assert.NoError(t, err) + require.NotNil(t, receiver) + assert.NoError(t, receiver.Shutdown(ctx)) + } else { + assert.Error(t, err) + assert.IsType(t, component.ErrDataTypeIsNotSupported, err) + assert.Nil(t, receiver) + } +} diff --git a/receiver/journaldreceiver/journald.go b/receiver/journaldreceiver/journald.go index ad92712de219..c42b02128cbd 100644 --- a/receiver/journaldreceiver/journald.go +++ b/receiver/journaldreceiver/journald.go @@ -10,10 +10,7 @@ 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/journald" "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/journaldreceiver/internal/metadata" ) @@ -22,38 +19,7 @@ func newFactoryAdapter() receiver.Factory { return adapter.NewFactory(ReceiverType{}, metadata.LogsStability) } -// ReceiverType implements adapter.LogReceiverType -// to create a journald receiver -type ReceiverType struct{} - -// Type is the receiver type -func (f ReceiverType) Type() component.Type { - return metadata.Type -} - // CreateDefaultConfig creates a config with type and version func (f ReceiverType) CreateDefaultConfig() component.Config { - return &JournaldConfig{ - BaseConfig: adapter.BaseConfig{ - Operators: []operator.Config{}, - RetryOnFailure: consumerretry.NewDefaultConfig(), - }, - InputConfig: *journald.NewConfig(), - } -} - -// BaseConfig gets the base config from config, for now -func (f ReceiverType) BaseConfig(cfg component.Config) adapter.BaseConfig { - return cfg.(*JournaldConfig).BaseConfig -} - -// JournaldConfig defines configuration for the journald receiver -type JournaldConfig struct { - adapter.BaseConfig `mapstructure:",squash"` - InputConfig journald.Config `mapstructure:",squash"` -} - -// InputConfig unmarshals the input operator -func (f ReceiverType) InputConfig(cfg component.Config) operator.Config { - return operator.NewConfig(&cfg.(*JournaldConfig).InputConfig) + return createDefaultConfig() } diff --git a/receiver/journaldreceiver/journald_nonlinux.go b/receiver/journaldreceiver/journald_nonlinux.go index dd2f9cad904c..c946978b72be 100644 --- a/receiver/journaldreceiver/journald_nonlinux.go +++ b/receiver/journaldreceiver/journald_nonlinux.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/journaldreceiver/internal/metadata" ) @@ -27,18 +25,6 @@ func newFactoryAdapter() receiver.Factory { receiver.WithLogs(createLogsReceiver, metadata.LogsStability)) } -type JournaldConfig struct { - adapter.BaseConfig `mapstructure:",squash"` -} - -func createDefaultConfig() component.Config { - return &JournaldConfig{ - BaseConfig: adapter.BaseConfig{ - Operators: []operator.Config{}, - }, - } -} - func createLogsReceiver( _ context.Context, _ receiver.CreateSettings,