Skip to content

Commit

Permalink
fix: fail, if unknown collector is defined in enabled list (#1693)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkroepke authored Oct 16, 2024
1 parent 332b0a8 commit 92b7e44
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 5 additions & 1 deletion exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 9 additions & 1 deletion pkg/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 92b7e44

Please sign in to comment.