From d4879a5e8709e02b78f0eb481fcfe4978a720f5c Mon Sep 17 00:00:00 2001 From: matt durham Date: Tue, 3 Oct 2023 09:53:36 -0400 Subject: [PATCH] Rearrange kingpin --- collector/collector.go | 18 ++++++++++++++---- node_exporter.go | 2 +- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/collector/collector.go b/collector/collector.go index 9f756588b4..b142a55fa9 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -53,7 +53,8 @@ var ( factories = make(map[string]func(config *NodeCollectorConfig, logger log.Logger) (Collector, error)) initiatedCollectorsMtx = sync.Mutex{} initiatedCollectors = make(map[string]Collector) - collectorStateGlobal = make(map[string]*bool) + collectorStateGlobal = make(map[string]bool) + collectorFlagState = make(map[string]*bool) availableCollectors = make([]string, 0) forcedCollectors = map[string]bool{} // collectors which have been explicitly enabled or disabled ) @@ -61,6 +62,14 @@ var ( func GetDefaults() map[string]bool { defaults := make(map[string]bool) for k, v := range collectorStateGlobal { + defaults[k] = v + } + return defaults +} + +func GetFlagDefaults() map[string]bool { + defaults := make(map[string]bool) + for k, v := range collectorFlagState { defaults[k] = *v } return defaults @@ -84,7 +93,8 @@ func registerCollector(collector string, isDefaultEnabled bool, factory func(con defaultValue := fmt.Sprintf("%v", isDefaultEnabled) flag := kingpin.Flag(flagName, flagHelp).Default(defaultValue).Action(collectorFlagAction(collector)).Bool() - collectorStateGlobal[collector] = flag + collectorStateGlobal[collector] = isDefaultEnabled + collectorFlagState[collector] = flag factories[collector] = factory } @@ -98,9 +108,9 @@ type NodeCollector struct { // DisableDefaultCollectors sets the collector state to false for all collectors which // have not been explicitly enabled on the command line. func DisableDefaultCollectors() { - for c := range collectorStateGlobal { + for c := range collectorFlagState { if _, ok := forcedCollectors[c]; !ok { - *collectorStateGlobal[c] = false + *collectorFlagState[c] = false } } } diff --git a/node_exporter.go b/node_exporter.go index 74aa067188..68eba49956 100644 --- a/node_exporter.go +++ b/node_exporter.go @@ -102,7 +102,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // (in which case it will log all the collectors enabled via command-line // flags). func (h *handler) innerHandler(filters ...string) (http.Handler, error) { - nc, err := collector.NewNodeCollector(h.collectorConfig, collector.GetDefaults(), h.logger, filters...) + nc, err := collector.NewNodeCollector(h.collectorConfig, collector.GetFlagDefaults(), h.logger, filters...) if err != nil { return nil, fmt.Errorf("couldn't create collector: %s", err) }