Skip to content

Commit

Permalink
Rearrange kingpin
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdurham committed Oct 3, 2023
1 parent 5bc1573 commit d4879a5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
18 changes: 14 additions & 4 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,23 @@ 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
)

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
Expand All @@ -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
}
Expand All @@ -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
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion node_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit d4879a5

Please sign in to comment.