Add metric for refresh duration

This commit is contained in:
Robert Jacob 2020-06-27 17:59:17 +02:00
parent 1eda093550
commit 73f4c697da
1 changed files with 19 additions and 9 deletions

View File

@ -20,6 +20,10 @@ var (
refreshPrefix+"_time", refreshPrefix+"_time",
"Contains the time of the last refresh try, successful or not.", "Contains the time of the last refresh try, successful or not.",
nil, nil) nil, nil)
refreshDurationDesc = prometheus.NewDesc(
refreshPrefix+"_duration_seconds",
"Contains the time it took for the last refresh to complete, even if it was unsuccessful.",
nil, nil)
cacheTimestampDesc = prometheus.NewDesc( cacheTimestampDesc = prometheus.NewDesc(
prefix+"cache_updated_time", prefix+"cache_updated_time",
@ -105,15 +109,16 @@ var (
) )
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 lastRefreshDuration time.Duration
cacheTimestamp time.Time cacheLock sync.RWMutex
cachedData *netatmo.DeviceCollection cacheTimestamp time.Time
cachedData *netatmo.DeviceCollection
} }
func (c *NetatmoCollector) Describe(dChan chan<- *prometheus.Desc) { func (c *NetatmoCollector) Describe(dChan chan<- *prometheus.Desc) {
@ -135,6 +140,7 @@ func (c *NetatmoCollector) Collect(mChan chan<- prometheus.Metric) {
} }
c.sendMetric(mChan, netatmoUpDesc, prometheus.GaugeValue, upValue) c.sendMetric(mChan, netatmoUpDesc, prometheus.GaugeValue, upValue)
c.sendMetric(mChan, refreshTimestampDesc, prometheus.GaugeValue, convertTime(c.lastRefresh)) c.sendMetric(mChan, refreshTimestampDesc, prometheus.GaugeValue, convertTime(c.lastRefresh))
c.sendMetric(mChan, refreshDurationDesc, prometheus.GaugeValue, c.lastRefreshDuration.Seconds())
c.cacheLock.RLock() c.cacheLock.RLock()
defer c.cacheLock.RUnlock() defer c.cacheLock.RUnlock()
@ -156,6 +162,10 @@ 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
defer func(start time.Time) {
c.lastRefreshDuration = time.Since(start)
}(time.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)