Skip to content

Commit

Permalink
add details in help
Browse files Browse the repository at this point in the history
∙ fix InvalidSeriesCount logic, add test
  • Loading branch information
yomek33 committed Jul 17, 2024
1 parent cc3c990 commit 8acfd37
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
19 changes: 17 additions & 2 deletions cmd/avalanche.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ var (
seriesCount = kingpin.Flag("series-count", "Number of series per-metric.").Default("10").Int()
seriesChangeRate = kingpin.Flag("series-change-rate", "The rate at which the number of active series changes over time. Applies to 'gradual-change' modes.").Default("10").Int()
maxSeriesCount = kingpin.Flag("max-series-count", "Maximum number of series to serve. Applies to 'gradual-change' modes.").Default("10000").Int()
minSeriesCount = kingpin.Flag("min-series-count", "Minimum number of series to serve. Applies to 'gradual-change' modes.").Default("").Int()
minSeriesCount = kingpin.Flag("min-series-count", "Minimum number of series to serve. Applies to 'gradual-change' modes.").Default("0").Int()
metricLength = kingpin.Flag("metricname-length", "Modify length of metric names.").Default("5").Int()
labelLength = kingpin.Flag("labelname-length", "Modify length of label names.").Default("5").Int()
constLabels = kingpin.Flag("const-label", "Constant label to add to every metric. Format is labelName=labelValue. Flag can be specified multiple times.").Strings()
valueInterval = kingpin.Flag("value-interval", "Change series values every {interval} seconds.").Default("30").Int()
labelInterval = kingpin.Flag("series-interval", "Change series_id label values every {interval} seconds.").Default("60").Int()
metricInterval = kingpin.Flag("metric-interval", "Change __name__ label values every {interval} seconds.").Default("120").Int()
seriesChangeInterval = kingpin.Flag("series-change-interval", "Change the number of series every {interval} seconds.").Default("10").Int()
seriesOperationMode = kingpin.Flag("series-operation-mode", "Mode of operation: 'gradual-change', 'spike', 'double-halve'").Default("default").String()
seriesOperationMode = kingpin.Flag("series-operation-mode", "Mode of operation: 'gradual-change', 'double-halve'").Default("default").String()
port = kingpin.Flag("port", "Port to serve at").Default("9001").Int()
remoteURL = kingpin.Flag("remote-url", "URL to send samples via remote_write API.").URL()
remotePprofURLs = kingpin.Flag("remote-pprof-urls", "a list of urls to download pprofs during the remote write: --remote-pprof-urls=http://127.0.0.1:10902/debug/pprof/heap --remote-pprof-urls=http://127.0.0.1:10902/debug/pprof/profile").URLList()
Expand All @@ -61,6 +61,21 @@ 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"
kingpin.CommandLine.Help = "avalanche - metrics test server\n" +
"\nSeries Operation Modes:\n" +
" double-halve:\n" +
" Alternately doubles and halves the series count at regular intervals.\n" +
" Usage: ./avalanche --operation-mode=double-halve --series-change-interval=30 --series-count=20\n" +
" Description: This mode alternately doubles and halves the series count at regular intervals.\n" +
" The series count is doubled on one tick and halved on the next, ensuring it never drops below 1.\n" +
"\n" +
" gradual-change:\n" +
" Gradually changes the series count by a fixed rate at regular intervals.\n" +
" Usage: ./avalanche --operation-mode=gradual-change --series-change-interval=30 --series-change-rate=10 --series-count=20\n" +
" Description: This mode gradually increases the series count by seriesChangeRate on each tick up to maxSeriesCount,\n" +
" then decreases it back to the starting value, and repeats this cycle indefinitely.\n" +
" The series count is incremented by seriesChangeRate on each tick, ensuring it never drops below 1."

kingpin.Parse()
if *maxSeriesCount <= *minSeriesCount {
fmt.Fprintf(os.Stderr, "Error: --max-series-count (%d) must be greater than --min-series-count (%d)\n", *maxSeriesCount, *minSeriesCount)
Expand Down
5 changes: 3 additions & 2 deletions metrics/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ func RunMetrics(metricCount, labelCount, seriesCount, seriesChangeRate, maxSerie

case "gradual-change":
if minSeriesCount >= maxSeriesCount {
fmt.Printf("Error: minSeriesCount must be less than maxSeriesCount, got %d and %d\n", minSeriesCount, maxSeriesCount)
close(stop)
return nil, fmt.Errorf("error: minSeriesCount must be less than maxSeriesCount, got %d and %d", minSeriesCount, maxSeriesCount)
}
seriesIncrease := true
currentSeriesCount = minSeriesCount
Expand Down Expand Up @@ -183,6 +182,8 @@ func RunMetrics(metricCount, labelCount, seriesCount, seriesChangeRate, maxSerie
}()
default:
currentSeriesCount = seriesCount
registerMetrics(metricCount, metricLength, metricCycle, labelKeys)
cycleValues(labelKeys, labelValues, seriesCount, seriesCycle)
go func() {
for tick := range metricTick.C {
metricsMux.Lock()
Expand Down
39 changes: 36 additions & 3 deletions metrics/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,21 @@ func TestRunMetricsGradualChange(t *testing.T) {
assert.NoError(t, err)

time.Sleep(2 * time.Second)
initialCount := countSeries(t, promRegistry)
expectedInitialCount := minSeriesCount
assert.Equal(t, expectedInitialCount, initialCount, "Initial series count should be minSeriesCount %d but got %d", expectedInitialCount, initialCount)
currentCount := countSeries(t, promRegistry)
expectedInitialCount := currentCount
assert.Equal(t, expectedInitialCount, currentCount, "Initial series count should be minSeriesCount %d but got %d", expectedInitialCount, currentCount)

assert.Eventually(t, func() bool {
graduallyIncreasedCount := countSeries(t, promRegistry)
fmt.Println("seriesCount: ", graduallyIncreasedCount)
if graduallyIncreasedCount > maxSeriesCount {
t.Fatalf("Gradually increased series count should be less than maxSeriesCount %d but got %d", maxSeriesCount, graduallyIncreasedCount)
}
if currentCount > graduallyIncreasedCount {
t.Fatalf("Gradually increased series count should be greater than initial series count %d but got %d", currentCount, graduallyIncreasedCount)
} else {
currentCount = graduallyIncreasedCount
}

return graduallyIncreasedCount == maxSeriesCount
}, 15*time.Second, seriesChangeInterval*time.Second, "Did not receive update notification for series count gradual increase in time")
Expand All @@ -114,3 +119,31 @@ func TestRunMetricsGradualChange(t *testing.T) {
return graduallyIncreasedCount == minSeriesCount
}, 15*time.Second, seriesChangeInterval*time.Second, "Did not receive update notification for series count gradual increase in time")
}

// if min is bigger than maxSeriesCount, fail in GradualChange
func TestRunMetricsWithInvalidSeriesCounts(t *testing.T) {
const (
metricCount = 1
labelCount = 1
seriesCount = 100
maxSeriesCount = 10
minSeriesCount = 100
seriesChangeRate = 10
metricLength = 1
labelLength = 1
valueInterval = 100
seriesInterval = 100
metricInterval = 100
seriesChangeInterval = 3
operationMode = "gradual-change"
constLabel = "constLabel=test"
)

stop := make(chan struct{})
defer close(stop)

promRegistry = prometheus.NewRegistry()

_, err := RunMetrics(metricCount, labelCount, seriesCount, seriesChangeRate, maxSeriesCount, minSeriesCount, metricLength, labelLength, valueInterval, seriesInterval, metricInterval, seriesChangeInterval, operationMode, []string{constLabel}, stop)
assert.Error(t, err)
}

0 comments on commit 8acfd37

Please sign in to comment.