Skip to content

Commit

Permalink
Merge pull request #257 from cloudflare/ivan/validate-map-value-size
Browse files Browse the repository at this point in the history
Validate expected value size for maps
  • Loading branch information
bobrik authored Aug 31, 2023
2 parents 55d45c1 + 010dc16 commit 9c4571a
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 9c4571a

Please sign in to comment.