Skip to content

Commit bfd90fb

Browse files
jskswamyraghav1674
andcommitted
Add support to specify metric name for stress
This adds feature to stress command to get the metric name from the user and does the stress on only these metrics, if metric name is not specified then it goes back to old behaviour of fetching all the metric name from thanos store Co-authored-by: Raghav Gupta <[email protected]> Signed-off-by: Krishnaswamy Subramanian <[email protected]>
1 parent aac3b58 commit bfd90fb

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

cmd/thanosbench/stress.go

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,34 @@ var (
2626
maxTime = timestamp.FromTime(time.Unix(math.MaxInt64/1000-62135596801, 999999999))
2727
)
2828

29+
func getMetricsFromStore(ctx context.Context, timeout *time.Duration, c storepb.StoreClient) ([]string, error) {
30+
lblvlsCtx, lblvlsCancel := context.WithTimeout(ctx, *timeout)
31+
defer lblvlsCancel()
32+
33+
labelvaluesResp, err := c.LabelValues(lblvlsCtx, &storepb.LabelValuesRequest{
34+
Label: labels.MetricName,
35+
Start: minTime,
36+
End: maxTime,
37+
})
38+
if err != nil {
39+
return nil, err
40+
}
41+
if len(labelvaluesResp.Warnings) > 0 {
42+
return nil, errors.New(fmt.Sprintf("got %#v warnings from LabelValues() call", labelvaluesResp.Warnings))
43+
}
44+
labelvalues := labelvaluesResp.Values
45+
if len(labelvalues) == 0 {
46+
return nil, errors.New("the StoreAPI responded with zero metric names")
47+
}
48+
return labelvalues, nil
49+
}
50+
2951
func registerStress(m map[string]setupFunc, app *kingpin.Application) {
3052
cmd := app.Command("stress", "Stress tests a remote StoreAPI.")
3153
workers := cmd.Flag("workers", "Number of go routines for stress testing.").Required().Int()
3254
timeout := cmd.Flag("timeout", "Timeout of each operation").Default("60s").Duration()
3355
lookback := cmd.Flag("query.look-back", "How much time into the past at max we should look back").Default("300h").Duration()
56+
userSpecifiedMetrics := cmd.Flag("metric-name", "Metric to query for").Strings()
3457
target := cmd.Arg("target", "IP:PORT pair of the target to stress.").Required().TCP()
3558

3659
// TODO(GiedriusS): send other requests like Info() as well.
@@ -43,28 +66,19 @@ func registerStress(m map[string]setupFunc, app *kingpin.Application) {
4366
return err
4467
}
4568
defer conn.Close()
46-
c := storepb.NewStoreClient(conn)
4769

48-
lblvlsCtx, lblvlsCancel := context.WithTimeout(mainCtx, *timeout)
49-
defer lblvlsCancel()
70+
c := storepb.NewStoreClient(conn)
71+
errg, errCtx := errgroup.WithContext(mainCtx)
5072

51-
labelvaluesResp, err := c.LabelValues(lblvlsCtx, &storepb.LabelValuesRequest{
52-
Label: labels.MetricName,
53-
Start: minTime,
54-
End: maxTime,
55-
})
56-
if err != nil {
57-
return err
58-
}
59-
if len(labelvaluesResp.Warnings) > 0 {
60-
return errors.New(fmt.Sprintf("got %#v warnings from LabelValues() call", labelvaluesResp.Warnings))
73+
var metrics []string
74+
if *userSpecifiedMetrics != nil && len(*userSpecifiedMetrics) != 0 {
75+
metrics = *userSpecifiedMetrics
76+
} else {
77+
metrics, err = getMetricsFromStore(mainCtx, timeout, c)
78+
if err != nil {
79+
return err
80+
}
6181
}
62-
labelvalues := labelvaluesResp.Values
63-
if len(labelvalues) == 0 {
64-
return errors.New("the StoreAPI responded with zero metric names")
65-
}
66-
67-
errg, errCtx := errgroup.WithContext(mainCtx)
6882

6983
for i := 0; i < *workers; i++ {
7084
errg.Go(func() error {
@@ -78,7 +92,7 @@ func registerStress(m map[string]setupFunc, app *kingpin.Application) {
7892
opCtx, cancel := context.WithTimeout(errCtx, *timeout)
7993
defer cancel()
8094

81-
randomMetric := labelvalues[rand.Intn(len(labelvalues))]
95+
randomMetric := metrics[rand.Intn(len(metrics))]
8296
max := time.Now().Unix()
8397
min := time.Now().Unix() - rand.Int63n(int64(lookback.Seconds()))
8498

0 commit comments

Comments
 (0)