forked from thanos-io/thanos
/
roundtrip_test.go
816 lines (741 loc) · 22.1 KB
/
roundtrip_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package queryfrontend
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"sync"
"testing"
"time"
cortexcache "github.com/cortexproject/cortex/pkg/chunk/cache"
"github.com/cortexproject/cortex/pkg/cortexpb"
"github.com/cortexproject/cortex/pkg/querier/queryrange"
cortexvalidation "github.com/cortexproject/cortex/pkg/util/validation"
"github.com/go-kit/log"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/promql/parser"
"github.com/weaveworks/common/user"
"github.com/thanos-io/thanos/pkg/store/labelpb"
"github.com/thanos-io/thanos/pkg/testutil"
)
const (
seconds = 1e3 // 1e3 milliseconds per second.
hour = 3600 * seconds
day = 24 * time.Hour
)
var defaultLimits = &cortexvalidation.Limits{
MaxQueryLength: model.Duration(7 * 24 * time.Hour),
MaxQueryParallelism: 14,
MaxCacheFreshness: model.Duration(time.Minute),
}
// fakeRoundTripper implements the RoundTripper interface.
type fakeRoundTripper struct {
*httptest.Server
host string
}
func newFakeRoundTripper() (*fakeRoundTripper, error) {
s := httptest.NewServer(nil)
u, err := url.Parse(s.URL)
if err != nil {
return nil, err
}
return &fakeRoundTripper{
Server: s,
host: u.Host,
}, nil
}
// setHandler is used for mocking.
func (r *fakeRoundTripper) setHandler(h http.Handler) {
r.Config.Handler = h
}
func (r *fakeRoundTripper) RoundTrip(h *http.Request) (*http.Response, error) {
h.URL.Scheme = "http"
h.URL.Host = r.host
return http.DefaultTransport.RoundTrip(h)
}
// TestRoundTripRetryMiddleware tests the retry middleware.
func TestRoundTripRetryMiddleware(t *testing.T) {
testRequest := &ThanosQueryRangeRequest{
Path: "/api/v1/query_range",
Start: 0,
End: 2 * hour,
Step: 10 * seconds,
}
testLabelsRequest := &ThanosLabelsRequest{Path: "/api/v1/labels", Start: 0, End: 2 * hour}
testSeriesRequest := &ThanosSeriesRequest{
Path: "/api/v1/series",
Start: 0,
End: 2 * hour,
Matchers: [][]*labels.Matcher{{labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")}},
}
queryRangeCodec := NewThanosQueryRangeCodec(true)
labelsCodec := NewThanosLabelsCodec(true, 2*time.Hour)
for _, tc := range []struct {
name string
maxRetries int
req queryrange.Request
codec queryrange.Codec
handlerFunc func(fail bool) (*int, http.Handler)
fail bool
expected int
}{
{
name: "not query range, retry won't be triggered 1.",
maxRetries: 100,
req: &ThanosQueryRangeRequest{
Path: "/api/v1/query",
Start: 0,
End: 2 * hour,
Step: 10 * seconds,
},
codec: queryRangeCodec,
handlerFunc: promqlResults,
expected: 1,
},
{
name: "no retry, get counter value 1",
maxRetries: 0,
req: testRequest,
codec: queryRangeCodec,
handlerFunc: promqlResults,
fail: true,
expected: 1,
},
{
name: "retry set to 1",
maxRetries: 1,
req: testRequest,
codec: queryRangeCodec,
handlerFunc: promqlResults,
fail: true,
expected: 1,
},
{
name: "retry set to 3",
maxRetries: 3,
fail: true,
req: testRequest,
codec: queryRangeCodec,
handlerFunc: promqlResults,
expected: 3,
},
{
name: "labels requests: no retry, get counter value 1",
maxRetries: 0,
req: testLabelsRequest,
codec: labelsCodec,
handlerFunc: labelsResults,
fail: true,
expected: 1,
},
{
name: "labels requests: retry set to 1",
maxRetries: 1,
req: testLabelsRequest,
codec: labelsCodec,
handlerFunc: labelsResults,
fail: true,
expected: 1,
},
{
name: "labels requests: retry set to 3",
maxRetries: 3,
fail: true,
req: testLabelsRequest,
codec: labelsCodec,
handlerFunc: labelsResults,
expected: 3,
},
{
name: "series requests: no retry, get counter value 1",
maxRetries: 0,
req: testSeriesRequest,
codec: labelsCodec,
handlerFunc: seriesResults,
fail: true,
expected: 1,
},
{
name: "series requests: retry set to 1",
maxRetries: 1,
req: testSeriesRequest,
codec: labelsCodec,
handlerFunc: seriesResults,
fail: true,
expected: 1,
},
{
name: "series requests: retry set to 3",
maxRetries: 3,
fail: true,
req: testSeriesRequest,
codec: labelsCodec,
handlerFunc: seriesResults,
expected: 3,
},
} {
t.Run(tc.name, func(t *testing.T) {
tpw, err := NewTripperware(
Config{
QueryRangeConfig: QueryRangeConfig{
MaxRetries: tc.maxRetries,
Limits: defaultLimits,
SplitQueriesByInterval: day,
},
LabelsConfig: LabelsConfig{
MaxRetries: tc.maxRetries,
Limits: defaultLimits,
SplitQueriesByInterval: day,
},
}, nil, log.NewNopLogger(),
)
testutil.Ok(t, err)
rt, err := newFakeRoundTripper()
testutil.Ok(t, err)
res, handler := tc.handlerFunc(tc.fail)
rt.setHandler(handler)
ctx := user.InjectOrgID(context.Background(), "1")
httpReq, err := tc.codec.EncodeRequest(ctx, tc.req)
testutil.Ok(t, err)
_, err = tpw(rt).RoundTrip(httpReq)
testutil.Equals(t, tc.fail, err != nil)
testutil.Equals(t, tc.expected, *res)
})
}
}
// TestRoundTripSplitIntervalMiddleware tests the split interval middleware.
func TestRoundTripSplitIntervalMiddleware(t *testing.T) {
testRequest := &ThanosQueryRangeRequest{
Path: "/api/v1/query_range",
Start: 0,
End: 2 * hour,
Step: 10 * seconds,
}
testLabelsRequest := &ThanosLabelsRequest{
Path: "/api/v1/labels",
Start: 0,
End: 2 * hour,
}
testSeriesRequest := &ThanosSeriesRequest{
Path: "/api/v1/series",
Start: 0,
End: 2 * hour,
Matchers: [][]*labels.Matcher{{labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")}},
}
queryRangeCodec := NewThanosQueryRangeCodec(true)
labelsCodec := NewThanosLabelsCodec(true, 2*time.Hour)
for _, tc := range []struct {
name string
splitInterval time.Duration
req queryrange.Request
codec queryrange.Codec
handlerFunc func(bool) (*int, http.Handler)
expected int
}{
{
name: "non query range request won't be split 1",
req: &ThanosQueryRangeRequest{
Path: "/api/v1/query",
Start: 0,
End: 2 * hour,
Step: 10 * seconds,
},
handlerFunc: promqlResults,
codec: queryRangeCodec,
splitInterval: time.Hour,
expected: 1,
},
{
name: "split interval == 0, disable split",
req: testRequest,
handlerFunc: promqlResults,
codec: queryRangeCodec,
splitInterval: 0,
expected: 1,
},
{
name: "won't be split. Interval == time range",
req: testRequest,
handlerFunc: promqlResults,
codec: queryRangeCodec,
splitInterval: 2 * time.Hour,
expected: 1,
},
{
name: "won't be split. Interval > time range",
req: testRequest,
handlerFunc: promqlResults,
codec: queryRangeCodec,
splitInterval: day,
expected: 1,
},
{
name: "split to 2 requests",
req: testRequest,
handlerFunc: promqlResults,
codec: queryRangeCodec,
splitInterval: 1 * time.Hour,
expected: 2,
},
{
name: "labels request won't be split",
req: testLabelsRequest,
handlerFunc: labelsResults,
codec: labelsCodec,
splitInterval: day,
expected: 1,
},
{
name: "labels request split to 2",
req: testLabelsRequest,
handlerFunc: labelsResults,
codec: labelsCodec,
splitInterval: 1 * time.Hour,
expected: 2,
},
{
name: "series request won't be split",
req: testSeriesRequest,
handlerFunc: seriesResults,
codec: labelsCodec,
splitInterval: day,
expected: 1,
},
{
name: "series request split to 2",
req: testSeriesRequest,
handlerFunc: seriesResults,
codec: labelsCodec,
splitInterval: 1 * time.Hour,
expected: 2,
},
} {
t.Run(tc.name, func(t *testing.T) {
tpw, err := NewTripperware(
Config{
QueryRangeConfig: QueryRangeConfig{
Limits: defaultLimits,
SplitQueriesByInterval: tc.splitInterval,
},
LabelsConfig: LabelsConfig{
Limits: defaultLimits,
SplitQueriesByInterval: tc.splitInterval,
},
}, nil, log.NewNopLogger(),
)
testutil.Ok(t, err)
rt, err := newFakeRoundTripper()
testutil.Ok(t, err)
defer rt.Close()
res, handler := tc.handlerFunc(false)
rt.setHandler(handler)
ctx := user.InjectOrgID(context.Background(), "1")
httpReq, err := tc.codec.EncodeRequest(ctx, tc.req)
testutil.Ok(t, err)
_, err = tpw(rt).RoundTrip(httpReq)
testutil.Ok(t, err)
testutil.Equals(t, tc.expected, *res)
})
}
}
// TestRoundTripQueryRangeCacheMiddleware tests the cache middleware.
func TestRoundTripQueryRangeCacheMiddleware(t *testing.T) {
testRequest := &ThanosQueryRangeRequest{
Path: "/api/v1/query_range",
Start: 0,
End: 2 * hour,
Step: 10 * seconds,
MaxSourceResolution: 1 * seconds,
Dedup: true, // Deduplication is enabled by default.
}
testRequestWithoutDedup := &ThanosQueryRangeRequest{
Path: "/api/v1/query_range",
Start: 0,
End: 2 * hour,
Step: 10 * seconds,
MaxSourceResolution: 1 * seconds,
Dedup: false,
}
// Non query range request, won't be cached.
testRequestInstant := &ThanosQueryRangeRequest{
Path: "/api/v1/query",
Start: 0,
End: 2 * hour,
Step: 10 * seconds,
Dedup: true,
}
// Same query params as testRequest, different maxSourceResolution
// but still in the same downsampling level, so it will be cached in this case.
testRequestSameLevelDownsampling := &ThanosQueryRangeRequest{
Path: "/api/v1/query_range",
Start: 0,
End: 2 * hour,
Step: 10 * seconds,
MaxSourceResolution: 10 * seconds,
Dedup: true,
}
// Same query params as testRequest, different maxSourceResolution
// and downsampling level so it won't be cached in this case.
testRequestHigherLevelDownsampling := &ThanosQueryRangeRequest{
Path: "/api/v1/query_range",
Start: 0,
End: 2 * hour,
Step: 10 * seconds,
MaxSourceResolution: 1 * hour,
Dedup: true,
}
// Same query params as testRequest, but with storeMatchers
testRequestWithStoreMatchers := &ThanosQueryRangeRequest{
Path: "/api/v1/query_range",
Start: 0,
End: 2 * hour,
Step: 10 * seconds,
MaxSourceResolution: 1 * seconds,
StoreMatchers: [][]*labels.Matcher{{labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")}},
Dedup: true,
}
cacheConf := &queryrange.ResultsCacheConfig{
CacheConfig: cortexcache.Config{
EnableFifoCache: true,
Fifocache: cortexcache.FifoCacheConfig{
MaxSizeBytes: "1MiB",
MaxSizeItems: 1000,
Validity: time.Hour,
},
},
}
tpw, err := NewTripperware(
Config{
QueryRangeConfig: QueryRangeConfig{
Limits: defaultLimits,
ResultsCacheConfig: cacheConf,
SplitQueriesByInterval: day,
},
}, nil, log.NewNopLogger(),
)
testutil.Ok(t, err)
rt, err := newFakeRoundTripper()
testutil.Ok(t, err)
defer rt.Close()
res, handler := promqlResults(false)
rt.setHandler(handler)
for _, tc := range []struct {
name string
req queryrange.Request
handlerAndResult func() (*int, http.Handler)
expected int
}{
{name: "first request", req: testRequest, expected: 1},
{name: "same request as the first one, directly use cache", req: testRequest, expected: 1},
{name: "same request as the first one but with dedup disabled, should not use cache", req: testRequestWithoutDedup, expected: 2},
{name: "non query range request won't be cached", req: testRequestInstant, expected: 3},
{name: "do it again", req: testRequestInstant, expected: 4},
{name: "different max source resolution but still same level", req: testRequestSameLevelDownsampling, expected: 4},
{name: "different max source resolution and different level", req: testRequestHigherLevelDownsampling, expected: 5},
{name: "storeMatchers requests won't go to cache", req: testRequestWithStoreMatchers, expected: 6},
{
name: "request but will be partitioned",
req: &ThanosQueryRangeRequest{
Path: "/api/v1/query_range",
Start: 0,
End: 25 * hour,
Step: 10 * seconds,
Dedup: true,
},
expected: 8,
},
{
name: "same query as the previous one",
req: &ThanosQueryRangeRequest{
Path: "/api/v1/query_range",
Start: 0,
End: 25 * hour,
Step: 10 * seconds,
Dedup: true,
},
expected: 8,
},
} {
if !t.Run(tc.name, func(t *testing.T) {
ctx := user.InjectOrgID(context.Background(), "1")
httpReq, err := NewThanosQueryRangeCodec(true).EncodeRequest(ctx, tc.req)
testutil.Ok(t, err)
_, err = tpw(rt).RoundTrip(httpReq)
testutil.Ok(t, err)
testutil.Equals(t, tc.expected, *res)
}) {
break
}
}
}
// TestRoundTripLabelsCacheMiddleware tests the cache middleware for labels requests.
func TestRoundTripLabelsCacheMiddleware(t *testing.T) {
testRequest := &ThanosLabelsRequest{
Path: "/api/v1/labels",
Start: 0,
End: 2 * hour,
}
// Same query params as testRequest, but with Matchers
testRequestWithMatchers := &ThanosLabelsRequest{
Path: "/api/v1/labels",
Start: 0,
End: 2 * hour,
Matchers: [][]*labels.Matcher{{labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")}},
}
// Same query params as testRequest, but with storeMatchers
testRequestWithStoreMatchers := &ThanosLabelsRequest{
Path: "/api/v1/labels",
Start: 0,
End: 2 * hour,
StoreMatchers: [][]*labels.Matcher{{labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")}},
}
testLabelValuesRequestFoo := &ThanosLabelsRequest{
Path: "/api/v1/label/foo/values",
Start: 0,
End: 2 * hour,
Label: "foo",
}
testLabelValuesRequestFooWithMatchers := &ThanosLabelsRequest{
Path: "/api/v1/label/foo/values",
Start: 0,
End: 2 * hour,
Label: "foo",
Matchers: [][]*labels.Matcher{{labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")}},
}
testLabelValuesRequestBar := &ThanosLabelsRequest{
Path: "/api/v1/label/bar/values",
Start: 0,
End: 2 * hour,
Label: "bar",
}
cacheConf := &queryrange.ResultsCacheConfig{
CacheConfig: cortexcache.Config{
EnableFifoCache: true,
Fifocache: cortexcache.FifoCacheConfig{
MaxSizeBytes: "1MiB",
MaxSizeItems: 1000,
Validity: time.Hour,
},
},
}
tpw, err := NewTripperware(
Config{
LabelsConfig: LabelsConfig{
Limits: defaultLimits,
ResultsCacheConfig: cacheConf,
SplitQueriesByInterval: day,
},
}, nil, log.NewNopLogger(),
)
testutil.Ok(t, err)
rt, err := newFakeRoundTripper()
testutil.Ok(t, err)
defer rt.Close()
res, handler := labelsResults(false)
rt.setHandler(handler)
for _, tc := range []struct {
name string
req queryrange.Request
handlerAndResult func() (*int, http.Handler)
expected int
}{
{name: "first request", req: testRequest, expected: 1},
{name: "same request as the first one, directly use cache", req: testRequest, expected: 1},
{name: "matchers requests won't go to cache", req: testRequestWithMatchers, expected: 2},
{name: "same matchers requests, use cache", req: testRequestWithMatchers, expected: 2},
{name: "storeMatchers requests won't go to cache", req: testRequestWithStoreMatchers, expected: 3},
{name: "label values request label name foo", req: testLabelValuesRequestFoo, expected: 4},
{name: "same label values query, use cache", req: testLabelValuesRequestFoo, expected: 4},
{name: "label values query with matchers, won't go to cache", req: testLabelValuesRequestFooWithMatchers, expected: 5},
{name: "same label values query with matchers, use cache", req: testLabelValuesRequestFooWithMatchers, expected: 5},
{name: "label values request different label", req: testLabelValuesRequestBar, expected: 6},
{
name: "request but will be partitioned",
req: &ThanosLabelsRequest{
Path: "/api/v1/labels",
Start: 0,
End: 25 * hour,
},
expected: 8,
},
{
name: "same query as the previous one",
req: &ThanosLabelsRequest{
Path: "/api/v1/labels",
Start: 0,
End: 25 * hour,
},
expected: 8,
},
} {
if !t.Run(tc.name, func(t *testing.T) {
ctx := user.InjectOrgID(context.Background(), "1")
httpReq, err := NewThanosLabelsCodec(true, 24*time.Hour).EncodeRequest(ctx, tc.req)
testutil.Ok(t, err)
_, err = tpw(rt).RoundTrip(httpReq)
testutil.Ok(t, err)
testutil.Equals(t, tc.expected, *res)
}) {
break
}
}
}
// TestRoundTripSeriesCacheMiddleware tests the cache middleware for series requests.
func TestRoundTripSeriesCacheMiddleware(t *testing.T) {
testRequest := &ThanosSeriesRequest{
Path: "/api/v1/series",
Start: 0,
End: 2 * hour,
Matchers: [][]*labels.Matcher{{labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")}},
Dedup: true,
}
testRequestWithoutDedup := &ThanosSeriesRequest{
Path: "/api/v1/series",
Start: 0,
End: 2 * hour,
Matchers: [][]*labels.Matcher{{labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")}},
Dedup: false,
}
// Different matchers set with the first request.
testRequest2 := &ThanosSeriesRequest{
Path: "/api/v1/series",
Start: 0,
End: 2 * hour,
Matchers: [][]*labels.Matcher{{labels.MustNewMatcher(labels.MatchEqual, "foo", "baz")}},
Dedup: true,
}
// Same query params as testRequest, but with storeMatchers
testRequestWithStoreMatchers := &ThanosSeriesRequest{
Path: "/api/v1/series",
Start: 0,
End: 2 * hour,
StoreMatchers: [][]*labels.Matcher{{labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")}},
}
cacheConf := &queryrange.ResultsCacheConfig{
CacheConfig: cortexcache.Config{
EnableFifoCache: true,
Fifocache: cortexcache.FifoCacheConfig{
MaxSizeBytes: "1MiB",
MaxSizeItems: 1000,
Validity: time.Hour,
},
},
}
tpw, err := NewTripperware(
Config{
LabelsConfig: LabelsConfig{
Limits: defaultLimits,
ResultsCacheConfig: cacheConf,
SplitQueriesByInterval: day,
},
}, nil, log.NewNopLogger(),
)
testutil.Ok(t, err)
rt, err := newFakeRoundTripper()
testutil.Ok(t, err)
defer rt.Close()
res, handler := seriesResults(false)
rt.setHandler(handler)
for _, tc := range []struct {
name string
req queryrange.Request
handlerAndResult func() (*int, http.Handler)
expected int
}{
{name: "first request", req: testRequest, expected: 1},
{name: "same request as the first one, directly use cache", req: testRequest, expected: 1},
{name: "same request as the first one but with dedup disabled, should not use cache", req: testRequestWithoutDedup, expected: 2},
{name: "different series request, not use cache", req: testRequest2, expected: 3},
{name: "storeMatchers requests won't go to cache", req: testRequestWithStoreMatchers, expected: 4},
} {
if !t.Run(tc.name, func(t *testing.T) {
ctx := user.InjectOrgID(context.Background(), "1")
httpReq, err := NewThanosLabelsCodec(true, 24*time.Hour).EncodeRequest(ctx, tc.req)
testutil.Ok(t, err)
_, err = tpw(rt).RoundTrip(httpReq)
testutil.Ok(t, err)
testutil.Equals(t, tc.expected, *res)
}) {
break
}
}
}
// promqlResults is a mock handler used to test split and cache middleware.
// Modified from Loki https://github.com/grafana/loki/blob/master/pkg/querier/queryrange/roundtrip_test.go#L547.
func promqlResults(fail bool) (*int, http.Handler) {
count := 0
var lock sync.Mutex
q := queryrange.PrometheusResponse{
Status: "success",
Data: queryrange.PrometheusData{
ResultType: string(parser.ValueTypeMatrix),
Result: []queryrange.SampleStream{
{
Labels: []cortexpb.LabelAdapter{},
Samples: []cortexpb.Sample{
{Value: 0, TimestampMs: 0},
{Value: 1, TimestampMs: 1},
},
},
},
},
}
return &count, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lock.Lock()
defer lock.Unlock()
// Set fail in the response code to test retry.
if fail {
w.WriteHeader(500)
}
if err := json.NewEncoder(w).Encode(q); err != nil {
panic(err)
}
count++
})
}
//labelsResults is a mock handler used to test split and cache middleware for label names and label values requests.
func labelsResults(fail bool) (*int, http.Handler) {
count := 0
var lock sync.Mutex
q := ThanosLabelsResponse{
Status: "success",
Data: []string{"__name__", "job"},
}
return &count, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lock.Lock()
defer lock.Unlock()
// Set fail in the response code to test retry.
if fail {
w.WriteHeader(500)
}
if err := json.NewEncoder(w).Encode(q); err != nil {
panic(err)
}
count++
})
}
// seriesResults is a mock handler used to test split and cache middleware for series requests.
func seriesResults(fail bool) (*int, http.Handler) {
count := 0
var lock sync.Mutex
q := ThanosSeriesResponse{
Status: "success",
Data: []labelpb.ZLabelSet{{Labels: []labelpb.ZLabel{{Name: "__name__", Value: "up"}, {Name: "foo", Value: "bar"}}}},
}
return &count, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lock.Lock()
defer lock.Unlock()
// Set fail in the response code to test retry.
if fail {
w.WriteHeader(500)
}
if err := json.NewEncoder(w).Encode(q); err != nil {
panic(err)
}
count++
})
}