Add metric for refresh duration

This commit is contained in:
Robert Jacob 2020-06-27 17:59:17 +02:00
parent 1eda093550
commit 73f4c697da

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",
@ -111,6 +115,7 @@ type NetatmoCollector struct {
Client *netatmo.Client Client *netatmo.Client
lastRefresh time.Time lastRefresh time.Time
lastRefreshError error lastRefreshError error
lastRefreshDuration time.Duration
cacheLock sync.RWMutex cacheLock sync.RWMutex
cacheTimestamp time.Time cacheTimestamp time.Time
cachedData *netatmo.DeviceCollection cachedData *netatmo.DeviceCollection
@ -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)