Add failure_threshold monitors config
License: MIT Signed-off-by: Adrian Lanzafame <adrianlanzafame92@gmail.com>
This commit is contained in:
parent
3d6eb64db6
commit
c4b76619c1
|
@ -56,6 +56,7 @@ func TestDefault(t *testing.T) {
|
|||
if cfg.Validate() == nil {
|
||||
t.Fatal("expected error validating")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestApplyEnvVars(t *testing.T) {
|
||||
|
|
|
@ -14,18 +14,21 @@ const envConfigKey = "cluster_pubsubmon"
|
|||
|
||||
// Default values for this Config.
|
||||
const (
|
||||
DefaultCheckInterval = 15 * time.Second
|
||||
DefaultCheckInterval = 15 * time.Second
|
||||
DefaultFailureThreshold = 3.0
|
||||
)
|
||||
|
||||
// Config allows to initialize a Monitor and customize some parameters.
|
||||
type Config struct {
|
||||
config.Saver
|
||||
|
||||
CheckInterval time.Duration
|
||||
CheckInterval time.Duration
|
||||
FailureThreshold float64
|
||||
}
|
||||
|
||||
type jsonConfig struct {
|
||||
CheckInterval string `json:"check_interval"`
|
||||
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()
|
||||
}
|
||||
|
@ -92,6 +102,7 @@ func (cfg *Config) ToJSON() ([]byte, error) {
|
|||
|
||||
func (cfg *Config) toJSONConfig() *jsonConfig {
|
||||
return &jsonConfig{
|
||||
CheckInterval: cfg.CheckInterval.String(),
|
||||
CheckInterval: cfg.CheckInterval.String(),
|
||||
FailureThreshold: cfg.FailureThreshold,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -70,6 +70,7 @@ func (cfg *Config) Validate() error {
|
|||
if cfg.ConcurrentPins <= 0 {
|
||||
return errors.New("maptracker.concurrent_pins is too low")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user