package registry import ( "context" "git.faercol.me/monitoring/sys-exporter/collector" "git.faercol.me/monitoring/sys-exporter/config" "github.com/prometheus/client_golang/prometheus" "go.uber.org/zap" ) var R CollectorRegistry type CollectorRegistry struct { promRegistry *prometheus.Registry collectors map[string]collector.Collector conf *config.AppConfig l *zap.SugaredLogger } 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 } 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) } } r.l.Infof("Started %d collectors", loaded) } func init() { R = CollectorRegistry{ promRegistry: prometheus.NewRegistry(), collectors: make(map[string]collector.Collector), } }