Skip to content

Commit

Permalink
mapmetrics,observer: Refactor metrics collector
Browse files Browse the repository at this point in the history
Use new helpers from pkg/metrics to define custom metrics and collector.

Signed-off-by: Anna Kapuscinska <anna@isovalent.com>
  • Loading branch information
lambdanis committed Jul 22, 2024
1 parent a05fbc7 commit 7309c9b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 64 deletions.
30 changes: 16 additions & 14 deletions pkg/metrics/mapmetrics/mapmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
67 changes: 17 additions & 50 deletions pkg/observer/observer_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ 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"
"github.com/prometheus/client_golang/prometheus"
"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
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 7309c9b

Please sign in to comment.