From 92b7e445e1fc5c59b2e98dc9f242446d1a0362ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Thu, 17 Oct 2024 01:17:57 +0200 Subject: [PATCH] fix: fail, if unknown collector is defined in enabled list (#1693) --- exporter.go | 6 +++++- pkg/collector/collector.go | 10 +++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) 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.