Link collectors to config and logging
This commit is contained in:
parent
fa8c65f302
commit
3deeb6b159
9 changed files with 94 additions and 27 deletions
|
@ -37,14 +37,21 @@ func main() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger, err := conf.ZapConfig().Build()
|
rawL, err := conf.ZapConfig().Build()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Failed to init logger: %s\n", err)
|
fmt.Fprintf(os.Stderr, "Failed to init logger: %s\n", err)
|
||||||
os.Exit(1)
|
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")
|
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.Handle("/metrics", promhttp.HandlerFor(registry.R.PromRegistry(), promhttp.HandlerOpts{}))
|
||||||
http.ListenAndServe(":2112", nil)
|
http.ListenAndServe(":2112", nil)
|
||||||
|
|
|
@ -4,10 +4,11 @@ import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Collector interface {
|
type Collector interface {
|
||||||
Collect() interface{}
|
Collect() interface{}
|
||||||
PromCollector() prometheus.Collector
|
PromCollector() prometheus.Collector
|
||||||
Run(ctx context.Context)
|
Run(ctx context.Context, l *zap.SugaredLogger)
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
|
|
||||||
"git.faercol.me/monitoring/sys-exporter/registry"
|
"git.faercol.me/monitoring/sys-exporter/registry"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
const loadAvgFileLocation = "/proc/loadavg"
|
const loadAvgFileLocation = "/proc/loadavg"
|
||||||
|
@ -18,6 +19,7 @@ type LoadAvgCollector struct {
|
||||||
loadAvgFileLocation string
|
loadAvgFileLocation string
|
||||||
promExporter *prometheus.SummaryVec
|
promExporter *prometheus.SummaryVec
|
||||||
updateFreq time.Duration
|
updateFreq time.Duration
|
||||||
|
l *zap.SugaredLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *LoadAvgCollector) Collect() interface{} {
|
func (c *LoadAvgCollector) Collect() interface{} {
|
||||||
|
@ -52,18 +54,23 @@ func (c *LoadAvgCollector) update() error {
|
||||||
return nil
|
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 {
|
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 {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
|
c.l.Debug("Stopping collector")
|
||||||
return
|
return
|
||||||
case <-time.After(c.updateFreq):
|
case <-time.After(c.updateFreq):
|
||||||
|
c.l.Debug("Updating collector")
|
||||||
if err := c.update(); err != nil {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"git.faercol.me/monitoring/sys-exporter/registry"
|
"git.faercol.me/monitoring/sys-exporter/registry"
|
||||||
"git.faercol.me/monitoring/sys-exporter/utils"
|
"git.faercol.me/monitoring/sys-exporter/utils"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
const meminfoFileLocation = "/proc/meminfo"
|
const meminfoFileLocation = "/proc/meminfo"
|
||||||
|
@ -51,6 +52,8 @@ type MeminfoCollector struct {
|
||||||
|
|
||||||
promExporter *prometheus.SummaryVec
|
promExporter *prometheus.SummaryVec
|
||||||
updateFreq time.Duration
|
updateFreq time.Duration
|
||||||
|
|
||||||
|
l *zap.SugaredLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *MeminfoCollector) Collect() interface{} {
|
func (c *MeminfoCollector) Collect() interface{} {
|
||||||
|
@ -80,18 +83,23 @@ func (c *MeminfoCollector) update() error {
|
||||||
return nil
|
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 {
|
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 {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
|
c.l.Debug("Stopping collector")
|
||||||
return
|
return
|
||||||
case <-time.After(c.updateFreq):
|
case <-time.After(c.updateFreq):
|
||||||
|
c.l.Debug("Updating collector")
|
||||||
if err := c.update(); err != nil {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
|
|
||||||
"git.faercol.me/monitoring/sys-exporter/registry"
|
"git.faercol.me/monitoring/sys-exporter/registry"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -34,6 +35,8 @@ type PacmanCollector struct {
|
||||||
promPackages *prometheus.GaugeVec
|
promPackages *prometheus.GaugeVec
|
||||||
pendingUpdates int
|
pendingUpdates int
|
||||||
updateFreq time.Duration
|
updateFreq time.Duration
|
||||||
|
|
||||||
|
l *zap.SugaredLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *PacmanCollector) Collect() interface{} {
|
func (c *PacmanCollector) Collect() interface{} {
|
||||||
|
@ -70,24 +73,29 @@ func (c *PacmanCollector) updatePendingUpdates() error {
|
||||||
return nil
|
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 {
|
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 {
|
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 {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
|
c.l.Debug("Stopping collector")
|
||||||
return
|
return
|
||||||
case <-time.After(c.updateFreq):
|
case <-time.After(c.updateFreq):
|
||||||
|
c.l.Debug("Updating collector")
|
||||||
if err := c.updateInstalledPackages(); err != nil {
|
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 {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
|
|
||||||
"git.faercol.me/monitoring/sys-exporter/registry"
|
"git.faercol.me/monitoring/sys-exporter/registry"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
type serviceStatus struct {
|
type serviceStatus struct {
|
||||||
|
@ -31,6 +32,8 @@ func (s serviceStatus) unitType() string {
|
||||||
type SystemdCollector struct {
|
type SystemdCollector struct {
|
||||||
promExporter *prometheus.GaugeVec
|
promExporter *prometheus.GaugeVec
|
||||||
updateFreq time.Duration
|
updateFreq time.Duration
|
||||||
|
|
||||||
|
l *zap.SugaredLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *SystemdCollector) Collect() interface{} {
|
func (c *SystemdCollector) Collect() interface{} {
|
||||||
|
@ -97,18 +100,23 @@ func (c *SystemdCollector) update() error {
|
||||||
return nil
|
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 {
|
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 {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
|
c.l.Debug("Stopping collector")
|
||||||
return
|
return
|
||||||
case <-time.After(c.updateFreq):
|
case <-time.After(c.updateFreq):
|
||||||
|
c.l.Debug("Updating collector")
|
||||||
if err := c.update(); err != nil {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
|
|
||||||
"git.faercol.me/monitoring/sys-exporter/registry"
|
"git.faercol.me/monitoring/sys-exporter/registry"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
const procUptimeLocation = "/proc/uptime"
|
const procUptimeLocation = "/proc/uptime"
|
||||||
|
@ -20,6 +21,8 @@ type UptimeCollector struct {
|
||||||
uptimeFreq time.Duration
|
uptimeFreq time.Duration
|
||||||
uptime time.Duration
|
uptime time.Duration
|
||||||
uptimeProm prometheus.Gauge
|
uptimeProm prometheus.Gauge
|
||||||
|
|
||||||
|
l *zap.SugaredLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *UptimeCollector) Collect() interface{} {
|
func (c *UptimeCollector) Collect() interface{} {
|
||||||
|
@ -51,18 +54,23 @@ func (c *UptimeCollector) PromCollector() prometheus.Collector {
|
||||||
return c.uptimeProm
|
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 {
|
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 {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
|
c.l.Debug("Stopping collector")
|
||||||
return
|
return
|
||||||
case <-time.After(c.uptimeFreq):
|
case <-time.After(c.uptimeFreq):
|
||||||
|
c.l.Debug("Updating collector")
|
||||||
if err := c.update(); err != nil {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@ func (c logConfigJson) logConfig() (LogConfig, error) {
|
||||||
|
|
||||||
type appConfigJson struct {
|
type appConfigJson struct {
|
||||||
LogConfig logConfigJson `yaml:"logging"`
|
LogConfig logConfigJson `yaml:"logging"`
|
||||||
|
Collectors []string `yaml:"collectors"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c appConfigJson) appConfig() (AppConfig, error) {
|
func (c appConfigJson) appConfig() (AppConfig, error) {
|
||||||
|
@ -37,6 +38,7 @@ func (c appConfigJson) appConfig() (AppConfig, error) {
|
||||||
|
|
||||||
return AppConfig{
|
return AppConfig{
|
||||||
LogConfig: lc,
|
LogConfig: lc,
|
||||||
|
Collectors: c.Collectors,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +47,6 @@ type LogConfig struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *LogConfig) ZapConfig() zap.Config {
|
func (c *LogConfig) ZapConfig() zap.Config {
|
||||||
fmt.Println(c.Level)
|
|
||||||
zapC := zap.Config{
|
zapC := zap.Config{
|
||||||
Level: c.Level,
|
Level: c.Level,
|
||||||
Development: true, // TODO: remove later
|
Development: true, // TODO: remove later
|
||||||
|
@ -61,7 +62,8 @@ func (c *LogConfig) ZapConfig() zap.Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
type AppConfig struct {
|
type AppConfig struct {
|
||||||
LogConfig `yaml:"logging"`
|
LogConfig
|
||||||
|
Collectors []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(filepath string) (*AppConfig, error) {
|
func New(filepath string) (*AppConfig, error) {
|
||||||
|
|
|
@ -4,7 +4,9 @@ import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"git.faercol.me/monitoring/sys-exporter/collector"
|
"git.faercol.me/monitoring/sys-exporter/collector"
|
||||||
|
"git.faercol.me/monitoring/sys-exporter/config"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
var R CollectorRegistry
|
var R CollectorRegistry
|
||||||
|
@ -12,6 +14,8 @@ var R CollectorRegistry
|
||||||
type CollectorRegistry struct {
|
type CollectorRegistry struct {
|
||||||
promRegistry *prometheus.Registry
|
promRegistry *prometheus.Registry
|
||||||
collectors map[string]collector.Collector
|
collectors map[string]collector.Collector
|
||||||
|
conf *config.AppConfig
|
||||||
|
l *zap.SugaredLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *CollectorRegistry) RegisterCollector(name string, c collector.Collector) error {
|
func (r *CollectorRegistry) RegisterCollector(name string, c collector.Collector) error {
|
||||||
|
@ -28,10 +32,24 @@ func (r *CollectorRegistry) PromRegistry() *prometheus.Registry {
|
||||||
return r.promRegistry
|
return r.promRegistry
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *CollectorRegistry) Run(ctx context.Context) {
|
func (r *CollectorRegistry) Run(ctx context.Context, conf *config.AppConfig, l *zap.SugaredLogger) {
|
||||||
for _, c := range r.collectors {
|
r.l = l
|
||||||
go c.Run(ctx)
|
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() {
|
func init() {
|
||||||
|
|
Loading…
Reference in a new issue