mirror of
https://github.com/prometheus/node_exporter.git
synced 2025-01-30 07:12:35 -08:00
Only iniate collectors once
When /metrics is called for specific collectors, the collectors are initialed every time. Which means that we spend a lot of time re-initiating the same collectors again and again. Especially, some collectors make the assumptions that that are initiated once - e.g. systemd collector generates info message upon initiation. Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>
This commit is contained in:
parent
e1db354611
commit
2e20d668f2
|
@ -50,9 +50,11 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
factories = make(map[string]func(logger log.Logger) (Collector, error))
|
factories = make(map[string]func(logger log.Logger) (Collector, error))
|
||||||
collectorState = make(map[string]*bool)
|
initiatedCollectorsMtx = sync.Mutex{}
|
||||||
forcedCollectors = map[string]bool{} // collectors which have been explicitly enabled or disabled
|
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)) {
|
func registerCollector(collector string, isDefaultEnabled bool, factory func(logger log.Logger) (Collector, error)) {
|
||||||
|
@ -115,14 +117,21 @@ func NewNodeCollector(logger log.Logger, filters ...string) (*NodeCollector, err
|
||||||
f[filter] = true
|
f[filter] = true
|
||||||
}
|
}
|
||||||
collectors := make(map[string]Collector)
|
collectors := make(map[string]Collector)
|
||||||
|
initiatedCollectorsMtx.Lock()
|
||||||
|
defer initiatedCollectorsMtx.Unlock()
|
||||||
for key, enabled := range collectorState {
|
for key, enabled := range collectorState {
|
||||||
if *enabled {
|
if *enabled {
|
||||||
collector, err := factories[key](log.With(logger, "collector", key))
|
if collector, ok := initiatedCollectors[key]; ok {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if len(f) == 0 || f[key] {
|
|
||||||
collectors[key] = collector
|
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
|
||||||
|
initiatedCollectors[key] = collector
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue