Skip to content

Commit

Permalink
Fix tetragon_process_cache_size metric
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
lambdanis committed Aug 23, 2024
1 parent 340c6b0 commit 9e35cba
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
10 changes: 8 additions & 2 deletions pkg/process/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/process/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -47,6 +47,6 @@ func NewCacheCollector() prometheus.Collector {
}

func RegisterMetrics(group metrics.Group) {
group.MustRegister(ProcessCacheTotal)
group.MustRegister(processCacheTotal)
group.MustRegister(NewCacheCollector())
}
5 changes: 1 addition & 4 deletions pkg/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -488,7 +487,6 @@ func AddExecEvent(event *tetragonAPI.MsgExecveEventUnix) *ProcessInternal {
}

procCache.add(proc)
ProcessCacheTotal.Inc()
return proc
}

Expand All @@ -512,7 +510,6 @@ func AddCloneEvent(event *tetragonAPI.MsgCloneEvent) error {

parent.RefInc()
procCache.add(proc)
ProcessCacheTotal.Inc()
return nil
}

Expand Down

0 comments on commit 9e35cba

Please sign in to comment.