collector: Add disable flag (#2165)

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Jan-Otto Kröpke
2025-08-07 08:58:29 +02:00
committed by GitHub
parent 0e85959a4d
commit 930130f58a
2 changed files with 17 additions and 0 deletions

View File

@@ -89,6 +89,10 @@ func run(ctx context.Context, args []string) int {
"collectors.enabled", "collectors.enabled",
"Comma-separated list of collectors to use. Use '[defaults]' as a placeholder for all the collectors enabled by default."). "Comma-separated list of collectors to use. Use '[defaults]' as a placeholder for all the collectors enabled by default.").
Default(collector.DefaultCollectors).String() 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( timeoutMargin = app.Flag(
"scrape.timeout-margin", "scrape.timeout-margin",
"Seconds to subtract from the timeout allowed by the client. Tune to allow for overhead or high loads.", "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 return 1
} }
if *disabledCollectors != "" {
collectors.Disable(slices.Compact(strings.Split(*disabledCollectors, ",")))
}
// Initialize collectors before loading // Initialize collectors before loading
if err = collectors.Build(ctx, logger); err != nil { if err = collectors.Build(ctx, logger); err != nil {
for _, err := range utils.SplitError(err) { for _, err := range utils.SplitError(err) {

View File

@@ -198,6 +198,15 @@ func (c *Collection) Enable(enabledCollectors []string) error {
return nil 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. // Build To be called by the exporter for collector initialization.
// Instead, fail fast, it will try to build all collectors and return all errors. // Instead, fail fast, it will try to build all collectors and return all errors.
// errors are joined with errors.Join. // errors are joined with errors.Join.