diff --git a/exporter.go b/exporter.go index 107766fb..3d416546 100644 --- a/exporter.go +++ b/exporter.go @@ -188,7 +188,11 @@ func run() int { } enabledCollectorList := utils.ExpandEnabledCollectors(*enabledCollectors) - collectors.Enable(enabledCollectorList) + if err := collectors.Enable(enabledCollectorList); err != nil { + logger.Error(err.Error()) + + return 1 + } // Initialize collectors before loading if err = collectors.Build(logger); err != nil { diff --git a/pkg/collector/collector.go b/pkg/collector/collector.go index 9773570f..e5424abe 100644 --- a/pkg/collector/collector.go +++ b/pkg/collector/collector.go @@ -168,12 +168,20 @@ func (c *MetricCollectors) SetPerfCounterQuery(logger *slog.Logger) error { } // 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 { if !slices.Contains(enabledCollectors, name) { delete(c.Collectors, name) } } + + return nil } // Build To be called by the exporter for collector initialization.