Skip to content

Commit

Permalink
fix filter mechanism in the access log module (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrproliu authored Oct 9, 2024
1 parent f800504 commit d39db69
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
16 changes: 9 additions & 7 deletions pkg/accesslog/common/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,8 @@ func (c *ConnectionManager) isLocalTarget(pair *ip.SocketPair) addressProcessTyp

func (c *ConnectionManager) AddNewProcess(pid int32, entities []api.ProcessInterface) {
// filtering the namespace
if c.shouldExcludeTheProcess(entities) {
monitorProcesses := c.shouldMonitorProcesses(entities)
if len(monitorProcesses) == 0 {
c.RemoveProcess(pid, entities)
return
}
Expand All @@ -491,9 +492,9 @@ func (c *ConnectionManager) AddNewProcess(pid int32, entities []api.ProcessInter
defer c.monitoringProcessLock.Unlock()

// adding monitoring process and IP addresses
c.monitoringProcesses[pid] = entities
c.monitoringProcesses[pid] = monitorProcesses
c.updateMonitorStatusForProcess(pid, true)
for _, entity := range entities {
for _, entity := range monitorProcesses {
for _, host := range entity.ExposeHosts() {
c.localIPWithPid[host] = pid
}
Expand Down Expand Up @@ -529,8 +530,8 @@ func (c *ConnectionManager) printTotalAddressesWithPid(prefix string) {
log.Debugf("----------------------------")
}

func (c *ConnectionManager) shouldExcludeTheProcess(entities []api.ProcessInterface) bool {
return c.monitorFilter.ShouldExclude(entities)
func (c *ConnectionManager) shouldMonitorProcesses(entities []api.ProcessInterface) []api.ProcessInterface {
return c.monitorFilter.ShouldIncludeProcesses(entities)
}

func (c *ConnectionManager) RemoveProcess(pid int32, entities []api.ProcessInterface) {
Expand All @@ -549,10 +550,11 @@ func (c *ConnectionManager) RemoveProcess(pid int32, entities []api.ProcessInter
func (c *ConnectionManager) RecheckAllProcesses(processes map[int32][]api.ProcessInterface) {
shouldMonitoringProcesses := make(map[int32][]api.ProcessInterface)
for pid, p := range processes {
if c.shouldExcludeTheProcess(p) {
monitorProcesses := c.shouldMonitorProcesses(p)
if len(monitorProcesses) == 0 {
continue
}
shouldMonitoringProcesses[pid] = p
shouldMonitoringProcesses[pid] = monitorProcesses
}
// checking the monitoring process
c.monitoringProcesses = shouldMonitoringProcesses
Expand Down
26 changes: 12 additions & 14 deletions pkg/accesslog/common/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
)

type MonitorFilter interface {
// ShouldExclude returns true if the process should be excluded from monitoring.
ShouldExclude(process []api.ProcessInterface) bool
// ShouldIncludeProcesses returns true if the processes should be included in monitoring.
ShouldIncludeProcesses(process []api.ProcessInterface) []api.ProcessInterface
// ExcludeNamespaces returns a list of namespaces that should be excluded from monitoring.
ExcludeNamespaces() []string
}
Expand All @@ -45,26 +45,24 @@ func NewStaticMonitorFilter(namespaces, clusters []string) *StaticMonitorFilter
}
}

func (s *StaticMonitorFilter) ShouldExclude(processes []api.ProcessInterface) bool {
containsNotExcludeCluster := false
func (s *StaticMonitorFilter) ShouldIncludeProcesses(processes []api.ProcessInterface) (res []api.ProcessInterface) {
for _, entity := range processes {
if entity.DetectType() != api.Kubernetes { // for now, we only have the kubernetes detected processes
continue
}
namespace := entity.DetectProcess().(*kubernetes.Process).PodContainer().Pod.Namespace
if s.namespaces[namespace] {
return true
nameExclude := s.namespaces[namespace]
clusterExclude := false
if cluster, _, found := strings.Cut(entity.Entity().ServiceName, "::"); found && s.clusters[cluster] {
clusterExclude = true
}
if cluster, _, found := strings.Cut(entity.Entity().ServiceName, "::"); found {
if !s.clusters[cluster] {
containsNotExcludeCluster = true
}
} else {
containsNotExcludeCluster = true
break

// if the namespace and cluster are not excluded, include the process
if !nameExclude && !clusterExclude {
res = append(res, entity)
}
}
return !containsNotExcludeCluster
return
}

func (s *StaticMonitorFilter) ExcludeNamespaces() []string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ trees:
{{- contains .elements }}
- id: "{{ notEmpty .id }}"
parentid: "{{ notEmpty .parentid }}"
symbol: "fprintf"
symbol: "vfprintf"
stacktype: USER_SPACE
dumpcount: {{ gt .dumpcount 0 }}
{{- end }}
Expand Down

0 comments on commit d39db69

Please sign in to comment.