diff --git a/operator/builtin/input/syslog/syslog.go b/operator/builtin/input/syslog/syslog.go index 7c668938c561..438563d3de23 100644 --- a/operator/builtin/input/syslog/syslog.go +++ b/operator/builtin/input/syslog/syslog.go @@ -50,20 +50,28 @@ func (c SyslogInputConfig) Build(context operator.BuildContext) ([]operator.Oper if c.Tcp == nil && c.Udp == nil { return nil, fmt.Errorf("need tcp config or udp config") } + parentID := c.InputConfig.ID() + if parentID == "" { + parentID = c.InputConfig.Type() + } + subContext := context.WithSubNamespace(parentID) + if c.Tcp == nil && c.Udp == nil { + return nil, fmt.Errorf("need tcp config or udp config") + } c.SyslogParserConfig.OutputIDs = c.OutputIDs - ops, err := c.SyslogParserConfig.Build(context) + ops, err := c.SyslogParserConfig.Build(subContext) if err != nil { return nil, fmt.Errorf("failed to resolve syslog config: %s", err) } if c.Tcp != nil { c.Tcp.OutputIDs = []string{ops[0].ID()} - inputOps, err := c.Tcp.Build(context) + inputOps, err := c.Tcp.Build(subContext) if err != nil { return nil, fmt.Errorf("failed to resolve tcp config: %s", err) } - ops = append(ops, inputOps...) + ops = append(inputOps, ops...) } if c.Udp != nil { @@ -72,7 +80,7 @@ func (c SyslogInputConfig) Build(context operator.BuildContext) ([]operator.Oper if err != nil { return nil, fmt.Errorf("failed to resolve upd config: %s", err) } - ops = append(ops, inputOps...) + ops = append(inputOps, ops...) } return ops, nil diff --git a/operator/builtin/input/syslog/syslog_test.go b/operator/builtin/input/syslog/syslog_test.go index 9d91b2d17ced..0c2406bedbd8 100644 --- a/operator/builtin/input/syslog/syslog_test.go +++ b/operator/builtin/input/syslog/syslog_test.go @@ -93,12 +93,63 @@ func SyslogInputTest(t *testing.T, cfg *SyslogInputConfig, tc syslog.Case) { } } +func TestSyslogIDs(t *testing.T) { + basicConfig := func() *syslog.SyslogParserConfig { + cfg := syslog.NewSyslogParserConfig("test_syslog_parser") + return cfg + } + + cases := []struct { + Name string + Cfg *syslog.SyslogParserConfig + ExpectedOpIDs []string + UDPorTCP string + }{ + { + Name: "default", + Cfg: func() *syslog.SyslogParserConfig { + sysCfg := basicConfig() + sysCfg.Protocol = "RFC3164" + return sysCfg + }(), + UDPorTCP: "UDP", + ExpectedOpIDs: []string{ + "$.test_syslog.test_syslog_parser", + "$.fake", + }, + }, + } + + for _, tc := range cases { + t.Run(fmt.Sprintf("TCP-%s", tc.Name), func(t *testing.T) { + cfg := NewSyslogInputConfigWithTcp(tc.Cfg) + bc := testutil.NewBuildContext(t) + ops, err := cfg.Build(bc) + require.NoError(t, err) + for i, op := range ops { + out := op.GetOutputIDs() + require.Equal(t, tc.ExpectedOpIDs[i], out[0]) + } + }) + t.Run(fmt.Sprintf("UDP-%s", tc.Name), func(t *testing.T) { + cfg := NewSyslogInputConfigWithUdp(tc.Cfg) + bc := testutil.NewBuildContext(t) + ops, err := cfg.Build(bc) + require.NoError(t, err) + for i, op := range ops { + out := op.GetOutputIDs() + require.Equal(t, tc.ExpectedOpIDs[i], out[0]) + } + }) + } +} + 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.OutputIDs = []string{"$.fake"} return cfg } @@ -107,7 +158,7 @@ func NewSyslogInputConfigWithUdp(syslogCfg *syslog.SyslogParserConfig) *SyslogIn cfg.SyslogParserConfig = *syslogCfg cfg.Udp = udp.NewUDPInputConfig("test_syslog_udp") cfg.Udp.ListenAddress = ":12032" - cfg.OutputIDs = []string{"fake"} + cfg.OutputIDs = []string{"$.fake"} return cfg }