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 {
|
if cfg.Validate() == nil {
|
||||||
t.Fatal("expected error validating")
|
t.Fatal("expected error validating")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestApplyEnvVars(t *testing.T) {
|
func TestApplyEnvVars(t *testing.T) {
|
||||||
|
|
|
@ -15,6 +15,7 @@ const envConfigKey = "cluster_pubsubmon"
|
||||||
// Default values for this Config.
|
// Default values for this Config.
|
||||||
const (
|
const (
|
||||||
DefaultCheckInterval = 15 * time.Second
|
DefaultCheckInterval = 15 * time.Second
|
||||||
|
DefaultFailureThreshold = 3.0
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config allows to initialize a Monitor and customize some parameters.
|
// Config allows to initialize a Monitor and customize some parameters.
|
||||||
|
@ -22,10 +23,12 @@ type Config struct {
|
||||||
config.Saver
|
config.Saver
|
||||||
|
|
||||||
CheckInterval time.Duration
|
CheckInterval time.Duration
|
||||||
|
FailureThreshold float64
|
||||||
}
|
}
|
||||||
|
|
||||||
type jsonConfig struct {
|
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.
|
// 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.
|
// Default sets the fields of this Config to sensible values.
|
||||||
func (cfg *Config) Default() error {
|
func (cfg *Config) Default() error {
|
||||||
cfg.CheckInterval = DefaultCheckInterval
|
cfg.CheckInterval = DefaultCheckInterval
|
||||||
|
cfg.FailureThreshold = DefaultFailureThreshold
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,6 +62,11 @@ func (cfg *Config) Validate() error {
|
||||||
if cfg.CheckInterval <= 0 {
|
if cfg.CheckInterval <= 0 {
|
||||||
return errors.New("pubsubmon.check_interval too low")
|
return errors.New("pubsubmon.check_interval too low")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cfg.FailureThreshold <= 0 {
|
||||||
|
return errors.New("basic.failure_threshold too low")
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,6 +88,7 @@ func (cfg *Config) LoadJSON(raw []byte) error {
|
||||||
func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
|
func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
|
||||||
interval, _ := time.ParseDuration(jcfg.CheckInterval)
|
interval, _ := time.ParseDuration(jcfg.CheckInterval)
|
||||||
cfg.CheckInterval = interval
|
cfg.CheckInterval = interval
|
||||||
|
cfg.FailureThreshold = jcfg.FailureThreshold
|
||||||
|
|
||||||
return cfg.Validate()
|
return cfg.Validate()
|
||||||
}
|
}
|
||||||
|
@ -93,5 +103,6 @@ func (cfg *Config) ToJSON() ([]byte, error) {
|
||||||
func (cfg *Config) toJSONConfig() *jsonConfig {
|
func (cfg *Config) toJSONConfig() *jsonConfig {
|
||||||
return &jsonConfig{
|
return &jsonConfig{
|
||||||
CheckInterval: cfg.CheckInterval.String(),
|
CheckInterval: cfg.CheckInterval.String(),
|
||||||
|
FailureThreshold: cfg.FailureThreshold,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,8 @@ import (
|
||||||
|
|
||||||
var cfgJSON = []byte(`
|
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.CheckInterval = 0
|
||||||
|
cfg.FailureThreshold = -0.1
|
||||||
if cfg.Validate() == nil {
|
if cfg.Validate() == nil {
|
||||||
t.Fatal("expected error validating")
|
t.Fatal("expected error validating")
|
||||||
}
|
}
|
||||||
|
@ -60,10 +62,14 @@ func TestDefault(t *testing.T) {
|
||||||
|
|
||||||
func TestApplyEnvVars(t *testing.T) {
|
func TestApplyEnvVars(t *testing.T) {
|
||||||
os.Setenv("CLUSTER_PUBSUBMON_CHECKINTERVAL", "22s")
|
os.Setenv("CLUSTER_PUBSUBMON_CHECKINTERVAL", "22s")
|
||||||
|
os.Setenv("CLUSTER_PUBSUBMON_FAILURETHRESHOLD", "4.0")
|
||||||
cfg := &Config{}
|
cfg := &Config{}
|
||||||
cfg.ApplyEnvVars()
|
cfg.ApplyEnvVars()
|
||||||
|
|
||||||
if cfg.CheckInterval != 22*time.Second {
|
if cfg.CheckInterval != 22*time.Second {
|
||||||
t.Fatal("failed to override check_interval with env var")
|
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())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
mtrs := metrics.NewStore()
|
mtrs := metrics.NewStore()
|
||||||
checker := metrics.NewChecker(mtrs)
|
checker := metrics.NewChecker(mtrs, cfg.FailureThreshold)
|
||||||
|
|
||||||
subscription, err := psub.Subscribe(PubsubTopic)
|
subscription, err := psub.Subscribe(PubsubTopic)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -70,6 +70,7 @@ func (cfg *Config) Validate() error {
|
||||||
if cfg.ConcurrentPins <= 0 {
|
if cfg.ConcurrentPins <= 0 {
|
||||||
return errors.New("maptracker.concurrent_pins is too low")
|
return errors.New("maptracker.concurrent_pins is too low")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user