Refresh data on startup

This commit is contained in:
Robert Jacob 2020-06-27 18:20:51 +02:00
parent db8d1cbbf4
commit 6c3908e587
2 changed files with 9 additions and 3 deletions

View file

@ -112,6 +112,7 @@ var (
nil)
)
// NetatmoCollector is a Prometheus collector for Netatmo sensor values.
type NetatmoCollector struct {
Log logrus.FieldLogger
RefreshInterval time.Duration
@ -135,7 +136,7 @@ func (c *NetatmoCollector) Describe(dChan chan<- *prometheus.Desc) {
func (c *NetatmoCollector) Collect(mChan chan<- prometheus.Metric) {
now := time.Now()
if now.Sub(c.lastRefresh) >= c.RefreshInterval {
go c.refreshData(now)
go c.RefreshData(now)
}
upValue := 1.0
@ -163,8 +164,9 @@ func (c *NetatmoCollector) Collect(mChan chan<- prometheus.Metric) {
}
}
func (c *NetatmoCollector) refreshData(now time.Time) {
c.Log.Debugf("Refresh interval elapsed: %s > %s", now.Sub(c.lastRefresh), c.RefreshInterval)
// RefreshData causes the collector to try to refresh the cached data.
func (c *NetatmoCollector) RefreshData(now time.Time) {
c.Log.Debugf("Refreshing data. Time since last refresh: %s", now.Sub(c.lastRefresh))
c.lastRefresh = now
defer func(start time.Time) {

View file

@ -3,6 +3,7 @@ package main
import (
"net/http"
"os"
"time"
netatmo "github.com/exzz/netatmo-api-go"
"github.com/prometheus/client_golang/prometheus"
@ -45,6 +46,9 @@ func main() {
}
prometheus.MustRegister(metrics)
// Trigger first refresh
metrics.RefreshData(time.Now())
http.Handle("/metrics", promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}))
http.Handle("/version", versionHandler(log))
http.Handle("/", http.RedirectHandler("/metrics", http.StatusFound))