Skip to content

Commit

Permalink
Fix tetragon_process_cache_size metric
Browse files Browse the repository at this point in the history
[ upstream commit: 9e35cba ]

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 Sep 12, 2024
1 parent d390e63 commit 503de69
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 @@ -8,7 +8,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 @@ -46,6 +46,6 @@ func NewCacheCollector() prometheus.Collector {
}

func InitMetrics(registry *prometheus.Registry) {
registry.MustRegister(ProcessCacheTotal)
registry.MustRegister(processCacheTotal)
registry.MustRegister(NewCacheCollector())
}
5 changes: 1 addition & 4 deletions pkg/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,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 @@ -478,7 +477,6 @@ func AddExecEvent(event *tetragonAPI.MsgExecveEventUnix) *ProcessInternal {
}

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

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

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

Expand Down

0 comments on commit 503de69

Please sign in to comment.