diff --git a/cmd/avalanche/avalanche.go b/cmd/avalanche/avalanche.go index 8ecdbb9..e0f76f2 100644 --- a/cmd/avalanche/avalanche.go +++ b/cmd/avalanche/avalanche.go @@ -41,7 +41,10 @@ func main() { kingpin.Version(version.Print("avalanche")) log.SetFlags(log.Ltime | log.Lshortfile) // Show file name and line in logs. kingpin.CommandLine.Help = "avalanche - metrics test server\n" + - "\nSeries Operation Modes:\n" + + "\n" + + "Capable of generating metrics to server on \\metrics or send via Remote Write.\n" + + "\n" + + "\nOptionally, on top of the --value-interval, --series-interval, --metric-interval logic, you can specify advanced `--series-operation-mode`:\n" + " double-halve:\n" + " Alternately doubles and halves the series count at regular intervals.\n" + " Usage: ./avalanche --series-operation-mode=double-halve --series-change-interval=30 --series-count=500\n" + diff --git a/metrics/serve.go b/metrics/serve.go index c76363e..5731918 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 on top of --value-interval, --series-interval and --metric-interval.").Default(disabledOpMode). + EnumVar(&cfg.SeriesOperationMode, disabledOpMode, gradualChangeOpMode, doubleHalveOpMode, spikeOpMode) return cfg } +type opMode = string + +const ( + disabledOpMode opMode = "disabled" + 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, disabledOpMode: + 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..39dc060 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: disabledOpMode, MinSeriesCount: 0, MaxSeriesCount: 1000, @@ -127,6 +128,7 @@ func TestRunMetrics_ValueChange_SeriesCountSame(t *testing.T) { NativeHistogramMetricCount: 10, SummaryMetricCount: 10, SummaryObjectives: 2, + SeriesOperationMode: disabledOpMode, MinSeriesCount: 0, MaxSeriesCount: 1000, @@ -200,6 +202,7 @@ func TestRunMetrics_SeriesChurn(t *testing.T) { NativeHistogramMetricCount: 10, SummaryMetricCount: 10, SummaryObjectives: 2, + SeriesOperationMode: disabledOpMode, 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())