diff --git a/cmd/server/main.go b/cmd/server/main.go index 61e6d5f..98c2eaf 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -37,14 +37,21 @@ func main() { os.Exit(1) } - logger, err := conf.ZapConfig().Build() + rawL, err := conf.ZapConfig().Build() if err != nil { fmt.Fprintf(os.Stderr, "Failed to init logger: %s\n", err) os.Exit(1) } + logger := rawL.Sugar() + logger.Infof("Initialized logger to %v level", logger.Level()) + + logger.Infof("Loading %d collectors", len(conf.Collectors)) + if len(conf.Collectors) == 0 { + logger.Warn("No collector present in the config, is this expected?") + } logger.Info("Starting server") - registry.R.Run(context.Background()) + registry.R.Run(context.Background(), conf, logger) http.Handle("/metrics", promhttp.HandlerFor(registry.R.PromRegistry(), promhttp.HandlerOpts{})) http.ListenAndServe(":2112", nil) diff --git a/collector/collector.go b/collector/collector.go index 0df1698..ae8cd08 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -4,10 +4,11 @@ import ( "context" "github.com/prometheus/client_golang/prometheus" + "go.uber.org/zap" ) type Collector interface { Collect() interface{} PromCollector() prometheus.Collector - Run(ctx context.Context) + Run(ctx context.Context, l *zap.SugaredLogger) } diff --git a/collector/loadavg/loadavg.go b/collector/loadavg/loadavg.go index 8cbdfc0..29a8588 100644 --- a/collector/loadavg/loadavg.go +++ b/collector/loadavg/loadavg.go @@ -10,6 +10,7 @@ import ( "git.faercol.me/monitoring/sys-exporter/registry" "github.com/prometheus/client_golang/prometheus" + "go.uber.org/zap" ) const loadAvgFileLocation = "/proc/loadavg" @@ -18,6 +19,7 @@ type LoadAvgCollector struct { loadAvgFileLocation string promExporter *prometheus.SummaryVec updateFreq time.Duration + l *zap.SugaredLogger } func (c *LoadAvgCollector) Collect() interface{} { @@ -52,18 +54,23 @@ func (c *LoadAvgCollector) update() error { return nil } -func (c *LoadAvgCollector) Run(ctx context.Context) { +func (c *LoadAvgCollector) Run(ctx context.Context, l *zap.SugaredLogger) { + c.l = l + + c.l.Debug("Initializing collector") if err := c.update(); err != nil { - fmt.Printf("Failed to init loadavg collector: %s\n", err) + c.l.Errorf("Failed to init collector: %s\n", err) } for { select { case <-ctx.Done(): + c.l.Debug("Stopping collector") return case <-time.After(c.updateFreq): + c.l.Debug("Updating collector") if err := c.update(); err != nil { - fmt.Printf("Failed to update loadavg collector: %s\n", err) + c.l.Errorf("Failed to update collector: %s\n", err) } } } diff --git a/collector/meminfo/meminfo.go b/collector/meminfo/meminfo.go index f0dd887..07455b2 100644 --- a/collector/meminfo/meminfo.go +++ b/collector/meminfo/meminfo.go @@ -12,6 +12,7 @@ import ( "git.faercol.me/monitoring/sys-exporter/registry" "git.faercol.me/monitoring/sys-exporter/utils" "github.com/prometheus/client_golang/prometheus" + "go.uber.org/zap" ) const meminfoFileLocation = "/proc/meminfo" @@ -51,6 +52,8 @@ type MeminfoCollector struct { promExporter *prometheus.SummaryVec updateFreq time.Duration + + l *zap.SugaredLogger } func (c *MeminfoCollector) Collect() interface{} { @@ -80,18 +83,23 @@ func (c *MeminfoCollector) update() error { return nil } -func (c *MeminfoCollector) Run(ctx context.Context) { +func (c *MeminfoCollector) Run(ctx context.Context, l *zap.SugaredLogger) { + c.l = l + + c.l.Debug("Initializing collector") if err := c.update(); err != nil { - fmt.Printf("Failed to init meminfo collector: %s\n", err) + c.l.Errorf("Failed to init collector: %s\n", err) } for { select { case <-ctx.Done(): + c.l.Debug("Stopping collector") return case <-time.After(c.updateFreq): + c.l.Debug("Updating collector") if err := c.update(); err != nil { - fmt.Printf("Failed to update meminfo collector: %s\n", err) + c.l.Errorf("Failed to update collector: %s\n", err) } } } diff --git a/collector/pacman/pacman.go b/collector/pacman/pacman.go index 7a70a5d..19f67d2 100644 --- a/collector/pacman/pacman.go +++ b/collector/pacman/pacman.go @@ -9,6 +9,7 @@ import ( "git.faercol.me/monitoring/sys-exporter/registry" "github.com/prometheus/client_golang/prometheus" + "go.uber.org/zap" ) const ( @@ -34,6 +35,8 @@ type PacmanCollector struct { promPackages *prometheus.GaugeVec pendingUpdates int updateFreq time.Duration + + l *zap.SugaredLogger } func (c *PacmanCollector) Collect() interface{} { @@ -70,24 +73,29 @@ func (c *PacmanCollector) updatePendingUpdates() error { return nil } -func (c *PacmanCollector) Run(ctx context.Context) { +func (c *PacmanCollector) Run(ctx context.Context, l *zap.SugaredLogger) { + c.l = l + + c.l.Debug("Initializing collector") if err := c.updateInstalledPackages(); err != nil { - fmt.Printf("Failed to init count of installed packages: %s\n", err) + c.l.Errorf("Failed to init count of installed packages: %s\n", err) } if err := c.updatePendingUpdates(); err != nil { - fmt.Printf("Failed to init count of updates: %s\n", err) + c.l.Errorf("Failed to init count of updates: %s\n", err) } for { select { case <-ctx.Done(): + c.l.Debug("Stopping collector") return case <-time.After(c.updateFreq): + c.l.Debug("Updating collector") if err := c.updateInstalledPackages(); err != nil { - fmt.Printf("Failed to update count of installed packages: %s\n", err) + c.l.Errorf("Failed to update count of installed packages: %s\n", err) } if err := c.updatePendingUpdates(); err != nil { - fmt.Printf("Failed to update count of updates: %s\n", err) + c.l.Errorf("Failed to update count of updates: %s\n", err) } } } diff --git a/collector/systemd/systemd.go b/collector/systemd/systemd.go index 122a3e5..af7e488 100644 --- a/collector/systemd/systemd.go +++ b/collector/systemd/systemd.go @@ -10,6 +10,7 @@ import ( "git.faercol.me/monitoring/sys-exporter/registry" "github.com/prometheus/client_golang/prometheus" + "go.uber.org/zap" ) type serviceStatus struct { @@ -31,6 +32,8 @@ func (s serviceStatus) unitType() string { type SystemdCollector struct { promExporter *prometheus.GaugeVec updateFreq time.Duration + + l *zap.SugaredLogger } func (c *SystemdCollector) Collect() interface{} { @@ -97,18 +100,23 @@ func (c *SystemdCollector) update() error { return nil } -func (c *SystemdCollector) Run(ctx context.Context) { +func (c *SystemdCollector) Run(ctx context.Context, l *zap.SugaredLogger) { + c.l = l + + c.l.Debug("Initializing collector") if err := c.update(); err != nil { - fmt.Printf("Failed to init systemd status: %s\n", err) + c.l.Errorf("Failed to init collector: %s\n", err) } for { select { case <-ctx.Done(): + c.l.Debug("Stopping collector") return case <-time.After(c.updateFreq): + c.l.Debug("Updating collector") if err := c.update(); err != nil { - fmt.Printf("Failed to update systemd status: %s\n", err) + c.l.Errorf("Failed to update collector: %s\n", err) } } } diff --git a/collector/uptime/uptime.go b/collector/uptime/uptime.go index 967637f..2b58400 100644 --- a/collector/uptime/uptime.go +++ b/collector/uptime/uptime.go @@ -10,6 +10,7 @@ import ( "git.faercol.me/monitoring/sys-exporter/registry" "github.com/prometheus/client_golang/prometheus" + "go.uber.org/zap" ) const procUptimeLocation = "/proc/uptime" @@ -20,6 +21,8 @@ type UptimeCollector struct { uptimeFreq time.Duration uptime time.Duration uptimeProm prometheus.Gauge + + l *zap.SugaredLogger } func (c *UptimeCollector) Collect() interface{} { @@ -51,18 +54,23 @@ func (c *UptimeCollector) PromCollector() prometheus.Collector { return c.uptimeProm } -func (c *UptimeCollector) Run(ctx context.Context) { +func (c *UptimeCollector) Run(ctx context.Context, l *zap.SugaredLogger) { + c.l = l + + c.l.Debug("Initializing collector") if err := c.update(); err != nil { - fmt.Printf("Failed to init collector: %s\n", err) + c.l.Errorf("Failed to init collector: %s\n", err) } for { select { case <-ctx.Done(): + c.l.Debug("Stopping collector") return case <-time.After(c.uptimeFreq): + c.l.Debug("Updating collector") if err := c.update(); err != nil { - fmt.Printf("Failed to update collector: %s\n", err) + c.l.Errorf("Failed to update collector: %s\n", err) } } } diff --git a/config/config.go b/config/config.go index 55a9bea..8cf9625 100644 --- a/config/config.go +++ b/config/config.go @@ -26,7 +26,8 @@ func (c logConfigJson) logConfig() (LogConfig, error) { } type appConfigJson struct { - LogConfig logConfigJson `yaml:"logging"` + LogConfig logConfigJson `yaml:"logging"` + Collectors []string `yaml:"collectors"` } func (c appConfigJson) appConfig() (AppConfig, error) { @@ -36,7 +37,8 @@ func (c appConfigJson) appConfig() (AppConfig, error) { } return AppConfig{ - LogConfig: lc, + LogConfig: lc, + Collectors: c.Collectors, }, nil } @@ -45,7 +47,6 @@ type LogConfig struct { } func (c *LogConfig) ZapConfig() zap.Config { - fmt.Println(c.Level) zapC := zap.Config{ Level: c.Level, Development: true, // TODO: remove later @@ -61,7 +62,8 @@ func (c *LogConfig) ZapConfig() zap.Config { } type AppConfig struct { - LogConfig `yaml:"logging"` + LogConfig + Collectors []string } func New(filepath string) (*AppConfig, error) { diff --git a/registry/registry.go b/registry/registry.go index d205444..90c08bf 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -4,7 +4,9 @@ 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 @@ -12,6 +14,8 @@ 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 { @@ -28,10 +32,24 @@ func (r *CollectorRegistry) PromRegistry() *prometheus.Registry { return r.promRegistry } -func (r *CollectorRegistry) Run(ctx context.Context) { - for _, c := range r.collectors { - go c.Run(ctx) +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() {