From 9e35cbab946bf8c2d7d001dcf5a3b39b74768e8a Mon Sep 17 00:00:00 2001 From: Anna Kapuscinska Date: Tue, 20 Aug 2024 21:36:20 +0100 Subject: [PATCH] Fix tetragon_process_cache_size metric tetragon_process_cache_size metric was increased on every add() and never decreased. This is incorrect - fix it, so that it's increased on add() only if there was no LRU eviction and it's decreased on remove(). Signed-off-by: Anna Kapuscinska --- pkg/process/cache.go | 10 ++++++++-- pkg/process/metrics.go | 4 ++-- pkg/process/process.go | 5 +---- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/pkg/process/cache.go b/pkg/process/cache.go index 38a16f6c347..2dfd9df7b21 100644 --- a/pkg/process/cache.go +++ b/pkg/process/cache.go @@ -121,8 +121,9 @@ func (pc *Cache) refInc(p *ProcessInternal) { atomic.AddUint32(&p.refcnt, 1) } -func (pc *Cache) Purge() { +func (pc *Cache) purge() { pc.stopChan <- true + processCacheTotal.Set(0) } func NewCache( @@ -159,12 +160,17 @@ func (pc *Cache) get(processID string) (*ProcessInternal, error) { // clone or execve events func (pc *Cache) add(process *ProcessInternal) bool { evicted := pc.cache.Add(process.process.ExecId, process) + if !evicted { + processCacheTotal.Inc() + } return evicted } func (pc *Cache) remove(process *tetragon.Process) bool { present := pc.cache.Remove(process.ExecId) - if !present { + if present { + processCacheTotal.Dec() + } else { errormetrics.ErrorTotalInc(errormetrics.ProcessCacheMissOnRemove) } return present diff --git a/pkg/process/metrics.go b/pkg/process/metrics.go index 0998d191b7d..c10fd545cb6 100644 --- a/pkg/process/metrics.go +++ b/pkg/process/metrics.go @@ -9,7 +9,7 @@ import ( "github.com/prometheus/client_golang/prometheus" ) -var ProcessCacheTotal = prometheus.NewGauge(prometheus.GaugeOpts{ +var processCacheTotal = prometheus.NewGauge(prometheus.GaugeOpts{ Namespace: consts.MetricsNamespace, Name: "process_cache_size", Help: "The size of the process cache", @@ -47,6 +47,6 @@ func NewCacheCollector() prometheus.Collector { } func RegisterMetrics(group metrics.Group) { - group.MustRegister(ProcessCacheTotal) + group.MustRegister(processCacheTotal) group.MustRegister(NewCacheCollector()) } diff --git a/pkg/process/process.go b/pkg/process/process.go index d11fdbe774a..d2a4d469c82 100644 --- a/pkg/process/process.go +++ b/pkg/process/process.go @@ -82,9 +82,8 @@ func InitCache(w watcher.K8sResourceWatcher, size int) error { } func FreeCache() { - procCache.Purge() + procCache.purge() procCache = nil - ProcessCacheTotal.Set(0) } // GetProcessCopy() duplicates tetragon.Process and returns it @@ -488,7 +487,6 @@ func AddExecEvent(event *tetragonAPI.MsgExecveEventUnix) *ProcessInternal { } procCache.add(proc) - ProcessCacheTotal.Inc() return proc } @@ -512,7 +510,6 @@ func AddCloneEvent(event *tetragonAPI.MsgCloneEvent) error { parent.RefInc() procCache.add(proc) - ProcessCacheTotal.Inc() return nil }