From 010dc161583fb0e6c365c3f91c788b3999855071 Mon Sep 17 00:00:00 2001 From: Ivan Babrou Date: Wed, 30 Aug 2023 23:00:22 -0700 Subject: [PATCH] Validate expected value size for maps See: https://github.com/cloudflare/ebpf_exporter/issues/253#issuecomment-1694207100 --- exporter/exporter.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/exporter/exporter.go b/exporter/exporter.go index e0d4ff48..f6a03436 100644 --- a/exporter/exporter.go +++ b/exporter/exporter.go @@ -137,6 +137,11 @@ func (e *Exporter) Attach() error { return fmt.Errorf("failed to attach to config %q: %s", cfg.Name, err) } + err = validateMaps(module, cfg) + if err != nil { + return fmt.Errorf("error validating maps for config %q: %v", cfg.Name, err) + } + e.attachedProgs[cfg.Name] = attachments e.modules[cfg.Name] = module } @@ -488,6 +493,36 @@ func (e *Exporter) MapsHandler(w http.ResponseWriter, r *http.Request) { } } +func validateMaps(module *libbpfgo.Module, cfg config.Config) error { + maps := []string{} + + for _, counter := range cfg.Metrics.Counters { + if counter.Name != "" { + maps = append(maps, counter.Name) + } + } + + for _, histogram := range cfg.Metrics.Histograms { + if histogram.Name != "" { + maps = append(maps, histogram.Name) + } + } + + for _, name := range maps { + m, err := module.GetMap(name) + if err != nil { + return fmt.Errorf("failed to get map %q: %v", name, err) + } + + valueSize := m.ValueSize() + if valueSize != 8 { + return fmt.Errorf("value size for map %q is not expected 8 bytes (u64), it is %d bytes", name, valueSize) + } + } + + return nil +} + // aggregateMapValues aggregates values so that the same set of labels is not repeated. // This is useful for cases when underlying id maps to the same value for metrics. // A concrete example is changing cgroup id mapping to the same cgroup name,