Link collectors to config and logging

This commit is contained in:
Melora Hugues 2024-12-02 18:37:19 +01:00
parent fa8c65f302
commit 3deeb6b159
Signed by: faercol
SSH key fingerprint: SHA256:KQRA4MHbar0GIvFX08PLxgD75bldzhxxAjVxs6r5w6w
9 changed files with 94 additions and 27 deletions

View file

@ -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)

View file

@ -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)
}

View file

@ -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)
}
}
}

View file

@ -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)
}
}
}

View file

@ -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)
}
}
}

View file

@ -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)
}
}
}

View file

@ -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)
}
}
}

View file

@ -27,6 +27,7 @@ func (c logConfigJson) logConfig() (LogConfig, error) {
type appConfigJson struct {
LogConfig logConfigJson `yaml:"logging"`
Collectors []string `yaml:"collectors"`
}
func (c appConfigJson) appConfig() (AppConfig, error) {
@ -37,6 +38,7 @@ func (c appConfigJson) appConfig() (AppConfig, error) {
return AppConfig{
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) {

View file

@ -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,12 +32,26 @@ 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() {
R = CollectorRegistry{
promRegistry: prometheus.NewRegistry(),