Skip to content

Commit 7e5e09e

Browse files
committed
Add series gradual increase and decrease functionlity
1 parent c69af01 commit 7e5e09e

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

cmd/avalanche.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var (
4141
labelInterval = kingpin.Flag("series-interval", "Change series_id label values every {interval} seconds.").Default("60").Int()
4242
metricInterval = kingpin.Flag("metric-interval", "Change __name__ label values every {interval} seconds.").Default("120").Int()
4343
seriesChangeInterval = kingpin.Flag("series-change-interval", "Change the number of series every {interval} seconds.").Default("10").Int()
44-
operationMode = kingpin.Flag("operation-mode", "Mode of operation: 'gradual-change', 'series-spike', 'double-halve'").Default("gradual-change").String()
44+
operationMode = kingpin.Flag("operation-mode", "Mode of operation: 'gradual-change', 'series-spike', 'double-halve'").Default("default").String()
4545
port = kingpin.Flag("port", "Port to serve at").Default("9001").Int()
4646
remoteURL = kingpin.Flag("remote-url", "URL to send samples via remote_write API.").URL()
4747
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()

metrics/serve.go

+22
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,28 @@ func RunMetrics(metricCount, labelCount, seriesCount, seriesChangeRate, metricLe
184184
}
185185
}
186186
}()
187+
188+
case "gradual-change":
189+
go func() {
190+
for tick := range changeSeriesTick.C {
191+
fmt.Printf("%v: Adjusting series count. New count: %d\n", tick, seriesCount)
192+
metricsMux.Lock()
193+
seriesCount += seriesChangeRate
194+
if seriesCount < 1 {
195+
seriesCount = 1
196+
}
197+
unregisterMetrics()
198+
registerMetrics(metricCount, metricLength, metricCycle, labelKeys)
199+
cycleValues(labelKeys, labelValues, seriesCount, seriesCycle)
200+
metricsMux.Unlock()
201+
202+
select {
203+
case updateNotify <- struct{}{}:
204+
default:
205+
}
206+
}
207+
}()
208+
default:
187209
}
188210

189211
go func() {

metrics/serve_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,65 @@ func TestRunMetricsSeriesCountChangeDoubleHalve(t *testing.T) {
7575
t.Fatal("Did not receive update notification for series count halving in time")
7676
}
7777
}
78+
79+
func TestRunMetricsGradualChange(t *testing.T) {
80+
const (
81+
initialSeriesCount = 30
82+
metricCount = 1
83+
labelCount = 1
84+
seriesChangeRate = -10
85+
metricLength = 1
86+
labelLength = 1
87+
valueInterval = 1
88+
seriesInterval = 1
89+
metricInterval = 1
90+
seriesChangeInterval = 3
91+
operationMode = "gradual-change"
92+
constLabel = "constLabel=test"
93+
updateNotifyTimeout = 4 * time.Second
94+
waitTimeBetweenChecks = 3 * time.Second
95+
)
96+
97+
stop := make(chan struct{})
98+
defer close(stop)
99+
100+
promRegistry = prometheus.NewRegistry()
101+
102+
updateNotify, err := RunMetrics(metricCount, labelCount, initialSeriesCount, seriesChangeRate, metricLength, labelLength, valueInterval, seriesInterval, metricInterval, seriesChangeInterval, operationMode, []string{constLabel}, stop)
103+
assert.NoError(t, err)
104+
105+
initialCount := countSeries(t, promRegistry)
106+
expectedInitialCount := initialSeriesCount
107+
assert.Equal(t, expectedInitialCount, initialCount, "Initial series count should be %d but got %d", expectedInitialCount, initialCount)
108+
109+
select {
110+
case <-updateNotify:
111+
time.Sleep(waitTimeBetweenChecks)
112+
updatedCount := countSeries(t, promRegistry)
113+
expectedCount := initialSeriesCount + seriesChangeRate
114+
assert.Equal(t, expectedCount, updatedCount, "1 Decreased series count should be %d but got %d", expectedCount, updatedCount)
115+
case <-time.After(updateNotifyTimeout):
116+
t.Fatal("Did not receive update notification for series count doubling in time")
117+
}
118+
119+
select {
120+
case <-updateNotify:
121+
time.Sleep(waitTimeBetweenChecks)
122+
updatedCount := countSeries(t, promRegistry)
123+
expectedCount := initialSeriesCount + seriesChangeRate*2
124+
assert.Equal(t, expectedCount, updatedCount, "2 Decreased series count should be %d but got %d", expectedCount, updatedCount)
125+
case <-time.After(updateNotifyTimeout):
126+
t.Fatal("Did not receive update notification for series count doubling in time")
127+
}
128+
129+
// Test for the minimum value of the series is 1
130+
select {
131+
case <-updateNotify:
132+
time.Sleep(waitTimeBetweenChecks)
133+
updatedCount := countSeries(t, promRegistry)
134+
expectedCount := 1
135+
assert.Equal(t, expectedCount, updatedCount, "3 Decreased series count should be %d but got %d", expectedCount, updatedCount)
136+
case <-time.After(updateNotifyTimeout):
137+
t.Fatal("Did not receive update notification for series count doubling in time")
138+
}
139+
}

0 commit comments

Comments
 (0)