From 51688bb847b840618a763d305cb05c6b241b4fdf Mon Sep 17 00:00:00 2001 From: sadath-12 Date: Mon, 19 Feb 2024 13:05:36 +0530 Subject: [PATCH] fix: processLru migrated towards ProcessCacheTotal as separate metric processLru migrated towards ProcessCacheTotal as separate metric Signed-off-by: sadath-12 --- .../eventcachemetrics/eventcachemetrics.go | 8 ++--- pkg/metrics/metricsconfig/initmetrics.go | 2 -- pkg/process/metrics.go | 29 ++++++------------- pkg/process/process.go | 3 ++ 4 files changed, 16 insertions(+), 26 deletions(-) diff --git a/pkg/metrics/eventcachemetrics/eventcachemetrics.go b/pkg/metrics/eventcachemetrics/eventcachemetrics.go index 669fd61ba81..67400cb3a44 100644 --- a/pkg/metrics/eventcachemetrics/eventcachemetrics.go +++ b/pkg/metrics/eventcachemetrics/eventcachemetrics.go @@ -61,22 +61,22 @@ func InitMetrics(registry *prometheus.Registry) { registry.MustRegister(parentInfoErrors) } -// Get a new handle on an processInfoErrors metric for an eventType +// Get a new handle on a processInfoErrors metric for an eventType func ProcessInfoError(eventType string) prometheus.Counter { return processInfoErrors.WithLabelValues(eventType) } -// Get a new handle on an processInfoErrors metric for an eventType +// Get a new handle on a podInfoErrors metric for an eventType func PodInfoError(eventType string) prometheus.Counter { return podInfoErrors.WithLabelValues(eventType) } -// Get a new handle on an processInfoErrors metric for an eventType +// Get a new handle on an eventCacheErrorsTotal metric for an error func EventCacheError(err string) prometheus.Counter { return eventCacheErrorsTotal.WithLabelValues(err) } -// Get a new handle on the eventCacheRetriesTotal metric for an entryType +// Get a new handle on an eventCacheRetriesTotal metric for an entryType func EventCacheRetries(entryType string) prometheus.Counter { return eventCacheRetriesTotal.WithLabelValues(entryType) } diff --git a/pkg/metrics/metricsconfig/initmetrics.go b/pkg/metrics/metricsconfig/initmetrics.go index b66cb8560bc..000d19da0d6 100644 --- a/pkg/metrics/metricsconfig/initmetrics.go +++ b/pkg/metrics/metricsconfig/initmetrics.go @@ -19,7 +19,6 @@ import ( "github.com/cilium/tetragon/pkg/metrics/syscallmetrics" "github.com/cilium/tetragon/pkg/metrics/watchermetrics" "github.com/cilium/tetragon/pkg/observer" - "github.com/cilium/tetragon/pkg/process" "github.com/cilium/tetragon/pkg/version" grpcmetrics "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus" "github.com/prometheus/client_golang/prometheus" @@ -46,7 +45,6 @@ func InitAllMetrics(registry *prometheus.Registry) { // register BPF collectors registry.MustRegister(mapmetrics.NewBPFCollector( observer.NewBPFCollector(), - process.NewBPFCollector(), )) registry.MustRegister(eventmetrics.NewBPFCollector()) diff --git a/pkg/process/metrics.go b/pkg/process/metrics.go index 5d37cb34704..9dc24d4df42 100644 --- a/pkg/process/metrics.go +++ b/pkg/process/metrics.go @@ -4,28 +4,17 @@ package process import ( - "fmt" - - "github.com/cilium/tetragon/pkg/metrics/mapmetrics" + "github.com/cilium/tetragon/pkg/metrics/consts" "github.com/prometheus/client_golang/prometheus" ) -// bpfCollector implements prometheus.Collector. It collects metrics directly from BPF maps. -type bpfCollector struct{} - -func NewBPFCollector() prometheus.Collector { - return &bpfCollector{} -} - -func (c *bpfCollector) Describe(ch chan<- *prometheus.Desc) { - ch <- mapmetrics.MapSize.Desc() -} +var ProcessCacheTotal = prometheus.NewGauge(prometheus.GaugeOpts{ + Namespace: consts.MetricsNamespace, + Name: "process_cache_size", + Help: "The size of the process cache", + ConstLabels: nil, +}) -func (c *bpfCollector) Collect(ch chan<- prometheus.Metric) { - if procCache != nil { - ch <- mapmetrics.MapSize.MustMetric( - float64(procCache.len()), - "processLru", fmt.Sprint(procCache.size), - ) - } +func InitMetrics(registry *prometheus.Registry) { + registry.MustRegister(ProcessCacheTotal) } diff --git a/pkg/process/process.go b/pkg/process/process.go index 57e60f44b49..0bd50ce5670 100644 --- a/pkg/process/process.go +++ b/pkg/process/process.go @@ -88,6 +88,7 @@ func InitCache(w watcher.K8sResourceWatcher, size int) error { func FreeCache() { procCache.Purge() procCache = nil + ProcessCacheTotal.Set(0) } // GetProcessCopy() duplicates tetragon.Process and returns it @@ -462,6 +463,7 @@ func AddExecEvent(event *tetragonAPI.MsgExecveEventUnix) *ProcessInternal { } procCache.add(proc) + ProcessCacheTotal.Inc() return proc } @@ -485,6 +487,7 @@ func AddCloneEvent(event *tetragonAPI.MsgCloneEvent) error { parent.RefInc() procCache.add(proc) + ProcessCacheTotal.Inc() return nil }