Add failure_threshold monitors config

License: MIT
Signed-off-by: Adrian Lanzafame <adrianlanzafame92@gmail.com>
This commit is contained in:
Adrian Lanzafame 2019-03-12 13:00:51 +10:00
parent 3d6eb64db6
commit c4b76619c1
No known key found for this signature in database
GPG Key ID: 87E40C5D62EAE192
5 changed files with 25 additions and 6 deletions

View File

@ -56,6 +56,7 @@ func TestDefault(t *testing.T) {
if cfg.Validate() == nil {
t.Fatal("expected error validating")
}
}
func TestApplyEnvVars(t *testing.T) {

View File

@ -15,6 +15,7 @@ const envConfigKey = "cluster_pubsubmon"
// Default values for this Config.
const (
DefaultCheckInterval = 15 * time.Second
DefaultFailureThreshold = 3.0
)
// Config allows to initialize a Monitor and customize some parameters.
@ -22,10 +23,12 @@ type Config struct {
config.Saver
CheckInterval time.Duration
FailureThreshold float64
}
type jsonConfig struct {
CheckInterval string `json:"check_interval"`
FailureThreshold float64 `json:"failure_threshold"`
}
// ConfigKey provides a human-friendly identifier for this type of Config.
@ -36,6 +39,7 @@ func (cfg *Config) ConfigKey() string {
// Default sets the fields of this Config to sensible values.
func (cfg *Config) Default() error {
cfg.CheckInterval = DefaultCheckInterval
cfg.FailureThreshold = DefaultFailureThreshold
return nil
}
@ -58,6 +62,11 @@ func (cfg *Config) Validate() error {
if cfg.CheckInterval <= 0 {
return errors.New("pubsubmon.check_interval too low")
}
if cfg.FailureThreshold <= 0 {
return errors.New("basic.failure_threshold too low")
}
return nil
}
@ -79,6 +88,7 @@ func (cfg *Config) LoadJSON(raw []byte) error {
func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
interval, _ := time.ParseDuration(jcfg.CheckInterval)
cfg.CheckInterval = interval
cfg.FailureThreshold = jcfg.FailureThreshold
return cfg.Validate()
}
@ -93,5 +103,6 @@ func (cfg *Config) ToJSON() ([]byte, error) {
func (cfg *Config) toJSONConfig() *jsonConfig {
return &jsonConfig{
CheckInterval: cfg.CheckInterval.String(),
FailureThreshold: cfg.FailureThreshold,
}
}

View File

@ -9,7 +9,8 @@ import (
var cfgJSON = []byte(`
{
"check_interval": "15s"
"check_interval": "15s",
"failure_threshold": 3.0
}
`)
@ -53,6 +54,7 @@ func TestDefault(t *testing.T) {
}
cfg.CheckInterval = 0
cfg.FailureThreshold = -0.1
if cfg.Validate() == nil {
t.Fatal("expected error validating")
}
@ -60,10 +62,14 @@ func TestDefault(t *testing.T) {
func TestApplyEnvVars(t *testing.T) {
os.Setenv("CLUSTER_PUBSUBMON_CHECKINTERVAL", "22s")
os.Setenv("CLUSTER_PUBSUBMON_FAILURETHRESHOLD", "4.0")
cfg := &Config{}
cfg.ApplyEnvVars()
if cfg.CheckInterval != 22*time.Second {
t.Fatal("failed to override check_interval with env var")
}
if cfg.FailureThreshold != 4.0 {
t.Fatal("failed to override failure_threshold with env var")
}
}

View File

@ -68,7 +68,7 @@ func New(
ctx, cancel := context.WithCancel(context.Background())
mtrs := metrics.NewStore()
checker := metrics.NewChecker(mtrs)
checker := metrics.NewChecker(mtrs, cfg.FailureThreshold)
subscription, err := psub.Subscribe(PubsubTopic)
if err != nil {

View File

@ -70,6 +70,7 @@ func (cfg *Config) Validate() error {
if cfg.ConcurrentPins <= 0 {
return errors.New("maptracker.concurrent_pins is too low")
}
return nil
}