Skip to content

Commit

Permalink
Updates NAP receivers when NAP directives are deleted (#940)
Browse files Browse the repository at this point in the history
  • Loading branch information
RRashmit authored Dec 20, 2024
1 parent d64f32b commit 28adb68
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
45 changes: 44 additions & 1 deletion internal/collector/otel_collector_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,50 @@ func (oc *Collector) updateTcplogReceivers(nginxConfigContext *model.NginxConfig
}
}

return newTcplogReceiverAdded
tcplogReceiverDeleted := oc.areNapReceiversDeleted(nginxConfigContext)

return newTcplogReceiverAdded || tcplogReceiverDeleted
}

func (oc *Collector) areNapReceiversDeleted(nginxConfigContext *model.NginxConfigContext) bool {
listenAddressesToBeDeleted := oc.getConfigDeletedNapReceivers(nginxConfigContext)
if len(listenAddressesToBeDeleted) != 0 {
oc.deleteNapReceivers(listenAddressesToBeDeleted)
return true
}

return false
}

func (oc *Collector) deleteNapReceivers(listenAddressesToBeDeleted map[string]bool) {
filteredReceivers := (oc.config.Collector.Receivers.TcplogReceivers)[:0]
for _, receiver := range oc.config.Collector.Receivers.TcplogReceivers {
if !listenAddressesToBeDeleted[receiver.ListenAddress] {
filteredReceivers = append(filteredReceivers, receiver)
}
}
oc.config.Collector.Receivers.TcplogReceivers = filteredReceivers
}

func (oc *Collector) getConfigDeletedNapReceivers(nginxConfigContext *model.NginxConfigContext) map[string]bool {
elements := make(map[string]bool)

for _, tcplogReceiver := range oc.config.Collector.Receivers.TcplogReceivers {
elements[tcplogReceiver.ListenAddress] = true
}

if nginxConfigContext.NAPSysLogServers != nil {
addressesToDelete := make(map[string]bool)
for _, napAddress := range nginxConfigContext.NAPSysLogServers {
if !elements[napAddress] {
addressesToDelete[napAddress] = true
}
}

return addressesToDelete
}

return elements
}

func (oc *Collector) doesTcplogReceiverAlreadyExist(listenAddress string) bool {
Expand Down
16 changes: 16 additions & 0 deletions internal/collector/otel_collector_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,22 @@ func TestCollector_updateTcplogReceivers(t *testing.T) {
assert.Equal(t, "localhost:151", conf.Collector.Receivers.TcplogReceivers[0].ListenAddress)
assert.Len(t, conf.Collector.Receivers.TcplogReceivers[0].Operators, 4)
})

t.Run("Test 3: TcplogReceiver deleted", func(tt *testing.T) {
tcplogReceiverDeleted := collector.updateTcplogReceivers(&model.NginxConfigContext{})
assert.True(t, tcplogReceiverDeleted)
assert.Empty(t, conf.Collector.Receivers.TcplogReceivers)
})

t.Run("Test 4: New tcplogReceiver added and deleted another", func(tt *testing.T) {
tcplogReceiverDeleted := collector.updateTcplogReceivers(&model.NginxConfigContext{NAPSysLogServers: []string{
"localhost:152",
}})
assert.True(t, tcplogReceiverDeleted)
assert.Len(t, conf.Collector.Receivers.TcplogReceivers, 1)
assert.Equal(t, "localhost:152", conf.Collector.Receivers.TcplogReceivers[0].ListenAddress)
assert.Len(t, conf.Collector.Receivers.TcplogReceivers[0].Operators, 4)
})
}

func createFakeCollector() *typesfakes.FakeCollectorInterface {
Expand Down
4 changes: 3 additions & 1 deletion internal/collector/otelcol.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ service:
{{- end}}
{{- end}}
pipelines:
{{- if or (ne .Receivers.HostMetrics nil) (gt (len .Receivers.OtlpReceivers) 0) (gt (len .Receivers.NginxReceivers) 0) (gt (len .Receivers.NginxPlusReceivers) 0) }}
metrics:
receivers:
{{- if ne .Receivers.HostMetrics nil }}
Expand Down Expand Up @@ -252,7 +253,8 @@ service:
{{- if ne .Exporters.Debug nil }}
- debug
{{- end }}
{{- if ne .Receivers.TcplogReceivers nil }}
{{- end }}
{{- if gt (len .Receivers.TcplogReceivers) 0 }}
logs:
receivers:
{{- range $index, $tcplogReceiver := .Receivers.TcplogReceivers }}
Expand Down
7 changes: 6 additions & 1 deletion internal/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,14 @@ func (c *Config) AreReceiversConfigured() bool {
}

return c.Collector.Receivers.NginxPlusReceivers != nil ||
len(c.Collector.Receivers.NginxPlusReceivers) > 0 ||
c.Collector.Receivers.OtlpReceivers != nil ||
len(c.Collector.Receivers.OtlpReceivers) > 0 ||
c.Collector.Receivers.NginxReceivers != nil ||
c.Collector.Receivers.HostMetrics != nil
len(c.Collector.Receivers.NginxReceivers) > 0 ||
c.Collector.Receivers.HostMetrics != nil ||
c.Collector.Receivers.TcplogReceivers != nil ||
len(c.Collector.Receivers.TcplogReceivers) > 0
}

func isAllowedDir(dir string, allowedDirs []string) bool {
Expand Down

0 comments on commit 28adb68

Please sign in to comment.