Skip to content

Commit

Permalink
[receiver/journald] Migrate config unmarshaling from yaml to mapstruc…
Browse files Browse the repository at this point in the history
…ture (open-telemetry#13227)

[receiver/journald] Migrate config from yaml to mapstructure

pkg/stanza historically used yaml for configuration unmarshaling.
The process of migrating to mapstructure was begun long ago but
was never completed. This PR switches journald's underlying input
operator to use mapstructure unmarshaling.
  • Loading branch information
djaglowski authored Aug 16, 2022
1 parent ebdb228 commit 6a1e169
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 29 deletions.
2 changes: 1 addition & 1 deletion receiver/journaldreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza v0.58.0
github.com/stretchr/testify v1.8.0
go.opentelemetry.io/collector v0.58.0
gopkg.in/yaml.v2 v2.4.0
)

require (
Expand Down Expand Up @@ -43,6 +42,7 @@ require (
google.golang.org/grpc v1.48.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

Expand Down
14 changes: 3 additions & 11 deletions receiver/journaldreceiver/journald.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package journaldreceiver // import "github.com/open-telemetry/opentelemetry-coll
import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"gopkg.in/yaml.v2"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/adapter"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
Expand Down Expand Up @@ -53,7 +52,7 @@ func (f ReceiverType) CreateDefaultConfig() config.Receiver {
ReceiverSettings: config.NewReceiverSettings(config.NewComponentID(typeStr)),
Operators: adapter.OperatorConfigs{},
},
Input: adapter.InputConfig{},
Config: *journald.NewConfig(),
}
}

Expand All @@ -65,18 +64,11 @@ func (f ReceiverType) BaseConfig(cfg config.Receiver) adapter.BaseConfig {
// JournaldConfig defines configuration for the journald receiver
type JournaldConfig struct {
adapter.BaseConfig `mapstructure:",squash"`
Input adapter.InputConfig `mapstructure:",remain"`
journald.Config `mapstructure:",squash"`
}

// DecodeInputConfig unmarshals the input operator
func (f ReceiverType) DecodeInputConfig(cfg config.Receiver) (*operator.Config, error) {
logConfig := cfg.(*JournaldConfig)
yamlBytes, _ := yaml.Marshal(logConfig.Input)
inputCfg := journald.NewConfig()

if err := yaml.Unmarshal(yamlBytes, &inputCfg); err != nil {
return nil, err
}

return &operator.Config{Builder: inputCfg}, nil
return &operator.Config{Builder: &logConfig.Config}, nil
}
3 changes: 0 additions & 3 deletions receiver/journaldreceiver/journald_nonlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func NewFactory() component.ReceiverFactory {

type JournaldConfig struct {
adapter.BaseConfig `mapstructure:",squash"`
Input adapter.InputConfig `mapstructure:",remain"`
}

func createDefaultConfig() config.Receiver {
Expand All @@ -52,7 +51,6 @@ func createDefaultConfig() config.Receiver {
ReceiverSettings: config.NewReceiverSettings(config.NewComponentID(typeStr)),
Operators: adapter.OperatorConfigs{},
},
Input: adapter.InputConfig{},
}
}

Expand All @@ -62,6 +60,5 @@ func createLogsReceiver(
cfg config.Receiver,
consumer consumer.Logs,
) (component.LogsReceiver, error) {

return nil, fmt.Errorf("journald is only supported on linux")
}
30 changes: 16 additions & 14 deletions receiver/journaldreceiver/journald_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"go.opentelemetry.io/collector/service/servicetest"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/adapter"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/journald"
)

func TestLoadConfig(t *testing.T) {
Expand All @@ -44,7 +45,7 @@ func TestLoadConfig(t *testing.T) {

assert.Equal(t, len(cfg.Receivers), 1)

assert.Equal(t, testdataConfigYamlAsMap(), cfg.Receivers[config.NewComponentID("journald")])
assert.Equal(t, testdataConfigYaml(), cfg.Receivers[config.NewComponentID("journald")])
}

func TestDecodeInputConfigFailure(t *testing.T) {
Expand All @@ -55,29 +56,30 @@ func TestDecodeInputConfigFailure(t *testing.T) {
ReceiverSettings: config.NewReceiverSettings(config.NewComponentID(typeStr)),
Operators: adapter.OperatorConfigs{},
},
Input: adapter.InputConfig{
"units": map[string]interface{}{},
"priority": "info",
"directory": "/run/log/journal",
},
Config: func() journald.Config {
c := journald.NewConfig()
c.StartAt = "middle"
return *c
}(),
}
receiver, err := factory.CreateLogsReceiver(context.Background(), componenttest.NewNopReceiverCreateSettings(), badCfg, sink)
require.Error(t, err, "receiver creation should fail if input config isn't valid")
require.Nil(t, receiver, "receiver creation should fail if input config isn't valid")
}

func testdataConfigYamlAsMap() *JournaldConfig {
func testdataConfigYaml() *JournaldConfig {
return &JournaldConfig{
BaseConfig: adapter.BaseConfig{
ReceiverSettings: config.NewReceiverSettings(config.NewComponentID(typeStr)),
Operators: adapter.OperatorConfigs{},
},
Input: adapter.InputConfig{
"units": []interface{}{
"ssh",
},
"directory": "/run/log/journal",
"priority": "info",
},
Config: func() journald.Config {
c := journald.NewConfig()
c.Units = []string{"ssh"}
c.Priority = "info"
dir := "/run/log/journal"
c.Directory = &dir
return *c
}(),
}
}

0 comments on commit 6a1e169

Please sign in to comment.