Skip to content

Commit

Permalink
metrics: Do not return when we cannot find a _stats map
Browse files Browse the repository at this point in the history
In Tetragon, we have maps ending with _stats that contain metadata about
the main maps. An example is that we have execve_map and
execve_map_stats that contains errors and other information related to
execve_map map.

Now when we fail to open the _stats map, we just stop the loop and we
can possibly miss to check for other maps. This patch change that and
tries for the next map.

Additionally we do not try to collect stats for maps that already end up
with _stats as this would result in _stats_stats map names.

Signed-off-by: Anastasios Papagiannis <tasos.papagiannnis@gmail.com>
  • Loading branch information
tpapagian committed Jan 10, 2024
1 parent 93a0c65 commit c2e13ac
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions pkg/observer/observer_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ package observer
import (
"fmt"
"path/filepath"
"strings"

"github.com/cilium/ebpf"
"github.com/cilium/tetragon/pkg/logger"
"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.
Expand All @@ -27,19 +30,33 @@ func (c *bpfCollector) Describe(ch chan<- *prometheus.Desc) {
}

func (c *bpfCollector) Collect(ch chan<- prometheus.Metric) {
statsSuffix := "_stats"
for _, m := range sensors.AllMaps {
name := m.Name
pin := filepath.Join(option.Config.MapDir, name)
pinStats := pin + "_stats"
// Skip map names that end up with _stats.
// This will result in _stats_stats suffixes that we don't care about
if strings.HasSuffix(pin, statsSuffix) {
continue
}
pinStats := pin + statsSuffix

mapLinkStats, err := ebpf.LoadPinnedMap(pinStats, nil)
if err != nil {
return
// If we fail to open the map with _stats suffix
// continue to the next map.
continue
}
defer mapLinkStats.Close()
mapLink, err := ebpf.LoadPinnedMap(pin, nil)
if err != nil {
return
// We have already opened the map with _stats suffix
// so we don't expect that to fail.
logger.GetLogger().WithFields(logrus.Fields{
"MapName": pin,
"StatsMapName": pinStats,
}).Warn("Failed to open the corresponding map for an existing stats map.")
continue
}
defer mapLink.Close()

Expand Down

0 comments on commit c2e13ac

Please # to comment.