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

[chore][receiver/journald] Expose journaldreceiver config on all platforms #29754

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
41 changes: 41 additions & 0 deletions pkg/stanza/operator/input/journald/config.go
Original file line number Diff line number Diff line change
@@ -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
32 changes: 0 additions & 32 deletions pkg/stanza/operator/input/journald/journald.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions pkg/stanza/operator/input/journald/journald_nonlinux.go
Original file line number Diff line number Diff line change
@@ -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")
}
50 changes: 50 additions & 0 deletions receiver/journaldreceiver/config.go
Original file line number Diff line number Diff line change
@@ -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)
}
34 changes: 34 additions & 0 deletions receiver/journaldreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
}
}
36 changes: 1 addition & 35 deletions receiver/journaldreceiver/journald.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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()
}
14 changes: 0 additions & 14 deletions receiver/journaldreceiver/journald_nonlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
Expand Down