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 5bc0599 commit 0a54998
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
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").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)
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, defaultOpMode:
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: defaultOpMode,

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: defaultOpMode,

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: defaultOpMode,

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 0a54998

Please sign in to comment.