diff --git a/pkg/network/tracer/process_cache.go b/pkg/network/tracer/process_cache.go index c02c86b9fe562..a07cd2d6d3412 100644 --- a/pkg/network/tracer/process_cache.go +++ b/pkg/network/tracer/process_cache.go @@ -97,8 +97,6 @@ func newProcessCache(maxProcs int, filteredEnvs []string) (*processCache, error) var err error pc.cache, err = lru.NewWithEvict(maxProcs, func(_ processCacheKey, p *process) { - processCacheTelemetry.cacheEvicts.Inc() - pl, _ := pc.cacheByPid[p.Pid] if pl = pl.remove(p); len(pl) == 0 { delete(pc.cacheByPid, p.Pid) @@ -227,7 +225,9 @@ func (pc *processCache) add(p *process) { } p.Expiry = time.Now().Add(defaultExpiry).Unix() - pc.cache.Add(processCacheKey{pid: p.Pid, startTime: p.StartTime}, p) + if evicted := pc.cache.Add(processCacheKey{pid: p.Pid, startTime: p.StartTime}, p); evicted { + processCacheTelemetry.cacheEvicts.Inc() + } pl, _ := pc.cacheByPid[p.Pid] pc.cacheByPid[p.Pid] = pl.update(p) }