Make stale age configurable

This commit is contained in:
Robert Jacob 2020-06-21 15:50:29 +02:00
parent adb91d1992
commit 3d98195772
3 changed files with 35 additions and 17 deletions

View file

@ -8,10 +8,6 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
const (
staleDataThreshold = 30 * time.Minute
)
var ( var (
netatmoUp = prometheus.NewGauge(prometheus.GaugeOpts{ netatmoUp = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "netatmo_up", Name: "netatmo_up",
@ -98,6 +94,7 @@ var (
type netatmoCollector struct { type netatmoCollector struct {
log logrus.FieldLogger log logrus.FieldLogger
staleThreshold time.Duration
client *netatmo.Client client *netatmo.Client
} }
@ -140,8 +137,8 @@ func (c *netatmoCollector) collectData(ch chan<- prometheus.Metric, device *neta
} }
date := time.Unix(*data.LastMeasure, 0) date := time.Unix(*data.LastMeasure, 0)
if time.Since(date) > staleDataThreshold { if time.Since(date) > c.staleThreshold {
c.log.Warnf("Data is stale: %s > %s", time.Since(date), staleDataThreshold) c.log.Warnf("Data is stale: %s > %s", time.Since(date), c.staleThreshold)
return return
} }

View file

@ -3,6 +3,7 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"time"
netatmo "github.com/exzz/netatmo-api-go" netatmo "github.com/exzz/netatmo-api-go"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -12,6 +13,7 @@ import (
const ( const (
envVarListenAddress = "NETATMO_EXPORTER_ADDR" envVarListenAddress = "NETATMO_EXPORTER_ADDR"
envVarLogLevel = "NETATMO_LOG_LEVEL" envVarLogLevel = "NETATMO_LOG_LEVEL"
envVarStaleDuration = "NETATMO_AGE_STALE"
envVarNetatmoClientID = "NETATMO_CLIENT_ID" envVarNetatmoClientID = "NETATMO_CLIENT_ID"
envVarNetatmoClientSecret = "NETATMO_CLIENT_SECRET" envVarNetatmoClientSecret = "NETATMO_CLIENT_SECRET"
envVarNetatmoUsername = "NETATMO_CLIENT_USERNAME" envVarNetatmoUsername = "NETATMO_CLIENT_USERNAME"
@ -19,16 +21,20 @@ const (
flagListenAddress = "addr" flagListenAddress = "addr"
flagLogLevel = "log-level" flagLogLevel = "log-level"
flagStaleDuration = "age-stale"
flagNetatmoClientID = "client-id" flagNetatmoClientID = "client-id"
flagNetatmoClientSecret = "client-secret" flagNetatmoClientSecret = "client-secret"
flagNetatmoUsername = "username" flagNetatmoUsername = "username"
flagNetatmoPassword = "password" flagNetatmoPassword = "password"
defaultStaleDuration = 30 * time.Minute
) )
var ( var (
defaultConfig = config{ defaultConfig = config{
Addr: ":9210", Addr: ":9210",
LogLevel: logLevel(logrus.InfoLevel), LogLevel: logLevel(logrus.InfoLevel),
StaleDuration: defaultStaleDuration,
} }
errNoBinaryName = errors.New("need the binary name as first argument") errNoBinaryName = errors.New("need the binary name as first argument")
@ -62,6 +68,7 @@ func (l *logLevel) Set(value string) error {
type config struct { type config struct {
Addr string Addr string
LogLevel logLevel LogLevel logLevel
StaleDuration time.Duration
Netatmo netatmo.Config Netatmo netatmo.Config
} }
@ -75,6 +82,7 @@ func parseConfig(args []string, getenv func(string) string) (config, error) {
flagSet := pflag.NewFlagSet(args[0], pflag.ExitOnError) flagSet := pflag.NewFlagSet(args[0], pflag.ExitOnError)
flagSet.StringVarP(&cfg.Addr, flagListenAddress, "a", cfg.Addr, "Address to listen on.") 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.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.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.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.") 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 != "" { if envClientID := getenv(envVarNetatmoClientID); envClientID != "" {
cfg.Netatmo.ClientID = envClientID cfg.Netatmo.ClientID = envClientID
} }

View file

@ -3,6 +3,7 @@ package main
import ( import (
"reflect" "reflect"
"testing" "testing"
"time"
netatmo "github.com/exzz/netatmo-api-go" netatmo "github.com/exzz/netatmo-api-go"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -40,6 +41,7 @@ func TestParseConfig(t *testing.T) {
wantConfig: config{ wantConfig: config{
Addr: defaultConfig.Addr, Addr: defaultConfig.Addr,
LogLevel: logLevel(logrus.InfoLevel), LogLevel: logLevel(logrus.InfoLevel),
StaleDuration: defaultStaleDuration,
Netatmo: netatmo.Config{ Netatmo: netatmo.Config{
ClientID: "id", ClientID: "id",
ClientSecret: "secret", ClientSecret: "secret",
@ -57,6 +59,7 @@ func TestParseConfig(t *testing.T) {
env: map[string]string{ env: map[string]string{
envVarListenAddress: ":8080", envVarListenAddress: ":8080",
envVarLogLevel: "debug", envVarLogLevel: "debug",
envVarStaleDuration: "10m",
envVarNetatmoClientID: "id", envVarNetatmoClientID: "id",
envVarNetatmoClientSecret: "secret", envVarNetatmoClientSecret: "secret",
envVarNetatmoUsername: "username", envVarNetatmoUsername: "username",
@ -65,6 +68,7 @@ func TestParseConfig(t *testing.T) {
wantConfig: config{ wantConfig: config{
Addr: ":8080", Addr: ":8080",
LogLevel: logLevel(logrus.DebugLevel), LogLevel: logLevel(logrus.DebugLevel),
StaleDuration: 10 * time.Minute,
Netatmo: netatmo.Config{ Netatmo: netatmo.Config{
ClientID: "id", ClientID: "id",
ClientSecret: "secret", ClientSecret: "secret",