Skip to content

Commit

Permalink
Added validation and enum for operational mode.
Browse files Browse the repository at this point in the history
Signed-off-by: bwplotka <[email protected]>
  • Loading branch information
bwplotka committed Oct 14, 2024
1 parent 835dd5f commit 35a9cb8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
5 changes: 4 additions & 1 deletion cmd/avalanche/avalanche.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
"\nBy default, Series Operation Modes:\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" +
Expand Down
39 changes: 28 additions & 11 deletions metrics/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type Config struct {
ValueInterval, SeriesInterval, MetricInterval, SeriesChangeInterval, SeriesChangeRate int

SpikeMultiplier float64
SeriesOperationMode string
SeriesOperationMode opMode
ConstLabels []string
}

Expand Down Expand Up @@ -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 --metrics-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)
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down
11 changes: 7 additions & 4 deletions metrics/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func TestRunMetrics(t *testing.T) {
NativeHistogramMetricCount: 10,
SummaryMetricCount: 10,
SummaryObjectives: 2,
SeriesOperationMode: disabledOpMode,

MinSeriesCount: 0,
MaxSeriesCount: 1000,
Expand Down Expand Up @@ -127,6 +128,7 @@ func TestRunMetrics_ValueChange_SeriesCountSame(t *testing.T) {
NativeHistogramMetricCount: 10,
SummaryMetricCount: 10,
SummaryObjectives: 2,
SeriesOperationMode: disabledOpMode,

MinSeriesCount: 0,
MaxSeriesCount: 1000,
Expand Down Expand Up @@ -200,6 +202,7 @@ func TestRunMetrics_SeriesChurn(t *testing.T) {
NativeHistogramMetricCount: 10,
SummaryMetricCount: 10,
SummaryObjectives: 2,
SeriesOperationMode: disabledOpMode,

MinSeriesCount: 0,
MaxSeriesCount: 1000,
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand Down

0 comments on commit 35a9cb8

Please sign in to comment.