Skip to content

Commit

Permalink
[chore][receiver/journald] Expose journaldreceiver config on all plat…
Browse files Browse the repository at this point in the history
…forms (open-telemetry#29754)

**Description:**
Follow-up to
[comment](open-telemetry#29553 (comment))
in open-telemetry#29553 that `journald` also had different configurations according to
the GOOS specified at build time.

The change moves some types to sources that are built for all OSes while
preserving the behavior of the receiver.

**Link to tracking Issue:**
N/A

**Testing:**
Added factory tests that validate create and shutdown on all platforms.

**Documentation:**
N/A
  • Loading branch information
pjanotti committed Dec 12, 2023
1 parent f51b35f commit 0d4683e
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 81 deletions.
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

0 comments on commit 0d4683e

Please sign in to comment.