From 7309c9ba22a2b07bae182e5ea8b10aa214be13fd Mon Sep 17 00:00:00 2001 From: Anna Kapuscinska Date: Sun, 23 Jun 2024 19:32:26 +0100 Subject: [PATCH] mapmetrics,observer: Refactor metrics collector Use new helpers from pkg/metrics to define custom metrics and collector. Signed-off-by: Anna Kapuscinska --- pkg/metrics/mapmetrics/mapmetrics.go | 30 +++++++------ pkg/observer/observer_stats.go | 67 +++++++--------------------- 2 files changed, 33 insertions(+), 64 deletions(-) diff --git a/pkg/metrics/mapmetrics/mapmetrics.go b/pkg/metrics/mapmetrics/mapmetrics.go index c4361d97162..410a91ef982 100644 --- a/pkg/metrics/mapmetrics/mapmetrics.go +++ b/pkg/metrics/mapmetrics/mapmetrics.go @@ -6,27 +6,29 @@ package mapmetrics import ( "github.com/cilium/tetragon/pkg/metrics" "github.com/cilium/tetragon/pkg/metrics/consts" - "github.com/prometheus/client_golang/prometheus" ) +var MapLabel = metrics.ConstrainedLabel{ + Name: "map", + // These are maps which usage we monitor, not maps from which we read + // metrics. Metrics are read from separate maps suffixed with "_stats". + Values: []string{"execve_map", "tg_execve_joined_info_map"}, +} + var ( - MapSize = metrics.NewBPFGauge(prometheus.NewDesc( - prometheus.BuildFQName(consts.MetricsNamespace, "", "map_entries"), + MapSize = metrics.MustNewCustomGauge(metrics.NewOpts( + consts.MetricsNamespace, "", "map_entries", "The total number of in-use entries per map.", - []string{"map"}, nil, + nil, []metrics.ConstrainedLabel{MapLabel}, nil, )) - MapCapacity = metrics.NewBPFGauge(prometheus.NewDesc( - prometheus.BuildFQName(consts.MetricsNamespace, "", "map_capacity"), + MapCapacity = metrics.MustNewCustomGauge(metrics.NewOpts( + consts.MetricsNamespace, "", "map_capacity", "Capacity of a BPF map. Expected to be constant.", - []string{"map"}, nil, + nil, []metrics.ConstrainedLabel{MapLabel}, nil, )) - MapErrors = metrics.NewBPFCounter(prometheus.NewDesc( - prometheus.BuildFQName(consts.MetricsNamespace, "", "map_errors_total"), + MapErrors = metrics.MustNewCustomGauge(metrics.NewOpts( + consts.MetricsNamespace, "", "map_errors_total", "The number of errors per map.", - []string{"map"}, nil, + nil, []metrics.ConstrainedLabel{MapLabel}, nil, )) ) - -func InitMetrics(_ *prometheus.Registry) { - // custom collectors are registered independently -} diff --git a/pkg/observer/observer_stats.go b/pkg/observer/observer_stats.go index 3b4e7494d77..8849dd0d3ed 100644 --- a/pkg/observer/observer_stats.go +++ b/pkg/observer/observer_stats.go @@ -9,6 +9,7 @@ import ( "github.com/cilium/ebpf" "github.com/cilium/tetragon/pkg/logger" + "github.com/cilium/tetragon/pkg/metrics" "github.com/cilium/tetragon/pkg/metrics/mapmetrics" "github.com/cilium/tetragon/pkg/option" "github.com/cilium/tetragon/pkg/sensors" @@ -16,20 +17,19 @@ import ( "github.com/sirupsen/logrus" ) -// 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() - ch <- mapmetrics.MapCapacity.Desc() - ch <- mapmetrics.MapErrors.Desc() +func NewBPFCollector() metrics.CollectorWithInit { + return metrics.NewCustomCollector( + metrics.CustomMetrics{ + mapmetrics.MapSize, + mapmetrics.MapCapacity, + mapmetrics.MapErrors, + }, + collect, + collectForDocs, + ) } -func (c *bpfCollector) Collect(ch chan<- prometheus.Metric) { +func collect(ch chan<- prometheus.Metric) { statsSuffix := "_stats" // Depending on the sensors that are loaded and the dependencies between them, // sensors.AllMaps may have the same map name multiple times. This can cause in @@ -113,43 +113,10 @@ func updateMapErrors(ch chan<- prometheus.Metric, mapLinkStats *ebpf.Map, name s ) } -// bpfZeroCollector implements prometheus.Collector. It collects "zero" metrics. -// It's intended to be used when BPF metrics are not collected, but we still want -// Prometheus metrics to be exposed. -type bpfZeroCollector struct { - bpfCollector -} - -func NewBPFZeroCollector() prometheus.Collector { - return &bpfZeroCollector{ - bpfCollector: bpfCollector{}, - } -} - -func (c *bpfZeroCollector) Describe(ch chan<- *prometheus.Desc) { - c.bpfCollector.Describe(ch) -} - -func (c *bpfZeroCollector) Collect(ch chan<- prometheus.Metric) { - // This list should contain all monitored maps. - // These are not maps from which metrics are read in the "real" collector - - // the metrics are stored in separate maps suffixed with "_stats". - monitoredMaps := []string{ - "execve_map", - "tg_execve_joined_info_map", - } - for _, m := range monitoredMaps { - ch <- mapmetrics.MapSize.MustMetric( - 0, - m, - ) - ch <- mapmetrics.MapCapacity.MustMetric( - 0, - m, - ) - ch <- mapmetrics.MapErrors.MustMetric( - 0, - m, - ) +func collectForDocs(ch chan<- prometheus.Metric) { + for _, m := range mapmetrics.MapLabel.Values { + ch <- mapmetrics.MapSize.MustMetric(0, m) + ch <- mapmetrics.MapCapacity.MustMetric(0, m) + ch <- mapmetrics.MapErrors.MustMetric(0, m) } }