diff --git a/operator/builtin/input/syslog/syslog.go b/operator/builtin/input/syslog/syslog.go index 9b09d5d32a93..4dac2e897f03 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 c2a02f448c56..eb7dfaca5a0a 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,46 @@ 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: rfc5424 +udp: + listen_address: localhost:1234 +` + var cfg SyslogInputConfig + err := yaml.Unmarshal([]byte(base), &cfg) + require.NoError(t, err) + require.Equal(t, "rfc5424", cfg.Protocol) + require.Equal(t, "localhost:1234", cfg.Udp.ListenAddress) + + + base = `type: syslog_input +protocol: rfc5424 +tcp: + listen_address: localhost:1234 + tls: + enable: true +` + err = yaml.Unmarshal([]byte(base), &cfg) + require.NoError(t, err) + require.Equal(t, "rfc5424", cfg.Protocol) + require.Equal(t, "localhost:1234", cfg.Tcp.ListenAddress) + require.Equal(t, true, cfg.Tcp.TLS.Enable) + +}