Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate expected value size for maps #257

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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