From 930130f58a4e0694df33fe975f89652b5345e20c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Thu, 7 Aug 2025 08:58:29 +0200 Subject: [PATCH] collector: Add disable flag (#2165) Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- cmd/windows_exporter/main.go | 8 ++++++++ pkg/collector/collection.go | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/cmd/windows_exporter/main.go b/cmd/windows_exporter/main.go index ddc91c77..081a27b1 100644 --- a/cmd/windows_exporter/main.go +++ b/cmd/windows_exporter/main.go @@ -89,6 +89,10 @@ func run(ctx context.Context, args []string) int { "collectors.enabled", "Comma-separated list of collectors to use. Use '[defaults]' as a placeholder for all the collectors enabled by default."). Default(collector.DefaultCollectors).String() + disabledCollectors = app.Flag( + "collectors.disabled", + "Comma-separated list of collectors to exclude. Can be used to disable collector from the defaults."). + Default("").String() timeoutMargin = app.Flag( "scrape.timeout-margin", "Seconds to subtract from the timeout allowed by the client. Tune to allow for overhead or high loads.", @@ -166,6 +170,10 @@ func run(ctx context.Context, args []string) int { return 1 } + if *disabledCollectors != "" { + collectors.Disable(slices.Compact(strings.Split(*disabledCollectors, ","))) + } + // Initialize collectors before loading if err = collectors.Build(ctx, logger); err != nil { for _, err := range utils.SplitError(err) { diff --git a/pkg/collector/collection.go b/pkg/collector/collection.go index 29df38b0..17839c41 100644 --- a/pkg/collector/collection.go +++ b/pkg/collector/collection.go @@ -198,6 +198,15 @@ func (c *Collection) Enable(enabledCollectors []string) error { return nil } +// Disable removes all collectors that are listed in disabledCollectors. +func (c *Collection) Disable(disabledCollectors []string) { + for name := range c.collectors { + if slices.Contains(disabledCollectors, name) { + delete(c.collectors, name) + } + } +} + // Build To be called by the exporter for collector initialization. // Instead, fail fast, it will try to build all collectors and return all errors. // errors are joined with errors.Join.