Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tetragon_process_cache_size metric #2827

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's another Purge instance in here..

pc.cache.Purge()
but not sure it's needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The flow is: purge() sends to stopChan, then cacheGarbageCollector() reads from this channel and actually purges the inner lru.Cache (the line you pointed). So metric reset happens in a different goroutine than actual cleanup, but it should be fine.

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 @@ -80,9 +80,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 @@ -471,7 +470,6 @@ func AddExecEvent(event *tetragonAPI.MsgExecveEventUnix) *ProcessInternal {
}

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

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

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

Expand Down
Loading