@@ -6,7 +6,7 @@ package queryrange
6
6
import (
7
7
"bytes"
8
8
"context"
9
- io "io"
9
+ "io"
10
10
"net/http"
11
11
"strconv"
12
12
"testing"
@@ -36,6 +36,20 @@ func TestRequest(t *testing.T) {
36
36
url : query ,
37
37
expected : & parsedRequestWithHeaders ,
38
38
},
39
+ {
40
+ url : "/api/v1/query_range?end=60&query=sum%28container_memory_rss%29+by+%28namespace%29&start=0&stats=all&step=14" ,
41
+ expected : & PrometheusRequest {
42
+ Path : "/api/v1/query_range" ,
43
+ Start : 0 ,
44
+ End : 60_000 ,
45
+ Step : 14_000 ,
46
+ Query : "sum(container_memory_rss) by (namespace)" ,
47
+ Stats : "all" ,
48
+ Headers : []* PrometheusRequestHeader {
49
+ {Name : "Test-Header" , Values : []string {"test" }},
50
+ },
51
+ },
52
+ },
39
53
{
40
54
url : "api/v1/query_range?start=foo&stats=all" ,
41
55
expectedErr : httpgrpc .Errorf (http .StatusBadRequest , "invalid parameter \" start\" ; cannot parse \" foo\" to a valid timestamp" ),
@@ -129,7 +143,7 @@ func TestResponseWithStats(t *testing.T) {
129
143
expected * PrometheusResponse
130
144
}{
131
145
{
132
- body : `{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"foo":"bar"},"values":[[1536673680,"137"],[1536673780,"137"]]}],"stats":{"samples":{"totalQueryableSamples":10,"totalQueryableSamplesPerStep":[[1536673680,5],[1536673780,5]]}},"analysis":null}}` ,
146
+ body : `{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"foo":"bar"},"values":[[1536673680,"137"],[1536673780,"137"]]}],"stats":{"samples":{"peakSamples":0," totalQueryableSamples":10,"totalQueryableSamplesPerStep":[[1536673680,5],[1536673780,5]]}},"analysis":null}}` ,
133
147
expected : & PrometheusResponse {
134
148
Status : "success" ,
135
149
Data : PrometheusData {
@@ -158,7 +172,7 @@ func TestResponseWithStats(t *testing.T) {
158
172
},
159
173
},
160
174
{
161
- body : `{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"foo":"bar"},"values":[[1536673680,"137"],[1536673780,"137"]]}],"stats":{"samples":{"totalQueryableSamples":10,"totalQueryableSamplesPerStep":[[1536673680,5],[1536673780,5]]}},"analysis":{"name":"[noArgFunction]","executionTime":"1s","children":null}}}` ,
175
+ body : `{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"foo":"bar"},"values":[[1536673680,"137"],[1536673780,"137"]]}],"stats":{"samples":{"peakSamples":0," totalQueryableSamples":10,"totalQueryableSamplesPerStep":[[1536673680,5],[1536673780,5]]}},"analysis":{"name":"[noArgFunction]","executionTime":"1s","children":null}}}` ,
162
176
expected : & PrometheusResponse {
163
177
Status : "success" ,
164
178
Data : PrometheusData {
@@ -211,11 +225,27 @@ func TestResponseWithStats(t *testing.T) {
211
225
}
212
226
resp2 , err := PrometheusCodec .EncodeResponse (context .Background (), resp )
213
227
require .NoError (t , err )
214
- assert .Equal (t , response , resp2 )
228
+ assert .Equal (t , prettyPrintJsonBody ( t , response . Body ), prettyPrintJsonBody ( t , resp2 . Body ) )
215
229
})
216
230
}
217
231
}
218
232
233
+ func prettyPrintJsonBody (t * testing.T , body io.ReadCloser ) string {
234
+ t .Helper ()
235
+
236
+ bodyContent , err := io .ReadAll (body )
237
+ require .NoError (t , err )
238
+
239
+ var jsonData interface {}
240
+ err = json .Unmarshal (bodyContent , & jsonData )
241
+ require .NoError (t , err )
242
+
243
+ prettyBytes , err := json .MarshalIndent (jsonData , "" , " " )
244
+ require .NoError (t , err )
245
+
246
+ return string (prettyBytes )
247
+ }
248
+
219
249
func TestMergeAPIResponses (t * testing.T ) {
220
250
for _ , tc := range []struct {
221
251
name string
@@ -659,7 +689,7 @@ func TestMergeAPIResponses(t *testing.T) {
659
689
},
660
690
},
661
691
Stats : & PrometheusResponseStats {Samples : & PrometheusResponseSamplesStats {
662
- TotalQueryableSamples : 25 ,
692
+ TotalQueryableSamples : 30 ,
663
693
TotalQueryableSamplesPerStep : []* PrometheusResponseQueryableSamplesStatsPerStep {
664
694
{Value : 5 , TimestampMs : 1000 },
665
695
{Value : 5 , TimestampMs : 2000 },
@@ -696,7 +726,7 @@ func TestMergeAPIResponses(t *testing.T) {
696
726
},
697
727
},
698
728
Stats : & PrometheusResponseStats {Samples : & PrometheusResponseSamplesStats {
699
- TotalQueryableSamples : 28 ,
729
+ TotalQueryableSamples : 36 ,
700
730
TotalQueryableSamplesPerStep : []* PrometheusResponseQueryableSamplesStatsPerStep {
701
731
{Value : 1 , TimestampMs : 1000 },
702
732
{Value : 2 , TimestampMs : 2000 },
@@ -734,7 +764,7 @@ func TestMergeAPIResponses(t *testing.T) {
734
764
},
735
765
},
736
766
Stats : & PrometheusResponseStats {Samples : & PrometheusResponseSamplesStats {
737
- TotalQueryableSamples : 15 ,
767
+ TotalQueryableSamples : 26 ,
738
768
TotalQueryableSamplesPerStep : []* PrometheusResponseQueryableSamplesStatsPerStep {
739
769
{Value : 1 , TimestampMs : 1000 },
740
770
{Value : 2 , TimestampMs : 2000 },
@@ -769,7 +799,7 @@ func TestMergeAPIResponses(t *testing.T) {
769
799
},
770
800
},
771
801
Stats : & PrometheusResponseStats {Samples : & PrometheusResponseSamplesStats {
772
- TotalQueryableSamples : 14 ,
802
+ TotalQueryableSamples : 40 ,
773
803
TotalQueryableSamplesPerStep : []* PrometheusResponseQueryableSamplesStatsPerStep {
774
804
{Value : 2 , TimestampMs : 2000 },
775
805
{Value : 3 , TimestampMs : 3000 },
0 commit comments