mirror of
https://github.com/steinhobelgruen/netatmo-exporter.git
synced 2024-11-21 17:03:56 +00:00
Refresh data on startup
This commit is contained in:
parent
db8d1cbbf4
commit
6c3908e587
|
@ -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) {
|
||||
|
|
4
main.go
4
main.go
|
@ -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))
|
||||
|
|
Loading…
Reference in a new issue