32 lines
862 B
Go
32 lines
862 B
Go
package collector
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
)
|
|
|
|
// Group registers multiple device collectors as one Prometheus collector.
|
|
// Collecting devices concurrently prevents one slow device from delaying all others.
|
|
type Group []prometheus.Collector
|
|
|
|
// Describe forwards descriptors from every registered device collector.
|
|
func (group Group) Describe(channel chan<- *prometheus.Desc) {
|
|
for _, deviceCollector := range group {
|
|
deviceCollector.Describe(channel)
|
|
}
|
|
}
|
|
|
|
// Collect scrapes all configured devices concurrently.
|
|
func (group Group) Collect(channel chan<- prometheus.Metric) {
|
|
var waitGroup sync.WaitGroup
|
|
waitGroup.Add(len(group))
|
|
for _, deviceCollector := range group {
|
|
go func(current prometheus.Collector) {
|
|
defer waitGroup.Done()
|
|
current.Collect(channel)
|
|
}(deviceCollector)
|
|
}
|
|
waitGroup.Wait()
|
|
}
|