sys-exporter/registry/registry.go

61 lines
1.5 KiB
Go
Raw Normal View History

2024-12-01 16:00:52 +00:00
package registry
import (
"context"
"git.faercol.me/monitoring/sys-exporter/collector"
2024-12-02 17:37:19 +00:00
"git.faercol.me/monitoring/sys-exporter/config"
2024-12-01 16:00:52 +00:00
"github.com/prometheus/client_golang/prometheus"
2024-12-02 17:37:19 +00:00
"go.uber.org/zap"
2024-12-01 16:00:52 +00:00
)
var R CollectorRegistry
type CollectorRegistry struct {
promRegistry *prometheus.Registry
collectors map[string]collector.Collector
2024-12-02 17:37:19 +00:00
conf *config.AppConfig
l *zap.SugaredLogger
2024-12-01 16:00:52 +00:00
}
func (r *CollectorRegistry) RegisterCollector(name string, c collector.Collector) error {
r.collectors[name] = c
return r.promRegistry.Register(c.PromCollector())
}
func (r *CollectorRegistry) MustRegisterCollector(name string, c collector.Collector) {
r.collectors[name] = c
r.promRegistry.MustRegister(c.PromCollector())
}
func (r *CollectorRegistry) PromRegistry() *prometheus.Registry {
return r.promRegistry
}
2024-12-02 17:37:19 +00:00
func (r *CollectorRegistry) Run(ctx context.Context, conf *config.AppConfig, l *zap.SugaredLogger) {
r.l = l
r.conf = conf
r.l.Info("Starting all collectors")
loaded := 0
for _, enabledCollector := range r.conf.Collectors {
c, ok := r.collectors[enabledCollector]
if ok {
r.l.Debugf("Starting collector %s", enabledCollector)
go c.Run(ctx, r.l.With("collector", enabledCollector))
loaded++
} else {
r.l.Warnf("Collector %s does not exist, ignoring", enabledCollector)
}
2024-12-01 16:00:52 +00:00
}
2024-12-02 17:37:19 +00:00
r.l.Infof("Started %d collectors", loaded)
2024-12-01 16:00:52 +00:00
}
func init() {
R = CollectorRegistry{
promRegistry: prometheus.NewRegistry(),
collectors: make(map[string]collector.Collector),
}
}