Skip to content

Commit

Permalink
syslog_input config embed syslog_parser inline (#43)
Browse files Browse the repository at this point in the history
* flatten syslog input config
  • Loading branch information
wph95 authored Mar 3, 2021
1 parent 2b90914 commit fb7443a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
43 changes: 34 additions & 9 deletions operator/builtin/input/syslog/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
}
34 changes: 31 additions & 3 deletions operator/builtin/input/syslog/syslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)))
Expand All @@ -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)

}

0 comments on commit fb7443a

Please sign in to comment.