Move collector to own package

This commit is contained in:
Robert Jacob 2020-06-27 17:53:13 +02:00
parent 02bca43416
commit 1eda093550
2 changed files with 25 additions and 24 deletions

View file

@ -1,4 +1,4 @@
package main package collector
import ( import (
"sync" "sync"
@ -104,11 +104,11 @@ var (
nil) nil)
) )
type netatmoCollector struct { type NetatmoCollector struct {
log logrus.FieldLogger Log logrus.FieldLogger
refreshInterval time.Duration RefreshInterval time.Duration
staleThreshold time.Duration StaleThreshold time.Duration
client *netatmo.Client Client *netatmo.Client
lastRefresh time.Time lastRefresh time.Time
lastRefreshError error lastRefreshError error
cacheLock sync.RWMutex cacheLock sync.RWMutex
@ -116,16 +116,16 @@ type netatmoCollector struct {
cachedData *netatmo.DeviceCollection cachedData *netatmo.DeviceCollection
} }
func (c *netatmoCollector) Describe(dChan chan<- *prometheus.Desc) { func (c *NetatmoCollector) Describe(dChan chan<- *prometheus.Desc) {
dChan <- updatedDesc dChan <- updatedDesc
dChan <- tempDesc dChan <- tempDesc
dChan <- humidityDesc dChan <- humidityDesc
dChan <- cotwoDesc dChan <- cotwoDesc
} }
func (c *netatmoCollector) Collect(mChan chan<- prometheus.Metric) { func (c *NetatmoCollector) Collect(mChan chan<- prometheus.Metric) {
now := time.Now() now := time.Now()
if now.Sub(c.lastRefresh) >= c.refreshInterval { if now.Sub(c.lastRefresh) >= c.RefreshInterval {
go c.refreshData(now) go c.refreshData(now)
} }
@ -152,13 +152,13 @@ func (c *netatmoCollector) Collect(mChan chan<- prometheus.Metric) {
} }
} }
func (c *netatmoCollector) refreshData(now time.Time) { func (c *NetatmoCollector) refreshData(now time.Time) {
c.log.Debugf("Refresh interval elapsed: %s > %s", now.Sub(c.lastRefresh), c.refreshInterval) c.Log.Debugf("Refresh interval elapsed: %s > %s", now.Sub(c.lastRefresh), c.RefreshInterval)
c.lastRefresh = now c.lastRefresh = now
devices, err := c.client.Read() devices, err := c.Client.Read()
if err != nil { if err != nil {
c.log.Errorf("Error during refresh: %s", err) c.Log.Errorf("Error during refresh: %s", err)
c.lastRefreshError = err c.lastRefreshError = err
return return
} }
@ -169,18 +169,18 @@ func (c *netatmoCollector) refreshData(now time.Time) {
c.cachedData = devices c.cachedData = devices
} }
func (c *netatmoCollector) collectData(ch chan<- prometheus.Metric, device *netatmo.Device, stationName string) { func (c *NetatmoCollector) collectData(ch chan<- prometheus.Metric, device *netatmo.Device, stationName string) {
moduleName := device.ModuleName moduleName := device.ModuleName
data := device.DashboardData data := device.DashboardData
if data.LastMeasure == nil { if data.LastMeasure == nil {
c.log.Debugf("No data available.") c.Log.Debugf("No data available.")
return return
} }
date := time.Unix(*data.LastMeasure, 0) date := time.Unix(*data.LastMeasure, 0)
if time.Since(date) > c.staleThreshold { if time.Since(date) > c.StaleThreshold {
c.log.Debugf("Data is stale for %s: %s > %s", moduleName, time.Since(date), c.staleThreshold) c.Log.Debugf("Data is stale for %s: %s > %s", moduleName, time.Since(date), c.StaleThreshold)
return return
} }
@ -229,10 +229,10 @@ func (c *netatmoCollector) collectData(ch chan<- prometheus.Metric, device *neta
} }
} }
func (c *netatmoCollector) sendMetric(ch chan<- prometheus.Metric, desc *prometheus.Desc, valueType prometheus.ValueType, value float64, labelValues ...string) { func (c *NetatmoCollector) sendMetric(ch chan<- prometheus.Metric, desc *prometheus.Desc, valueType prometheus.ValueType, value float64, labelValues ...string) {
m, err := prometheus.NewConstMetric(desc, valueType, value, labelValues...) m, err := prometheus.NewConstMetric(desc, valueType, value, labelValues...)
if err != nil { if err != nil {
c.log.Errorf("Error creating %s metric: %s", updatedDesc.String(), err) c.Log.Errorf("Error creating %s metric: %s", updatedDesc.String(), err)
return return
} }
ch <- m ch <- m

11
main.go
View file

@ -8,6 +8,7 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/xperimental/netatmo-exporter/internal/collector"
"github.com/xperimental/netatmo-exporter/internal/config" "github.com/xperimental/netatmo-exporter/internal/config"
) )
@ -36,11 +37,11 @@ func main() {
log.Fatalf("Error creating client: %s", err) log.Fatalf("Error creating client: %s", err)
} }
metrics := &netatmoCollector{ metrics := &collector.NetatmoCollector{
log: log, Log: log,
client: client, Client: client,
refreshInterval: cfg.RefreshInterval, RefreshInterval: cfg.RefreshInterval,
staleThreshold: cfg.StaleDuration, StaleThreshold: cfg.StaleDuration,
} }
prometheus.MustRegister(metrics) prometheus.MustRegister(metrics)