From 3d98195772e0dd3f8dd6f1642ab4fe954d92cc5d Mon Sep 17 00:00:00 2001 From: Robert Jacob Date: Sun, 21 Jun 2020 15:50:29 +0200 Subject: [PATCH] Make stale age configurable --- collector.go | 13 +++++-------- config.go | 27 ++++++++++++++++++++++----- config_test.go | 12 ++++++++---- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/collector.go b/collector.go index 1e675fa..f86581d 100644 --- a/collector.go +++ b/collector.go @@ -8,10 +8,6 @@ import ( "github.com/sirupsen/logrus" ) -const ( - staleDataThreshold = 30 * time.Minute -) - var ( netatmoUp = prometheus.NewGauge(prometheus.GaugeOpts{ Name: "netatmo_up", @@ -97,8 +93,9 @@ var ( ) type netatmoCollector struct { - log logrus.FieldLogger - client *netatmo.Client + log logrus.FieldLogger + staleThreshold time.Duration + client *netatmo.Client } func (c *netatmoCollector) Describe(dChan chan<- *prometheus.Desc) { @@ -140,8 +137,8 @@ func (c *netatmoCollector) collectData(ch chan<- prometheus.Metric, device *neta } date := time.Unix(*data.LastMeasure, 0) - if time.Since(date) > staleDataThreshold { - c.log.Warnf("Data is stale: %s > %s", time.Since(date), staleDataThreshold) + if time.Since(date) > c.staleThreshold { + c.log.Warnf("Data is stale: %s > %s", time.Since(date), c.staleThreshold) return } diff --git a/config.go b/config.go index 1de445d..1f9a960 100644 --- a/config.go +++ b/config.go @@ -3,6 +3,7 @@ package main import ( "errors" "fmt" + "time" netatmo "github.com/exzz/netatmo-api-go" "github.com/sirupsen/logrus" @@ -12,6 +13,7 @@ import ( const ( envVarListenAddress = "NETATMO_EXPORTER_ADDR" envVarLogLevel = "NETATMO_LOG_LEVEL" + envVarStaleDuration = "NETATMO_AGE_STALE" envVarNetatmoClientID = "NETATMO_CLIENT_ID" envVarNetatmoClientSecret = "NETATMO_CLIENT_SECRET" envVarNetatmoUsername = "NETATMO_CLIENT_USERNAME" @@ -19,16 +21,20 @@ const ( flagListenAddress = "addr" flagLogLevel = "log-level" + flagStaleDuration = "age-stale" flagNetatmoClientID = "client-id" flagNetatmoClientSecret = "client-secret" flagNetatmoUsername = "username" flagNetatmoPassword = "password" + + defaultStaleDuration = 30 * time.Minute ) var ( defaultConfig = config{ - Addr: ":9210", - LogLevel: logLevel(logrus.InfoLevel), + Addr: ":9210", + LogLevel: logLevel(logrus.InfoLevel), + StaleDuration: defaultStaleDuration, } errNoBinaryName = errors.New("need the binary name as first argument") @@ -60,9 +66,10 @@ func (l *logLevel) Set(value string) error { } type config struct { - Addr string - LogLevel logLevel - Netatmo netatmo.Config + Addr string + LogLevel logLevel + StaleDuration time.Duration + Netatmo netatmo.Config } func parseConfig(args []string, getenv func(string) string) (config, error) { @@ -75,6 +82,7 @@ func parseConfig(args []string, getenv func(string) string) (config, error) { flagSet := pflag.NewFlagSet(args[0], pflag.ExitOnError) flagSet.StringVarP(&cfg.Addr, flagListenAddress, "a", cfg.Addr, "Address to listen on.") flagSet.Var(&cfg.LogLevel, flagLogLevel, "Sets the minimum level output through logging.") + flagSet.DurationVar(&cfg.StaleDuration, flagStaleDuration, cfg.StaleDuration, "Data age to consider as stale. Stale data does not create metrics anymore.") flagSet.StringVarP(&cfg.Netatmo.ClientID, flagNetatmoClientID, "i", cfg.Netatmo.ClientID, "Client ID for NetAtmo app.") flagSet.StringVarP(&cfg.Netatmo.ClientSecret, flagNetatmoClientSecret, "s", cfg.Netatmo.ClientSecret, "Client secret for NetAtmo app.") flagSet.StringVarP(&cfg.Netatmo.Username, flagNetatmoUsername, "u", cfg.Netatmo.Username, "Username of NetAtmo account.") @@ -119,6 +127,15 @@ func applyEnvironment(cfg *config, getenv func(string) string) error { } } + if envStaleDuration := getenv(envVarStaleDuration); envStaleDuration != "" { + duration, err := time.ParseDuration(envStaleDuration) + if err != nil { + return err + } + + cfg.StaleDuration = duration + } + if envClientID := getenv(envVarNetatmoClientID); envClientID != "" { cfg.Netatmo.ClientID = envClientID } diff --git a/config_test.go b/config_test.go index b496000..1f19226 100644 --- a/config_test.go +++ b/config_test.go @@ -3,6 +3,7 @@ package main import ( "reflect" "testing" + "time" netatmo "github.com/exzz/netatmo-api-go" "github.com/sirupsen/logrus" @@ -38,8 +39,9 @@ func TestParseConfig(t *testing.T) { }, env: map[string]string{}, wantConfig: config{ - Addr: defaultConfig.Addr, - LogLevel: logLevel(logrus.InfoLevel), + Addr: defaultConfig.Addr, + LogLevel: logLevel(logrus.InfoLevel), + StaleDuration: defaultStaleDuration, Netatmo: netatmo.Config{ ClientID: "id", ClientSecret: "secret", @@ -57,14 +59,16 @@ func TestParseConfig(t *testing.T) { env: map[string]string{ envVarListenAddress: ":8080", envVarLogLevel: "debug", + envVarStaleDuration: "10m", envVarNetatmoClientID: "id", envVarNetatmoClientSecret: "secret", envVarNetatmoUsername: "username", envVarNetatmoPassword: "password", }, wantConfig: config{ - Addr: ":8080", - LogLevel: logLevel(logrus.DebugLevel), + Addr: ":8080", + LogLevel: logLevel(logrus.DebugLevel), + StaleDuration: 10 * time.Minute, Netatmo: netatmo.Config{ ClientID: "id", ClientSecret: "secret",