-
Notifications
You must be signed in to change notification settings - Fork 54
/
engine_test.go
5753 lines (5492 loc) · 179 KB
/
engine_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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) The Thanos Community Authors.
// Licensed under the Apache License 2.0.
package engine_test
import (
"bytes"
"context"
"fmt"
"io"
"math"
"os"
"reflect"
"runtime"
"runtime/pprof"
"sort"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/efficientgo/core/errors"
"github.com/efficientgo/core/testutil"
"github.com/go-kit/log"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
"golang.org/x/exp/maps"
"github.com/thanos-io/promql-engine/engine"
"github.com/thanos-io/promql-engine/execution/model"
"github.com/thanos-io/promql-engine/execution/warnings"
"github.com/thanos-io/promql-engine/logicalplan"
"github.com/thanos-io/promql-engine/query"
"github.com/thanos-io/promql-engine/storage/prometheus"
"github.com/prometheus/prometheus/model/histogram"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/timestamp"
"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/promql/parser"
"github.com/prometheus/prometheus/promql/promqltest"
"github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/tsdb/chunkenc"
"github.com/prometheus/prometheus/tsdb/tsdbutil"
"github.com/prometheus/prometheus/util/annotations"
"github.com/prometheus/prometheus/util/stats"
"github.com/prometheus/prometheus/util/teststorage"
)
func TestMain(m *testing.M) {
parser.EnableExperimentalFunctions = true
goleak.VerifyTestMain(m)
}
func TestPromqlAcceptance(t *testing.T) {
// promql acceptance tests disable experimental functions again
// since we use them in our tests too we need to enable them afterwards again
t.Cleanup(func() { parser.EnableExperimentalFunctions = true })
engine := engine.New(engine.Opts{
EngineOpts: promql.EngineOpts{
EnableAtModifier: true,
EnableNegativeOffset: true,
MaxSamples: 5e10,
Timeout: 1 * time.Hour,
NoStepSubqueryIntervalFn: func(rangeMillis int64) int64 { return 30 * time.Second.Milliseconds() },
}})
promqltest.RunBuiltinTests(t, engine)
}
func TestVectorSelectorWithGaps(t *testing.T) {
t.Parallel()
opts := promql.EngineOpts{
Timeout: 1 * time.Hour,
MaxSamples: 1e10,
EnableNegativeOffset: true,
EnableAtModifier: true,
}
series := storage.MockSeries(
[]int64{240, 270, 300, 600, 630, 660},
[]float64{1, 2, 3, 4, 5, 6},
[]string{labels.MetricName, "foo"},
)
query := "foo"
start := time.Unix(0, 0)
end := time.Unix(1000, 0)
ctx := context.Background()
newEngine := engine.New(engine.Opts{EngineOpts: opts})
q1, err := newEngine.NewRangeQuery(ctx, storageWithSeries(series), nil, query, start, end, 30*time.Second)
testutil.Ok(t, err)
defer q1.Close()
newResult := q1.Exec(ctx)
testutil.Ok(t, newResult.Err)
oldEngine := promql.NewEngine(opts)
q2, err := oldEngine.NewRangeQuery(ctx, storageWithSeries(series), nil, query, start, end, 30*time.Second)
testutil.Ok(t, err)
defer q2.Close()
oldResult := q2.Exec(context.Background())
testutil.Ok(t, oldResult.Err)
testutil.WithGoCmp(comparer).Equals(t, oldResult, newResult, queryExplanation(q1))
}
type queryableCloseChecker struct {
closed bool
storage.Queryable
}
func (q *queryableCloseChecker) Querier(mint, maxt int64) (storage.Querier, error) {
qr, err := q.Queryable.Querier(mint, maxt)
if err != nil {
return nil, err
}
return &querierCloseChecker{Querier: qr, closed: &q.closed}, nil
}
type querierCloseChecker struct {
storage.Querier
closed *bool
}
func (q *querierCloseChecker) Close() error {
*q.closed = true
return q.Querier.Close()
}
// TestQuerierClosedAfterQueryClosed tests that the querier is only closed
// after the query is closed.
func TestQuerierClosedAfterQueryClosed(t *testing.T) {
t.Parallel()
opts := promql.EngineOpts{
Timeout: 1 * time.Hour,
MaxSamples: 1e10,
EnableNegativeOffset: true,
EnableAtModifier: true,
}
load := `load 30s
http_requests_total{pod="nginx-1", route="/"} 41.00+0.20x40
http_requests_total{pod="nginx-2", route="/"} 51+21.71x40`
storage := promqltest.LoadedStorage(t, load)
defer storage.Close()
optimizers := logicalplan.AllOptimizers
newEngine := engine.New(engine.Opts{
EngineOpts: opts,
DisableFallback: true,
LogicalOptimizers: optimizers,
// Set to 1 to make sure batching is tested.
SelectorBatchSize: 1,
})
ctx := context.Background()
qr := &queryableCloseChecker{
Queryable: storage,
}
q1, err := newEngine.NewInstantQuery(ctx, qr, nil, "sum(http_requests_total)", time.Unix(0, 0))
testutil.Ok(t, err)
_ = q1.Exec(ctx)
require.Equal(t, false, qr.closed)
q1.Close()
require.Equal(t, true, qr.closed)
}
func TestQueriesAgainstOldEngine(t *testing.T) {
t.Parallel()
start := time.Unix(0, 0)
end := time.Unix(1800, 0)
step := time.Second * 30
// Negative offset and at modifier are enabled by default
// since Prometheus v2.33.0 so we also enable them.
opts := promql.EngineOpts{
Timeout: 1 * time.Hour,
MaxSamples: 1e10,
EnableNegativeOffset: true,
EnableAtModifier: true,
}
cases := []struct {
load string
name string
query string
start time.Time
end time.Time
step time.Duration
}{
{
name: "fuzz",
load: `load 30s
http_requests_total{pod="nginx-1", route="/"} 46.00+13.00x40
http_requests_total{pod="nginx-2", route="/"} 2+5.25x40`,
query: `sum(quantile by (route) (-0.5044968945760265, {__name__="http_requests_total",route="/"}))`,
},
{
name: "predict_linear fuzz",
load: `load 30s
http_requests_total{pod="nginx-1", route="/"} 48.00+9.17x40
http_requests_total{pod="nginx-2", route="/"} -108+173.00x40`,
query: `predict_linear(http_requests_total{route="/"}[1h:1m] offset 1m, 60)`,
},
{
name: "duplicate label fuzz",
load: `load 30s
http_requests_total{pod="nginx-1", route="/"} 41.00+0.20x40
http_requests_total{pod="nginx-2", route="/"} 51+21.71x40`,
query: `
-avg by (__name__) (
(-group({__name__="http_requests_total"} @ 54.013) or {__name__="http_requests_total"} offset 1m32s)
)`,
},
{
name: "timestamp fuzz 1",
load: `load 30s
http_requests_total{pod="nginx-1", route="/"} 0.20+9.00x40
http_requests_total{pod="nginx-2", route="/"} 6+60.00x40`,
query: `timestamp(last_over_time(http_requests_total{route="/"}[1h]))`,
},
{
name: "timestamp fuzz 2",
load: `load 30s
http_requests_total{pod="nginx-1", route="/"} 8.00+9.17x40
http_requests_total{pod="nginx-2", route="/"} -12+103.00x40`,
query: `
timestamp(
http_requests_total{pod="nginx-1"} >= bool (http_requests_total < 2 * http_requests_total)
)`,
},
{
name: "timestamp with multiple parenthesis",
load: `load 30s
http_requests_total{pod="nginx-1", route="/"} 8.00+9.17x40
http_requests_total{pod="nginx-2", route="/"} -12+103.00x40`,
query: `timestamp((http_requests_total))`,
},
{
name: "subqueries in binary expression",
load: `load 30s
http_requests_total{pod="nginx-1", route="/"} 1.00+0.20x40
http_requests_total{pod="nginx-2", route="/"} -44+2.00x40`,
query: `
absent_over_time(http_requests_total @ end()[1h:1m])
or
avg_over_time(http_requests_total @ end()[1h:1m])`,
},
{
name: "nested unary negation",
query: `1 / (-(2 * 2))`,
},
{
name: "stddev with large values",
load: `load 30s
http_requests_total{pod="nginx-1", route="/"} 1e+181
http_requests_total{pod="nginx-2", route="/"} 1e+80`,
query: `stddev(http_requests_total)`,
},
{
name: "stddev with NaN 1",
load: `load 30s
http_requests_total{pod="nginx-1", route="/"} NaN
http_requests_total{pod="nginx-2", route="/"} 1`,
query: `stddev by (route) (http_requests_total)`,
},
{
name: "stddev with NaN 2",
load: `load 30s
http_requests_total{pod="nginx-1", route="/"} NaN
http_requests_total{pod="nginx-2", route="/"} 1`,
query: `stddev by (pod) (http_requests_total)`,
},
{
name: "aggregate without",
load: `load 30s
http_requests_total{pod="nginx-1"} 1+1.1x1
http_requests_total{pod="nginx-2"} 2+2.3x1`,
start: time.Unix(0, 0),
end: time.Unix(60, 0),
step: 30 * time.Second,
query: `avg without (pod) (http_requests_total)`,
},
{
name: "func with scalar arg that selects storage, checks whether same series handled correctly",
load: `load 30s
thanos_cache_redis_hits_total{name="caching-bucket",service="thanos-store"} 1+1x30`,
query: `
clamp_min(thanos_cache_redis_hits_total, scalar(max by (service) (thanos_cache_redis_hits_total)))
+
clamp_min(thanos_cache_redis_hits_total, scalar(max by (service) (thanos_cache_redis_hits_total)))`,
},
{
name: "sum + rate divided by itself",
load: `load 30s
thanos_cache_redis_hits_total{name="caching-bucket",service="thanos-store"} 1+1x30`,
query: `
(sum by (service) (rate(thanos_cache_redis_hits_total{name="caching-bucket"}[2m])))
/
(sum by (service) (rate(thanos_cache_redis_hits_total{name="caching-bucket"}[2m])))`,
},
{
name: "stddev_over_time",
load: `load 30s
http_requests_total{pod="nginx-1"} 1+1x15
http_requests_total{pod="nginx-2"} 1+2x18`,
query: `stddev_over_time(http_requests_total[30s])`,
},
{
name: "stdvar_over_time",
load: `load 30s
http_requests_total{pod="nginx-1"} 1+1x15
http_requests_total{pod="nginx-2"} 1+2x18`,
query: `stdvar_over_time(http_requests_total[30s])`,
},
{
name: "quantile_over_time",
load: `load 30s
http_requests_total{pod="nginx-1"} 1+1x15
http_requests_total{pod="nginx-2"} 1+2x18`,
query: `quantile_over_time(0.9, http_requests_total[1m])`,
},
{
name: "quantile_over_time with subquery",
load: `load 30s
http_requests_total{pod="nginx-1"} 41.00+0.20x40
http_requests_total{pod="nginx-2"} 51+21.71x40`,
query: `quantile_over_time(0.5, http_requests_total{pod="nginx-1"}[5m:1m])`,
start: start,
end: end,
},
{
name: "quantile_over_time with subquery and non-constant param",
load: `load 30s
http_requests_total{pod="nginx-1"} 41.00+0.20x40
http_requests_total{pod="nginx-2"} 51+21.71x40
param_series 0+0.01x40`,
query: `quantile_over_time(scalar(param_series), http_requests_total{pod="nginx-1"}[5m:1m])`,
start: start,
end: end,
},
{
name: "predict_linear with subquery and non-constant param",
load: `load 30s
http_requests_total{pod="nginx-1"} 41.00+0.20x40
http_requests_total{pod="nginx-2"} 51+21.71x40
param_series 1+1x40`,
query: `predict_linear(http_requests_total{pod="nginx-1"}[5m:1m], scalar(param_series))`,
start: start,
end: end,
},
{
name: "predict_linear with subquery and non-existing param series",
load: `load 30s
http_requests_total{pod="nginx-1"} 41.00+0.20x40
http_requests_total{pod="nginx-2"} 51+21.71x40`,
query: `predict_linear(http_requests_total{pod="nginx-1"}[5m:1m], scalar(non_existent))`,
start: start,
end: end,
},
{
name: "changes",
load: `load 30s
http_requests_total{pod="nginx-1"} 1+1x15
http_requests_total{pod="nginx-2"} 1+2x18
http_requests_total{pod="nginx-2"} 1+2x18
http_requests_total{pod="nginx-2"} 1+2x18
http_requests_total{pod="nginx-2"} 1+2x18`,
query: `changes(http_requests_total[30s])`,
},
{
name: "deriv",
load: `load 30s
http_requests_total{pod="nginx-1"} 1+1x15
http_requests_total{pod="nginx-2"} 1+2x18
http_requests_total{pod="nginx-2"} 1+2x18
http_requests_total{pod="nginx-2"} 1+2x18
http_requests_total{pod="nginx-2"} 1+2x18`,
query: `deriv(http_requests_total[30s])`,
},
{
name: "abs",
load: `load 30s
http_requests_total{pod="nginx-1"} -5+1x15
http_requests_total{pod="nginx-2"} -5+2x18`,
query: `abs(http_requests_total)`,
},
{
name: "ceil",
load: `load 30s
http_requests_total{pod="nginx-1"} -5.5+1x15
http_requests_total{pod="nginx-2"} -5.5+2x18`,
query: `ceil(http_requests_total)`,
},
{
name: "exp",
load: `load 30s
http_requests_total{pod="nginx-1"} -5.5+1x15
http_requests_total{pod="nginx-2"} -5.5+2x18`,
query: `exp(http_requests_total)`,
},
{
name: "floor",
load: `load 30s
http_requests_total{pod="nginx-1"} -5.5+1x15
http_requests_total{pod="nginx-2"} -5.5+2x18`,
query: `floor(http_requests_total)`,
},
{
name: "floor with a filter",
load: `load 30s
http_requests_total{pod="nginx-1", route="/"} 1
http_requests_total{pod="nginx-2", route="/"} 2`,
query: `floor(http_requests_total{pod="nginx-2"}) / http_requests_total`,
},
{
name: "sqrt",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: `sqrt(http_requests_total)`,
},
{
name: "ln",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: `ln(http_requests_total)`,
},
{
name: "log2",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: `log2(http_requests_total)`,
},
{
name: "log10",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: `log10(http_requests_total)`,
},
{
name: "sin",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: `sin(http_requests_total)`,
},
{
name: "cos",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: `cos(http_requests_total)`,
},
{
name: "tan",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: `tan(http_requests_total)`,
},
{
name: "asin",
load: `load 30s
http_requests_total{pod="nginx-1"} 0
http_requests_total{pod="nginx-2"} 1`,
query: `asin(http_requests_total)`,
},
{
name: "acos",
load: `load 30s
http_requests_total{pod="nginx-1"} 0
http_requests_total{pod="nginx-2"} 1`,
query: `acos(http_requests_total)`,
},
{
name: "atan",
load: `load 30s
http_requests_total{pod="nginx-1"} 0
http_requests_total{pod="nginx-2"} 1`,
query: `atan(http_requests_total)`,
},
{
name: "sinh",
load: `load 30s
http_requests_total{pod="nginx-1"} 0
http_requests_total{pod="nginx-2"} 1`,
query: `sinh(http_requests_total)`,
},
{
name: "cosh",
load: `load 30s
http_requests_total{pod="nginx-1"} 0
http_requests_total{pod="nginx-2"} 1`,
query: `cosh(http_requests_total)`,
},
{
name: "tanh",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: `tanh(http_requests_total)`,
},
{
name: "asinh",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: `asinh(http_requests_total)`,
},
{
name: "acosh",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: `acosh(http_requests_total)`,
},
{
name: "atanh",
load: `load 30s
http_requests_total{pod="nginx-1"} 0
http_requests_total{pod="nginx-2"} 1`,
query: `atanh(http_requests_total)`,
},
{
name: "rad",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: `rad(http_requests_total)`,
},
{
name: "deg",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: `deg(http_requests_total)`,
},
{
name: "pi",
load: ``,
query: `pi()`,
},
{
name: "sum",
load: `load 30s
http_requests_total{pod="nginx-1"} 1+1x15
http_requests_total{pod="nginx-2"} 1+2x18`,
query: `sum(http_requests_total)`,
},
{
name: "sum_over_time",
load: `load 30s
http_requests_total{pod="nginx-1"} 1+1x15
http_requests_total{pod="nginx-2"} 1+2x18`,
query: `sum_over_time(http_requests_total[30s])`,
},
{
name: "count",
load: `load 30s
http_requests_total{pod="nginx-1"} 1+1x15
http_requests_total{pod="nginx-2"} 1+2x18`,
query: `count(http_requests_total)`,
},
{
name: "count_over_time",
load: `load 30s
http_requests_total{pod="nginx-1"} 1+1x15
http_requests_total{pod="nginx-2"} 1+2x18`,
query: `count_over_time(http_requests_total[30s])`,
},
{
name: "average",
load: `load 30s
http_requests_total{pod="nginx-1"} 1+1x15
http_requests_total{pod="nginx-2"} 1+2x18`,
query: `avg(http_requests_total)`,
},
{
name: "avg_over_time",
load: `load 30s
http_requests_total{pod="nginx-1"} 1+1x15
http_requests_total{pod="nginx-2"} 1+2x18`,
query: `avg_over_time(http_requests_total[30s])`,
},
{
name: "abs",
load: `load 30s
http_requests_total{pod="nginx-1"} -10+1x15
http_requests_total{pod="nginx-2"} -10+2x18`,
query: `abs(http_requests_total)`,
},
{
name: "max",
load: `load 30s
http_requests_total{pod="nginx-1"} 1+1x15
http_requests_total{pod="nginx-2"} 1+2x18`,
query: `max(http_requests_total)`,
},
{
name: "max with only 1 sample",
load: `load 30s
http_requests_total{pod="nginx-1"} -1
http_requests_total{pod="nginx-2"} 1`,
query: `max by (pod) (http_requests_total)`,
},
{
name: "max_over_time",
load: `load 30s
http_requests_total{pod="nginx-1"} 1+1x15
http_requests_total{pod="nginx-2"} 1+2x18`,
query: `max_over_time(http_requests_total[30s])`,
},
{
name: "min",
load: `load 30s
http_requests_total{pod="nginx-1"} 1+1x15
http_requests_total{pod="nginx-2"} 1+2x18`,
query: `min(http_requests_total)`,
},
{
name: "min with only 1 sample",
load: `load 30s
http_requests_total{pod="nginx-1"} -1
http_requests_total{pod="nginx-2"} 1`,
query: `min by (pod) (http_requests_total)`,
},
{
name: "min_over_time",
load: `load 30s
http_requests_total{pod="nginx-1"} 1+1x15
http_requests_total{pod="nginx-2"} 1+2x18`,
query: `min_over_time(http_requests_total[30s])`,
},
{
name: "count_over_time",
load: `load 30s
http_requests_total{pod="nginx-1"} 1+1x15
http_requests_total{pod="nginx-2"} 1+2x18`,
query: `count_over_time(http_requests_total[30s])`,
},
{
name: "sum by pod",
load: `load 30s
http_requests_total{pod="nginx-1"} 1+1x15
http_requests_total{pod="nginx-2"} 1+2x18
http_requests_total{pod="nginx-3"} 1+2x20
http_requests_total{pod="nginx-4"} 1+2x50`,
query: `sum by (pod) (http_requests_total)`,
},
{
name: "multi label grouping by",
load: `load 30s
http_requests_total{pod="nginx-1", ns="a"} 1+1x15
http_requests_total{pod="nginx-2", ns="a"} 1+1x15`,
query: `avg by (pod, ns) (avg_over_time(http_requests_total[2m]))`,
},
{
name: "multi label grouping without",
load: `load 30s
http_requests_total{pod="nginx-1", ns="a"} 1+1x15
http_requests_total{pod="nginx-2", ns="a"} 1+1x15`,
query: `avg without (pod, ns) (avg_over_time(http_requests_total[2m]))`,
},
{
name: "query in the future",
load: `load 30s
http_requests_total{pod="nginx-1"} 1+1x15
http_requests_total{pod="nginx-2"} 1+2x18`,
query: `sum by (pod) (http_requests_total)`,
start: time.Unix(400, 0),
end: time.Unix(3000, 0),
},
{
name: "count_over_time",
load: `load 30s
http_requests_total{pod="nginx-1"} 1
http_requests_total{pod="nginx-1"} 1+1x30
http_requests_total{pod="nginx-2"} 1+2x600`,
query: `count_over_time(http_requests_total[10m])`,
start: time.Unix(60, 0),
end: time.Unix(600, 0),
},
{
name: "rate",
load: `load 30s
http_requests_total{pod="nginx-1", series="1"} 1+1.1x40
http_requests_total{pod="nginx-2", series="2"} 2+2.3x50
http_requests_total{pod="nginx-4", series="3"} 5+2.4x50
http_requests_total{pod="nginx-5", series="1"} 8.4+2.3x50
http_requests_total{pod="nginx-6", series="2"} 2.3+2.3x50`,
query: `rate(http_requests_total[1m])`,
start: time.Unix(0, 0),
end: time.Unix(3000, 0),
step: 2 * time.Second,
},
{
name: "rate with counter reset and step larger than window",
load: `load 30s
http_requests_total{pod="nginx-1", series="1"} 0+1x3 0+1x2 0+1x3`,
query: `rate(http_requests_total[1m])`,
start: time.Unix(0, 0),
end: time.Unix(200, 0),
step: 90 * time.Second,
},
{
name: "sum rate",
load: `load 30s
http_requests_total{pod="nginx-1"} 1+1x4
http_requests_total{pod="nginx-2"} 1+2x4`,
query: `sum(rate(http_requests_total[1m]))`,
},
{
name: "sum rate with stale series",
load: `load 30s
http_requests_total{pod="nginx-1"} 1+1x40
http_requests_total{pod="nginx-2"} 1+2x50
http_requests_total{pod="nginx-4"} 1+2x50
http_requests_total{pod="nginx-5"} 1+2x50
http_requests_total{pod="nginx-6"} 1+2x50`,
query: `sum(rate(http_requests_total[1m]))`,
start: time.Unix(421, 0),
end: time.Unix(3230, 0),
step: 28 * time.Second,
},
{
name: "delta",
load: `load 30s
http_requests_total{pod="nginx-1", series="1"} 1+1.1x40
http_requests_total{pod="nginx-2", series="2"} 2+2.3x50
http_requests_total{pod="nginx-4", series="3"} 5+2.4x50
http_requests_total{pod="nginx-5", series="1"} 8.4+2.3x50
http_requests_total{pod="nginx-6", series="2"} 2.3+2.3x50`,
query: `delta(http_requests_total[1m])`,
start: time.Unix(0, 0),
end: time.Unix(3000, 0),
step: 2 * time.Second,
},
{
name: "increase",
load: `load 30s
http_requests_total{pod="nginx-1", series="1"} 1+1.1x40
http_requests_total{pod="nginx-2", series="2"} 2+2.3x50
http_requests_total{pod="nginx-4", series="3"} 5+2.4x50
http_requests_total{pod="nginx-5", series="1"} 8.4+2.3x50
http_requests_total{pod="nginx-6", series="2"} 2.3+2.3x50`,
query: `increase(http_requests_total[1m])`,
start: time.Unix(0, 0),
end: time.Unix(3000, 0),
step: 2 * time.Second,
},
{
name: "irate",
load: `load 30s
http_requests_total{pod="nginx-1", series="1"} 1+1.1x40
http_requests_total{pod="nginx-2", series="2"} 2+2.3x50
http_requests_total{pod="nginx-4", series="3"} 5+2.4x50
http_requests_total{pod="nginx-5", series="1"} 8.4+2.3x50
http_requests_total{pod="nginx-6", series="2"} 2.3+2.3x50`,
query: `irate(http_requests_total[1m])`,
start: time.Unix(0, 0),
end: time.Unix(3000, 0),
step: 2 * time.Second,
},
{
name: "idelta",
load: `load 30s
http_requests_total{pod="nginx-1", series="1"} 1+1.1x40
http_requests_total{pod="nginx-2", series="2"} 2+2.3x50
http_requests_total{pod="nginx-4", series="3"} 5+2.4x50
http_requests_total{pod="nginx-5", series="1"} 8.4+2.3x50
http_requests_total{pod="nginx-6", series="2"} 2.3+2.3x50`,
query: `idelta(http_requests_total[1m])`,
start: time.Unix(0, 0),
end: time.Unix(3000, 0),
step: 2 * time.Second,
},
{
name: "number literal",
load: "",
query: `34`,
},
{
name: "vector",
load: "",
query: `vector(24)`,
},
{
name: "binary operation atan2",
load: `load 30s
foo{} 10
bar{} 2`,
query: `foo atan2 bar`,
},
{
name: "binary operation atan2 with NaN",
load: `load 30s
foo{} 10
bar{} NaN`,
query: `foo atan2 bar`,
},
{
name: "binary operation with one-to-one matching",
load: `load 30s
foo{method="get", code="500"} 1+1x1
foo{method="get", code="404"} 2+1x2
foo{method="put", code="501"} 3+1x3
foo{method="put", code="500"} 1+1x4
foo{method="post", code="500"} 4+1x4
foo{method="post", code="404"} 5+1x5
bar{method="get"} 1+1x1
bar{method="del"} 2+1x2
bar{method="post"} 3+1x3`,
query: `foo{code="500"} + ignoring (code) bar`,
start: time.Unix(0, 0),
end: time.Unix(600, 0),
},
{
// Example from https://prometheus.io/docs/prometheus/latest/querying/operators/#many-to-one-and-one-to-many-vector-matches
name: "binary operation with group_left",
load: `load 30s
foo{method="get", code="500", path="/"} 1+1.1x30
foo{method="get", code="404", path="/"} 1+2.2x20
foo{method="put", code="501", path="/"} 4+3.4x60
foo{method="post", code="500", path="/"} 1+5.1x40
foo{method="post", code="404", path="/"} 2+3.7x40
bar{method="get", path="/a"} 3+7.4x10
bar{method="del", path="/b"} 8+6.1x30
bar{method="post", path="/c"} 1+2.1x40`,
query: `foo * ignoring (path, code) group_left () bar`,
start: time.Unix(0, 0),
end: time.Unix(600, 0),
},
{
// Example from https://prometheus.io/docs/prometheus/latest/querying/operators/#many-to-one-and-one-to-many-vector-matches
name: "binary operation with group_right",
load: `load 30s
foo{method="get", code="500"} 1+1.1x30
foo{method="get", code="404"} 1+2.2x20
foo{method="put", code="501"} 4+3.4x60
foo{method="post", code="500"} 1+5.1x40
foo{method="post", code="404"} 2+3.7x40
bar{method="get", path="/a"} 3+7.4x10
bar{method="del", path="/b"} 8+6.1x30
bar{method="post", path="/c"} 1+2.1x40`,
query: `bar * ignoring (code, path) group_right () foo`,
start: time.Unix(0, 0),
end: time.Unix(3000, 0),
step: 2 * time.Second,
},
{
name: "binary operation with group_left and included labels",
load: `load 30s
foo{method="get", code="500"} 1+1.1x30
foo{method="get", code="404"} 1+2.2x20
foo{method="put", code="501"} 4+3.4x60
foo{method="post", code="500"} 1+5.1x40
foo{method="post", code="404"} 2+3.7x40
bar{method="get", path="/a"} 3+7.4x10
bar{method="del", path="/b"} 8+6.1x30
bar{method="post", path="/c"} 1+2.1x40`,
query: `foo * ignoring (code, path) group_left (path) bar`,
start: time.Unix(0, 0),
end: time.Unix(600, 0),
},
{
name: "binary operation with group_right and included labels",
load: `load 30s
foo{method="get", code="500"} 1+1.1x30
foo{method="get", code="404"} 1+2.2x20
foo{method="put", code="501"} 4+3.4x60
foo{method="post", code="500"} 1+5.1x40
foo{method="post", code="404"} 2+3.7x40
bar{method="get", path="/a"} 3+7.4x10
bar{method="del", path="/b"} 8+6.1x30
bar{method="post", path="/c"} 1+2.1x40`,
query: `bar * ignoring (code, path) group_right (path) foo`,
start: time.Unix(0, 0),
end: time.Unix(600, 0),
},
{
name: "binary operation with vector and scalar on the right",
load: `load 30s
foo{method="get", code="500"} 1+1.1x30
foo{method="get", code="404"} 1+2.2x20`,
query: `sum(foo) * 2`,
},
{
name: "binary operation with vector and scalar on the left",
load: `load 30s
foo{method="get", code="500"} 1+1.1x30
foo{method="get", code="404"} 1+2.2x20`,
query: `2 * sum(foo)`,
},
{
name: "complex binary operation",
load: `load 30s
foo{method="get", code="500"} 1+1.1x30
foo{method="get", code="404"} 1+2.2x20`,
query: `1 - (100 * sum(foo{method="get"}) / sum(foo))`,
},
{
name: "binary operation with many-to-many matching",
load: `load 30s
foo{code="200", method="get"} 1+1x20
foo{code="200", method="post"} 1+1x20
bar{code="200", method="get"} 1+1x20
bar{code="200", method="post"} 1+1x20`,
query: `foo + on (code) bar`,
},
{
name: "binary operation with many-to-many matching lhs high card",
load: `load 30s
foo{code="200", method="get"} 1+1x20
foo{code="200", method="post"} 1+1x20
bar{code="200", method="get"} 1+1x20
bar{code="200", method="post"} 1+1x20`,
query: `foo + on (code) group_left () bar`,
},
{
name: "binary operation with many-to-many matching rhs high card",
load: `load 30s
foo{code="200", method="get"} 1+1x20
foo{code="200", method="post"} 1+1x20
bar{code="200", method="get"} 1+1x20
bar{code="200", method="post"} 1+1x20`,
query: `foo + on (code) group_right () bar`,
},
{
name: "vector binary op ==",
load: `load 30s
foo{method="get", code="500"} 1+1x40
bar{method="get", code="404"} 1+1.1x30`,
query: `sum by (method) (foo) == sum by (method) (bar)`,
},
{
name: "vector binary op !=",
load: `load 30s
foo{method="get", code="500"} 1+1x40
bar{method="get", code="404"} 1+1.1x30`,
query: `sum by (method) (foo) != sum by (method) (bar)`,
},
{
name: "vector binary op >",
load: `load 30s
foo{method="get", code="500"} 1+1x40
bar{method="get", code="404"} 1+1.1x30`,
query: `sum by (method) (foo) > sum by (method) (bar)`,
},
{
name: "vector binary op with name <",
load: `load 30s
foo{method="get", code="500"} 1+1x40
bar{method="get", code="500"} 1+1.1x30`,
query: `foo < bar`,
},
{
name: "vector binary op with name < scalar",
load: `load 30s
foo{method="get", code="500"} 1+1x40
bar{method="get", code="500"} 1+1.1x30`,
query: `foo < 10`,
},
{
name: "vector binary op with name < scalar and bool modifier",
load: `load 30s
foo{method="get", code="500"} 1+1x40
bar{method="get", code="500"} 1+1.1x30`,
query: `foo < bool 10`,
},
{
name: "vector binary op > scalar",
load: `load 30s
foo{method="get", code="500"} 1+2x40
bar{method="get", code="404"} 1+1x30`,
query: `sum by (method) (foo) > 10`,
},
{
name: "vector binary op > scalar and bool modifier",
load: `load 30s
foo{method="get", code="500"} 1+2x40
bar{method="get", code="404"} 1+1x30`,
query: `sum by (method) (foo) > bool 10`,
},
{
name: "scalar < vector binary op",