From b3c745c9294e9c0859159bba7b2206f8d4daa7c7 Mon Sep 17 00:00:00 2001 From: wph95 Date: Tue, 2 Mar 2021 22:15:09 +0800 Subject: [PATCH 1/4] improve syslog input config --- operator/builtin/input/syslog/syslog.go | 43 ++++++++++++++++---- operator/builtin/input/syslog/syslog_test.go | 20 +++++++-- 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/operator/builtin/input/syslog/syslog.go b/operator/builtin/input/syslog/syslog.go index 9b09d5d3..4dac2e89 100644 --- a/operator/builtin/input/syslog/syslog.go +++ b/operator/builtin/input/syslog/syslog.go @@ -18,23 +18,26 @@ func NewSyslogInputConfig(operatorID string) *SyslogInputConfig { } } -type SyslogInputConfig struct { +type BaseSyslogInputConfig struct { helper.InputConfig `yaml:",inline"` - Tcp *tcp.TCPInputConfig `json:"tcp" yaml:"tcp"` - Udp *udp.UDPInputConfig `json:"udp" yaml:"udp"` - Syslog *syslog.SyslogParserConfig `json:"syslog" yaml:"syslog"` + Tcp *tcp.TCPInputConfig `json:"tcp" yaml:"tcp"` + Udp *udp.UDPInputConfig `json:"udp" yaml:"udp"` +} + +type SyslogInputConfig struct { + syslog.SyslogParserConfig `yaml:"-"` + helper.InputConfig `yaml:",inline"` + Tcp *tcp.TCPInputConfig `json:"tcp" yaml:"tcp"` + Udp *udp.UDPInputConfig `json:"udp" yaml:"udp"` } func (c SyslogInputConfig) Build(context operator.BuildContext) ([]operator.Operator, error) { - if c.Syslog == nil { - return nil, fmt.Errorf("need syslog config") - } if c.Tcp == nil && c.Udp == nil { return nil, fmt.Errorf("need tcp config or udp config") } - c.Syslog.OutputIDs = c.OutputIDs - ops, err := c.Syslog.Build(context) + c.SyslogParserConfig.OutputIDs = c.OutputIDs + ops, err := c.SyslogParserConfig.Build(context) if err != nil { return nil, fmt.Errorf("failed to resolve syslog config: %s", err) } @@ -59,3 +62,25 @@ func (c SyslogInputConfig) Build(context operator.BuildContext) ([]operator.Oper return ops, nil } + +func (c *SyslogInputConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { + parserCfg := syslog.NewSyslogParserConfig("syslog_parser") + + err := unmarshal(parserCfg) + if err != nil { + return err + } + c.SyslogParserConfig = *parserCfg + + base := &BaseSyslogInputConfig{ + } + err = unmarshal(base) + if err != nil { + return err + } + + c.InputConfig = base.InputConfig + c.Tcp= base.Tcp + c.Udp = base.Udp + return nil +} diff --git a/operator/builtin/input/syslog/syslog_test.go b/operator/builtin/input/syslog/syslog_test.go index c2a02f44..3a0284af 100644 --- a/operator/builtin/input/syslog/syslog_test.go +++ b/operator/builtin/input/syslog/syslog_test.go @@ -8,6 +8,7 @@ import ( "github.com/open-telemetry/opentelemetry-log-collection/pipeline" "github.com/open-telemetry/opentelemetry-log-collection/testutil" "github.com/stretchr/testify/require" + "gopkg.in/yaml.v2" "net" "testing" "time" @@ -54,7 +55,6 @@ func SyslogInputTest(t *testing.T, cfg *SyslogInputConfig, tc syslog.Case) { require.NoError(t, err) } - switch tc.InputRecord.(type) { case string: _, err = conn.Write([]byte(tc.InputRecord.(string))) @@ -80,18 +80,32 @@ func SyslogInputTest(t *testing.T, cfg *SyslogInputConfig, tc syslog.Case) { func NewSyslogInputConfigWithTcp(syslogCfg *syslog.SyslogParserConfig) *SyslogInputConfig { cfg := NewSyslogInputConfig("test_syslog") + cfg.SyslogParserConfig = *syslogCfg cfg.Tcp = tcp.NewTCPInputConfig("test_syslog_tcp") cfg.Tcp.ListenAddress = ":14201" cfg.OutputIDs = []string{"fake"} - cfg.Syslog = syslogCfg return cfg } func NewSyslogInputConfigWithUdp(syslogCfg *syslog.SyslogParserConfig) *SyslogInputConfig { cfg := NewSyslogInputConfig("test_syslog") + cfg.SyslogParserConfig = *syslogCfg cfg.Udp = udp.NewUDPInputConfig("test_syslog_udp") cfg.Udp.ListenAddress = ":12032" cfg.OutputIDs = []string{"fake"} - cfg.Syslog = syslogCfg return cfg } + +func TestConfigYamlUnmarshal(t *testing.T) { + base := `type: syslog_input +protocol: utc5404 +udp: + listen_address: localhost:1234 +` + var cfg SyslogInputConfig + err := yaml.Unmarshal([]byte(base), &cfg) + require.NoError(t, err) + require.Equal(t, "utc5404", cfg.Protocol) + require.Equal(t, "localhost:1234", cfg.Udp.ListenAddress) + +} From ea0acb6cc1dc3b0559450ec304dccb636e9918fe Mon Sep 17 00:00:00 2001 From: wph95 Date: Tue, 2 Mar 2021 22:21:27 +0800 Subject: [PATCH 2/4] add more test case --- operator/builtin/input/syslog/syslog_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/operator/builtin/input/syslog/syslog_test.go b/operator/builtin/input/syslog/syslog_test.go index 3a0284af..ad996a95 100644 --- a/operator/builtin/input/syslog/syslog_test.go +++ b/operator/builtin/input/syslog/syslog_test.go @@ -108,4 +108,18 @@ udp: require.Equal(t, "utc5404", cfg.Protocol) require.Equal(t, "localhost:1234", cfg.Udp.ListenAddress) + + base = `type: syslog_input +protocol: utc5404 +tcp: + listen_address: localhost:1234 + tls: + enable: true +` + err = yaml.Unmarshal([]byte(base), &cfg) + require.NoError(t, err) + require.Equal(t, "utc5404", cfg.Protocol) + require.Equal(t, "localhost:1234", cfg.Tcp.ListenAddress) + require.Equal(t, true, cfg.Tcp.TLS.Enable) + } From 1ca03a506b8899d74da881ad490ba8296412d715 Mon Sep 17 00:00:00 2001 From: wph95 Date: Tue, 2 Mar 2021 22:42:25 +0800 Subject: [PATCH 3/4] rerun actions From 33d7230bc627d78dabf7cee78da517a58ee52436 Mon Sep 17 00:00:00 2001 From: wph95 Date: Wed, 3 Mar 2021 10:37:44 +0800 Subject: [PATCH 4/4] fix test protocol --- operator/builtin/input/syslog/syslog_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/operator/builtin/input/syslog/syslog_test.go b/operator/builtin/input/syslog/syslog_test.go index ad996a95..eb7dfaca 100644 --- a/operator/builtin/input/syslog/syslog_test.go +++ b/operator/builtin/input/syslog/syslog_test.go @@ -98,19 +98,19 @@ func NewSyslogInputConfigWithUdp(syslogCfg *syslog.SyslogParserConfig) *SyslogIn func TestConfigYamlUnmarshal(t *testing.T) { base := `type: syslog_input -protocol: utc5404 +protocol: rfc5424 udp: listen_address: localhost:1234 ` var cfg SyslogInputConfig err := yaml.Unmarshal([]byte(base), &cfg) require.NoError(t, err) - require.Equal(t, "utc5404", cfg.Protocol) + require.Equal(t, "rfc5424", cfg.Protocol) require.Equal(t, "localhost:1234", cfg.Udp.ListenAddress) base = `type: syslog_input -protocol: utc5404 +protocol: rfc5424 tcp: listen_address: localhost:1234 tls: @@ -118,7 +118,7 @@ tcp: ` err = yaml.Unmarshal([]byte(base), &cfg) require.NoError(t, err) - require.Equal(t, "utc5404", cfg.Protocol) + require.Equal(t, "rfc5424", cfg.Protocol) require.Equal(t, "localhost:1234", cfg.Tcp.ListenAddress) require.Equal(t, true, cfg.Tcp.TLS.Enable)