From e03f95d137588d3bffe4b055c73e9f2f2afc6ad5 Mon Sep 17 00:00:00 2001 From: JustWPH Date: Thu, 25 Mar 2021 02:25:13 +0800 Subject: [PATCH] add journal_input support mapstructure (#63) * journalinput config support mapstructure --- operator/builtin/input/journald/journald.go | 8 +++--- .../builtin/input/journald/journald_test.go | 25 +++++++++++++++++-- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/operator/builtin/input/journald/journald.go b/operator/builtin/input/journald/journald.go index 0335d4a99126..810993d450e7 100644 --- a/operator/builtin/input/journald/journald.go +++ b/operator/builtin/input/journald/journald.go @@ -47,11 +47,11 @@ func NewJournaldInputConfig(operatorID string) *JournaldInputConfig { // JournaldInputConfig is the configuration of a journald input operator type JournaldInputConfig struct { - helper.InputConfig `yaml:",inline"` + helper.InputConfig `mapstructure:",squash" yaml:",inline"` - Directory *string `json:"directory,omitempty" yaml:"directory,omitempty"` - Files []string `json:"files,omitempty" yaml:"files,omitempty"` - StartAt string `json:"start_at,omitempty" yaml:"start_at,omitempty"` + Directory *string `mapstructure:"directory,omitempty" json:"directory,omitempty" yaml:"directory,omitempty"` + Files []string `mapstructure:"files,omitempty" json:"files,omitempty" yaml:"files,omitempty"` + StartAt string `mapstructure:"start_at,omitempty" json:"start_at,omitempty" yaml:"start_at,omitempty"` } // Build will build a journald input operator from the supplied configuration diff --git a/operator/builtin/input/journald/journald_test.go b/operator/builtin/input/journald/journald_test.go index 3d9c9971b5c2..87f22e3c6ddc 100644 --- a/operator/builtin/input/journald/journald_test.go +++ b/operator/builtin/input/journald/journald_test.go @@ -24,11 +24,13 @@ import ( "testing" "time" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" + "github.com/open-telemetry/opentelemetry-log-collection/entry" "github.com/open-telemetry/opentelemetry-log-collection/operator" + "github.com/open-telemetry/opentelemetry-log-collection/operator/helper" "github.com/open-telemetry/opentelemetry-log-collection/testutil" - "github.com/stretchr/testify/mock" - "github.com/stretchr/testify/require" ) type fakeJournaldCmd struct{} @@ -113,3 +115,22 @@ func TestInputJournald(t *testing.T) { require.FailNow(t, "Timed out waiting for entry to be read") } } + +func TestJournaldInputConfig(t *testing.T) { + expect := NewJournaldInputConfig("my_journald_input") + expect.WriteTo = entry.NewRecordField("to") + + input := map[string]interface{}{ + "id": "my_journald_input", + "type": "journald_input", + "start_at": "end", + "write_to": "$record.to", + "attributes": map[string]interface{}{}, + "resource": map[string]interface{}{}, + } + + var actual JournaldInputConfig + err := helper.UnmarshalMapstructure(input, &actual) + require.NoError(t, err) + require.Equal(t, expect, &actual) +}