From 0a54998e4762d0c613524eb3af5b84dcd1829732 Mon Sep 17 00:00:00 2001 From: bwplotka Date: Tue, 8 Oct 2024 09:52:05 +0100 Subject: [PATCH] Added validation and enum for operational mode. Signed-off-by: bwplotka --- metrics/serve.go | 39 ++++++++++++++++++++++++++++----------- metrics/serve_test.go | 11 +++++++---- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/metrics/serve.go b/metrics/serve.go index e8a4c2d..58dc589 100644 --- a/metrics/serve.go +++ b/metrics/serve.go @@ -91,7 +91,7 @@ type Config struct { ValueInterval, SeriesInterval, MetricInterval, SeriesChangeInterval, SeriesChangeRate int SpikeMultiplier float64 - SeriesOperationMode string + SeriesOperationMode opMode ConstLabels []string } @@ -143,11 +143,20 @@ func NewConfigFromFlags(flagReg func(name, help string) *kingpin.FlagClause) *Co flagReg("series-change-rate", "The rate at which the number of active series changes over time. Applies to 'gradual-change' mode.").Default("100"). IntVar(&cfg.SeriesChangeRate) - flagReg("series-operation-mode", "Mode of operation: 'gradual-change', 'double-halve', 'spike'").Default("default"). - StringVar(&cfg.SeriesOperationMode) + flagReg("series-operation-mode", "Mode of operation, so optional advanced behaviours").Default(defaultOpMode). + EnumVar(&cfg.SeriesOperationMode, defaultOpMode, gradualChangeOpMode, doubleHalveOpMode, spikeOpMode) return cfg } +type opMode = string + +const ( + defaultOpMode opMode = "default" + gradualChangeOpMode opMode = "gradual-change" + doubleHalveOpMode opMode = "double-halve" + spikeOpMode opMode = "spike" +) + func (c Config) Validate() error { if c.MaxSeriesCount <= c.MinSeriesCount { return fmt.Errorf("--max-series-count (%d) must be greater than --min-series-count (%d)", c.MaxSeriesCount, c.MinSeriesCount) @@ -164,11 +173,19 @@ func (c Config) Validate() error { return fmt.Errorf("constant label argument must have format labelName=labelValue but got %s", cLabel) } } - if c.SeriesOperationMode == "gradual-change" && c.SeriesChangeRate <= 0 { - return fmt.Errorf("--series-change-rate must be greater than 0, got %d", c.SeriesChangeRate) - } - if c.SeriesOperationMode == "spike" && c.SpikeMultiplier < 1 { - return fmt.Errorf("--spike-multiplier must be greater than or equal to 1, got %f", c.SpikeMultiplier) + + switch c.SeriesOperationMode { + case gradualChangeOpMode: + if c.SeriesChangeRate <= 0 { + return fmt.Errorf("--series-change-rate must be greater than 0, got %d", c.SeriesChangeRate) + } + case spikeOpMode: + if c.SpikeMultiplier < 1 { + return fmt.Errorf("--spike-multiplier must be greater than or equal to 1, got %f", c.SpikeMultiplier) + } + case doubleHalveOpMode, defaultOpMode: + default: + return fmt.Errorf("unknown --series-operation-mode %v", c.SeriesOperationMode) } return nil } @@ -511,18 +528,18 @@ func (c *Collector) Run() error { c.recreateMetrics(unsafeReadOnlyGetState) switch c.cfg.SeriesOperationMode { - case "double-halve": + case doubleHalveOpMode: fmt.Printf("Starting double-halve mode; starting series: %d, change series interval: %d seconds\n", c.cfg.SeriesCount, c.cfg.SeriesChangeInterval) go c.handleDoubleHalveMode(&mutableState.seriesCount, unsafeReadOnlyGetState) - case "gradual-change": + case gradualChangeOpMode: fmt.Printf("Starting gradual-change mode; min series: %d, max series: %d, series change rate: %d, change series interval: %d seconds\n", c.cfg.MinSeriesCount, c.cfg.MaxSeriesCount, c.cfg.SeriesChangeRate, c.cfg.SeriesChangeInterval) c.mu.Lock() mutableState.seriesCount = c.cfg.MinSeriesCount c.mu.Unlock() go c.handleGradualChangeMode(&mutableState.seriesCount, unsafeReadOnlyGetState) - case "spike": + case spikeOpMode: fmt.Printf("Starting spike mode; initial series: %d, spike multiplier: %f, spike interval: %v\n", c.cfg.SeriesCount, c.cfg.SpikeMultiplier, c.cfg.SeriesChangeInterval) go c.handleSpikeMode(&mutableState.seriesCount, unsafeReadOnlyGetState, c.cfg.SpikeMultiplier) } diff --git a/metrics/serve_test.go b/metrics/serve_test.go index d789578..29f97fa 100644 --- a/metrics/serve_test.go +++ b/metrics/serve_test.go @@ -88,6 +88,7 @@ func TestRunMetrics(t *testing.T) { NativeHistogramMetricCount: 10, SummaryMetricCount: 10, SummaryObjectives: 2, + SeriesOperationMode: defaultOpMode, MinSeriesCount: 0, MaxSeriesCount: 1000, @@ -127,6 +128,7 @@ func TestRunMetrics_ValueChange_SeriesCountSame(t *testing.T) { NativeHistogramMetricCount: 10, SummaryMetricCount: 10, SummaryObjectives: 2, + SeriesOperationMode: defaultOpMode, MinSeriesCount: 0, MaxSeriesCount: 1000, @@ -200,6 +202,7 @@ func TestRunMetrics_SeriesChurn(t *testing.T) { NativeHistogramMetricCount: 10, SummaryMetricCount: 10, SummaryObjectives: 2, + SeriesOperationMode: defaultOpMode, MinSeriesCount: 0, MaxSeriesCount: 1000, @@ -258,7 +261,7 @@ func TestRunMetricsSeriesCountChangeDoubleHalve(t *testing.T) { SeriesInterval: 100, MetricInterval: 100, SeriesChangeInterval: 3, - SeriesOperationMode: "double-halve", + SeriesOperationMode: doubleHalveOpMode, ConstLabels: []string{"constLabel=test"}, } assert.NoError(t, testCfg.Validate()) @@ -302,7 +305,7 @@ func TestRunMetricsGradualChange(t *testing.T) { SeriesInterval: 100, MetricInterval: 100, SeriesChangeInterval: 3, - SeriesOperationMode: "gradual-change", + SeriesOperationMode: gradualChangeOpMode, ConstLabels: []string{"constLabel=test"}, } assert.NoError(t, testCfg.Validate()) @@ -362,7 +365,7 @@ func TestRunMetricsWithInvalidSeriesCounts(t *testing.T) { SeriesInterval: 100, MetricInterval: 100, SeriesChangeInterval: 3, - SeriesOperationMode: "gradual-change", + SeriesOperationMode: gradualChangeOpMode, ConstLabels: []string{"constLabel=test"}, } assert.Error(t, testCfg.Validate()) @@ -383,7 +386,7 @@ func TestRunMetricsSpikeChange(t *testing.T) { SeriesInterval: 100, MetricInterval: 100, SeriesChangeInterval: 10, - SeriesOperationMode: "spike", + SeriesOperationMode: spikeOpMode, ConstLabels: []string{"constLabel=test"}, } assert.NoError(t, testCfg.Validate())