mirror of
https://github.com/prometheus/node_exporter.git
synced 2024-12-28 23:19:46 -08:00
Merge pull request #2048 from prometheus/init-collectors
Only initiate collectors once
This commit is contained in:
commit
5b13775dc8
|
@ -50,9 +50,11 @@ const (
|
|||
)
|
||||
|
||||
var (
|
||||
factories = make(map[string]func(logger log.Logger) (Collector, error))
|
||||
collectorState = make(map[string]*bool)
|
||||
forcedCollectors = map[string]bool{} // collectors which have been explicitly enabled or disabled
|
||||
factories = make(map[string]func(logger log.Logger) (Collector, error))
|
||||
initiatedCollectorsMtx = sync.Mutex{}
|
||||
initiatedCollectors = make(map[string]Collector)
|
||||
collectorState = make(map[string]*bool)
|
||||
forcedCollectors = map[string]bool{} // collectors which have been explicitly enabled or disabled
|
||||
)
|
||||
|
||||
func registerCollector(collector string, isDefaultEnabled bool, factory func(logger log.Logger) (Collector, error)) {
|
||||
|
@ -115,15 +117,21 @@ func NewNodeCollector(logger log.Logger, filters ...string) (*NodeCollector, err
|
|||
f[filter] = true
|
||||
}
|
||||
collectors := make(map[string]Collector)
|
||||
initiatedCollectorsMtx.Lock()
|
||||
defer initiatedCollectorsMtx.Unlock()
|
||||
for key, enabled := range collectorState {
|
||||
if *enabled {
|
||||
if !*enabled || (len(f) > 0 && !f[key]) {
|
||||
continue
|
||||
}
|
||||
if collector, ok := initiatedCollectors[key]; ok {
|
||||
collectors[key] = collector
|
||||
} else {
|
||||
collector, err := factories[key](log.With(logger, "collector", key))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(f) == 0 || f[key] {
|
||||
collectors[key] = collector
|
||||
}
|
||||
collectors[key] = collector
|
||||
initiatedCollectors[key] = collector
|
||||
}
|
||||
}
|
||||
return &NodeCollector{Collectors: collectors, logger: logger}, nil
|
||||
|
|
Loading…
Reference in a new issue