fix: fail, if unknown collector is defined in enabled list (#1693)

This commit is contained in:
Jan-Otto Kröpke 2024-10-17 01:17:57 +02:00 committed by GitHub
parent 332b0a8a1c
commit 92b7e445e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 2 deletions

View File

@ -188,7 +188,11 @@ func run() int {
} }
enabledCollectorList := utils.ExpandEnabledCollectors(*enabledCollectors) enabledCollectorList := utils.ExpandEnabledCollectors(*enabledCollectors)
collectors.Enable(enabledCollectorList) if err := collectors.Enable(enabledCollectorList); err != nil {
logger.Error(err.Error())
return 1
}
// Initialize collectors before loading // Initialize collectors before loading
if err = collectors.Build(logger); err != nil { if err = collectors.Build(logger); err != nil {

View File

@ -168,12 +168,20 @@ func (c *MetricCollectors) SetPerfCounterQuery(logger *slog.Logger) error {
} }
// Enable removes all collectors that not enabledCollectors. // Enable removes all collectors that not enabledCollectors.
func (c *MetricCollectors) Enable(enabledCollectors []string) { func (c *MetricCollectors) Enable(enabledCollectors []string) error {
for _, name := range enabledCollectors {
if _, ok := c.Collectors[name]; !ok {
return fmt.Errorf("unknown collector %s", name)
}
}
for name := range c.Collectors { for name := range c.Collectors {
if !slices.Contains(enabledCollectors, name) { if !slices.Contains(enabledCollectors, name) {
delete(c.Collectors, name) delete(c.Collectors, name)
} }
} }
return nil
} }
// Build To be called by the exporter for collector initialization. // Build To be called by the exporter for collector initialization.