diff --git a/informer/disk/config.go b/informer/disk/config.go index 961cf068..e7cec8b2 100644 --- a/informer/disk/config.go +++ b/informer/disk/config.go @@ -14,19 +14,33 @@ const envConfigKey = "cluster_disk" // Default values for disk Config const ( - DefaultMetricTTL = 30 * time.Second + DefaultMetricTTL = 30 * time.Second + DefaultMetricType = MetricFreeSpace ) +// String returns a string representation for MetricType. +func (t MetricType) String() string { + switch t { + case MetricFreeSpace: + return "freespace" + case MetricRepoSize: + return "reposize" + } + return "" +} + // Config is used to initialize an Informer and customize // the type and parameters of the metric it produces. type Config struct { config.Saver - MetricTTL time.Duration + MetricTTL time.Duration + MetricType MetricType } type jsonConfig struct { - MetricTTL string `json:"metric_ttl"` + MetricTTL string `json:"metric_ttl"` + MetricType string `json:"metric_type"` } // ConfigKey returns a human-friendly identifier for this type of Metric. @@ -37,6 +51,7 @@ func (cfg *Config) ConfigKey() string { // Default initializes this Config with sensible values. func (cfg *Config) Default() error { cfg.MetricTTL = DefaultMetricTTL + cfg.MetricType = DefaultMetricType return nil } @@ -60,6 +75,9 @@ func (cfg *Config) Validate() error { return errors.New("disk.metric_ttl is invalid") } + if cfg.MetricType.String() == "" { + return errors.New("disk.metric_type is invalid") + } return nil } @@ -82,6 +100,15 @@ func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error { t, _ := time.ParseDuration(jcfg.MetricTTL) cfg.MetricTTL = t + switch jcfg.MetricType { + case "reposize": + cfg.MetricType = MetricRepoSize + case "freespace": + cfg.MetricType = MetricFreeSpace + default: + return errors.New("disk.metric_type is invalid") + } + return cfg.Validate() } @@ -96,7 +123,8 @@ func (cfg *Config) ToJSON() (raw []byte, err error) { func (cfg *Config) toJSONConfig() *jsonConfig { return &jsonConfig{ - MetricTTL: cfg.MetricTTL.String(), + MetricTTL: cfg.MetricTTL.String(), + MetricType: cfg.MetricType.String(), } } diff --git a/informer/disk/config_test.go b/informer/disk/config_test.go index 4d3adeed..acfc2116 100644 --- a/informer/disk/config_test.go +++ b/informer/disk/config_test.go @@ -30,6 +30,25 @@ func TestLoadJSON(t *testing.T) { if err == nil { t.Error("expected error decoding metric_ttl") } + + j = &jsonConfig{} + json.Unmarshal(cfgJSON, j) + j.MetricType = "abc" + tst, _ = json.Marshal(j) + err = cfg.LoadJSON(tst) + if err == nil { + t.Error("expected error decoding check_interval") + } + + j = &jsonConfig{} + json.Unmarshal(cfgJSON, j) + j.MetricType = "reposize" + tst, _ = json.Marshal(j) + err = cfg.LoadJSON(tst) + if err != nil { + t.Error("reposize should be a valid type") + } + } func TestToJSON(t *testing.T) { @@ -57,6 +76,12 @@ func TestDefault(t *testing.T) { if cfg.Validate() == nil { t.Fatal("expected error validating") } + + cfg.Default() + cfg.MetricType = MetricRepoSize + if cfg.Validate() != nil { + t.Fatal("MetricRepoSize is a valid type") + } } func TestApplyEnvVars(t *testing.T) { diff --git a/informer/disk/disk.go b/informer/disk/disk.go index 804fb518..f3ff237b 100644 --- a/informer/disk/disk.go +++ b/informer/disk/disk.go @@ -50,7 +50,7 @@ func NewInformer(cfg *Config) (*Informer, error) { // Name returns the user-facing name of this informer. func (disk *Informer) Name() string { - return "reposize" + return disk.config.MetricType.String() } // SetClient provides us with an rpc.Client which allows @@ -108,12 +108,17 @@ func (disk *Informer) GetMetric(ctx context.Context) *api.Metric { logger.Error(err) valid = false } else { - size := repoStat.RepoSize - total := repoStat.StorageMax - if size < total { - metric = total - size - } else { // Make sure we don't underflow - metric = 0 + switch disk.config.MetricType { + case MetricFreeSpace: + size := repoStat.RepoSize + total := repoStat.StorageMax + if size < total { + metric = total - size + } else { // Make sure we don't underflow + metric = 0 + } + case MetricRepoSize: + metric = repoStat.RepoSize } } diff --git a/informer/disk/disk_test.go b/informer/disk/disk_test.go index 2b393c0b..a4c4dbcc 100644 --- a/informer/disk/disk_test.go +++ b/informer/disk/disk_test.go @@ -52,6 +52,7 @@ func TestFreeSpace(t *testing.T) { ctx := context.Background() cfg := &Config{} cfg.Default() + cfg.MetricType = MetricFreeSpace inf, err := NewInformer(cfg) if err != nil { @@ -73,6 +74,32 @@ func TestFreeSpace(t *testing.T) { } } +func TestRepoSize(t *testing.T) { + ctx := context.Background() + cfg := &Config{} + cfg.Default() + cfg.MetricType = MetricRepoSize + + inf, err := NewInformer(cfg) + if err != nil { + t.Fatal(err) + } + defer inf.Shutdown(ctx) + m := inf.GetMetric(ctx) + if m.Valid { + t.Error("metric should be invalid") + } + inf.SetClient(test.NewMockRPCClient(t)) + m = inf.GetMetric(ctx) + if !m.Valid { + t.Error("metric should be valid") + } + // The mock client reports 100KB and 2 pins of 1 KB + if m.Value != "2000" { + t.Error("bad metric value") + } +} + func TestWithErrors(t *testing.T) { ctx := context.Background() cfg := &Config{}