Skip to content

Commit

Permalink
Replace syslog receiver with tcplog receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
dhurley committed Nov 28, 2024
1 parent 6f0edde commit 5faae62
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
22 changes: 19 additions & 3 deletions internal/collector/otel_collector_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,10 +466,14 @@ func (oc *Collector) updateExistingNginxOSSReceiver(
}

func (oc *Collector) updateTcplogReceivers(nginxConfigContext *model.NginxConfigContext) bool {
oc.config.Collector.Receivers.TcplogReceivers = make([]config.TcplogReceiver, 0)

newTcplogReceiverAdded := false
if nginxConfigContext.NAPSysLogServers != nil {
napLoop:
for _, napSysLogServer := range nginxConfigContext.NAPSysLogServers {
if oc.doesTcplogReceiverAlreadyExist(napSysLogServer) {
continue napLoop
}

oc.config.Collector.Receivers.TcplogReceivers = append(
oc.config.Collector.Receivers.TcplogReceivers,
config.TcplogReceiver{
Expand Down Expand Up @@ -512,10 +516,22 @@ func (oc *Collector) updateTcplogReceivers(nginxConfigContext *model.NginxConfig
},
},
)

newTcplogReceiverAdded = true
}
}

return newTcplogReceiverAdded
}

func (oc *Collector) doesTcplogReceiverAlreadyExist(listenAddress string) bool {
for _, tcplogReceiver := range oc.config.Collector.Receivers.TcplogReceivers {
if listenAddress == tcplogReceiver.ListenAddress {
return true
}
}

return len(oc.config.Collector.Receivers.TcplogReceivers) > 0
return false
}

// nolint: revive
Expand Down
40 changes: 38 additions & 2 deletions internal/collector/otel_collector_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ func TestCollector_ProcessNginxConfigUpdateTopic(t *testing.T) {
PlusAPI: "",
},
},
TcplogReceivers: make([]config.TcplogReceiver, 0),
},
},
{
Expand Down Expand Up @@ -222,7 +221,6 @@ func TestCollector_ProcessNginxConfigUpdateTopic(t *testing.T) {
},
},
},
TcplogReceivers: make([]config.TcplogReceiver, 0),
},
},
}
Expand Down Expand Up @@ -652,6 +650,44 @@ func TestCollector_updateResourceAttributes(t *testing.T) {
}
}

func TestCollector_updateTcplogReceivers(t *testing.T) {
conf := types.OTelConfig(t)
conf.Collector.Log.Path = ""
conf.Collector.Processors.Batch = nil
conf.Collector.Processors.Attribute = nil
conf.Collector.Processors.Resource = nil

collector, err := New(conf)
require.NoError(t, err)

nginxConfigContext := &model.NginxConfigContext{
NAPSysLogServers: []string{
"localhost:151",
},
}

assert.Empty(t, conf.Collector.Receivers.TcplogReceivers)

t.Run("Test 1: New TcplogReceiver added", func(tt *testing.T) {
tcplogReceiverAdded := collector.updateTcplogReceivers(nginxConfigContext)

assert.True(tt, tcplogReceiverAdded)
assert.Len(tt, conf.Collector.Receivers.TcplogReceivers, 1)
assert.Equal(tt, "localhost:151", conf.Collector.Receivers.TcplogReceivers[0].ListenAddress)
assert.Len(tt, conf.Collector.Receivers.TcplogReceivers[0].Operators, 5)
})

// Calling updateTcplogReceivers shouldn't update the TcplogReceivers slice
// since there is already a receiver with the same ListenAddress
t.Run("Test 2: TcplogReceiver already exists", func(tt *testing.T) {
tcplogReceiverAdded := collector.updateTcplogReceivers(nginxConfigContext)
assert.False(t, tcplogReceiverAdded)
assert.Len(t, conf.Collector.Receivers.TcplogReceivers, 1)
assert.Equal(t, "localhost:151", conf.Collector.Receivers.TcplogReceivers[0].ListenAddress)
assert.Len(t, conf.Collector.Receivers.TcplogReceivers[0].Operators, 5)
})
}

func createFakeCollector() *typesfakes.FakeCollectorInterface {
fakeCollector := &typesfakes.FakeCollectorInterface{}
fakeCollector.RunStub = func(ctx context.Context) error { return nil }
Expand Down

0 comments on commit 5faae62

Please sign in to comment.