Skip to content

Commit

Permalink
Allow the passing in of enabled collectors.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdurham committed Oct 3, 2023
1 parent e78bc30 commit 5bc1573
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
9 changes: 4 additions & 5 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var (
forcedCollectors = map[string]bool{} // collectors which have been explicitly enabled or disabled
)

func getDefaults() map[string]bool {
func GetDefaults() map[string]bool {
defaults := make(map[string]bool)
for k, v := range collectorStateGlobal {
defaults[k] = *v
Expand Down Expand Up @@ -118,11 +118,10 @@ func collectorFlagAction(collector string) func(ctx *kingpin.ParseContext) error
}

// NewNodeCollector creates a new NodeCollector.
func NewNodeCollector(config *NodeCollectorConfig, logger log.Logger, filters ...string) (*NodeCollector, error) {
func NewNodeCollector(config *NodeCollectorConfig, enabledCollectors map[string]bool, logger log.Logger, filters ...string) (*NodeCollector, error) {
f := make(map[string]bool)
defaults := getDefaults()
for _, filter := range filters {
enabled, exist := defaults[filter]
enabled, exist := enabledCollectors[filter]
if !exist {
return nil, fmt.Errorf("missing collector: %s", filter)
}
Expand All @@ -134,7 +133,7 @@ func NewNodeCollector(config *NodeCollectorConfig, logger log.Logger, filters ..
collectors := make(map[string]Collector)
initiatedCollectorsMtx.Lock()
defer initiatedCollectorsMtx.Unlock()
for key, enabled := range defaults {
for key, enabled := range enabledCollectors {
if !enabled || (len(f) > 0 && !f[key]) {
continue
}
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, h.logger, filters...)
nc, err := collector.NewNodeCollector(h.collectorConfig, collector.GetDefaults(), h.logger, filters...)
if err != nil {
return nil, fmt.Errorf("couldn't create collector: %s", err)
}
Expand Down

0 comments on commit 5bc1573

Please sign in to comment.