@@ -52,24 +52,27 @@ func TestProtobufCodec_Encode(t *testing.T) {
5252
5353 tests := []struct {
5454 name string
55- data * v1.QueryData
55+ resp * v1.Response
5656 cortexInternal bool
5757 expected * tripperware.PrometheusResponse
5858 }{
5959 {
6060 name : "vector" ,
61- data : & v1.QueryData {
62- ResultType : parser .ValueTypeVector ,
63- Result : promql.Vector {
64- promql.Sample {
65- Metric : labels .FromStrings ("__name__" , "foo" ),
66- T : 1000 ,
67- F : 1 ,
68- },
69- promql.Sample {
70- Metric : labels .FromStrings ("__name__" , "bar" ),
71- T : 2000 ,
72- F : 2 ,
61+ resp : & v1.Response {
62+ Status : tripperware .StatusSuccess ,
63+ Data : & v1.QueryData {
64+ ResultType : parser .ValueTypeVector ,
65+ Result : promql.Vector {
66+ promql.Sample {
67+ Metric : labels .FromStrings ("__name__" , "foo" ),
68+ T : 1000 ,
69+ F : 1 ,
70+ },
71+ promql.Sample {
72+ Metric : labels .FromStrings ("__name__" , "bar" ),
73+ T : 2000 ,
74+ F : 2 ,
75+ },
7376 },
7477 },
7578 },
@@ -102,9 +105,12 @@ func TestProtobufCodec_Encode(t *testing.T) {
102105 },
103106 {
104107 name : "scalar" ,
105- data : & v1.QueryData {
106- ResultType : parser .ValueTypeScalar ,
107- Result : promql.Scalar {T : 1000 , V : 1 },
108+ resp : & v1.Response {
109+ Status : tripperware .StatusSuccess ,
110+ Data : & v1.QueryData {
111+ ResultType : parser .ValueTypeScalar ,
112+ Result : promql.Scalar {T : 1000 , V : 1 },
113+ },
108114 },
109115 expected : & tripperware.PrometheusResponse {
110116 Status : tripperware .StatusSuccess ,
@@ -120,16 +126,19 @@ func TestProtobufCodec_Encode(t *testing.T) {
120126 },
121127 {
122128 name : "matrix" ,
123- data : & v1.QueryData {
124- ResultType : parser .ValueTypeMatrix ,
125- Result : promql.Matrix {
126- promql.Series {
127- Metric : labels .FromStrings ("__name__" , "foo" ),
128- Floats : []promql.FPoint {{F : 1 , T : 1000 }},
129- },
130- promql.Series {
131- Metric : labels .FromStrings ("__name__" , "bar" ),
132- Floats : []promql.FPoint {{F : 2 , T : 2000 }},
129+ resp : & v1.Response {
130+ Status : tripperware .StatusSuccess ,
131+ Data : & v1.QueryData {
132+ ResultType : parser .ValueTypeMatrix ,
133+ Result : promql.Matrix {
134+ promql.Series {
135+ Metric : labels .FromStrings ("__name__" , "foo" ),
136+ Floats : []promql.FPoint {{F : 1 , T : 1000 }},
137+ },
138+ promql.Series {
139+ Metric : labels .FromStrings ("__name__" , "bar" ),
140+ Floats : []promql.FPoint {{F : 2 , T : 2000 }},
141+ },
133142 },
134143 },
135144 },
@@ -166,15 +175,18 @@ func TestProtobufCodec_Encode(t *testing.T) {
166175 },
167176 {
168177 name : "matrix with multiple float samples" ,
169- data : & v1.QueryData {
170- ResultType : parser .ValueTypeMatrix ,
171- Result : promql.Matrix {
172- promql.Series {
173- Metric : labels .FromStrings ("__name__" , "foo" , "__job__" , "bar" ),
174- Floats : []promql.FPoint {
175- {F : 0.14 , T : 18555000 },
176- {F : 2.9 , T : 18556000 },
177- {F : 30 , T : 18557000 },
178+ resp : & v1.Response {
179+ Status : tripperware .StatusSuccess ,
180+ Data : & v1.QueryData {
181+ ResultType : parser .ValueTypeMatrix ,
182+ Result : promql.Matrix {
183+ promql.Series {
184+ Metric : labels .FromStrings ("__name__" , "foo" , "__job__" , "bar" ),
185+ Floats : []promql.FPoint {
186+ {F : 0.14 , T : 18555000 },
187+ {F : 2.9 , T : 18556000 },
188+ {F : 30 , T : 18557000 },
189+ },
178190 },
179191 },
180192 },
@@ -207,12 +219,15 @@ func TestProtobufCodec_Encode(t *testing.T) {
207219 },
208220 {
209221 name : "matrix with histogram and not cortex internal" ,
210- data : & v1.QueryData {
211- ResultType : parser .ValueTypeMatrix ,
212- Result : promql.Matrix {
213- promql.Series {
214- Histograms : []promql.HPoint {{H : testFloatHistogram , T : 1000 }},
215- Metric : labels .FromStrings ("__name__" , "foo" ),
222+ resp : & v1.Response {
223+ Status : tripperware .StatusSuccess ,
224+ Data : & v1.QueryData {
225+ ResultType : parser .ValueTypeMatrix ,
226+ Result : promql.Matrix {
227+ promql.Series {
228+ Histograms : []promql.HPoint {{H : testFloatHistogram , T : 1000 }},
229+ Metric : labels .FromStrings ("__name__" , "foo" ),
230+ },
216231 },
217232 },
218233 },
@@ -297,12 +312,15 @@ func TestProtobufCodec_Encode(t *testing.T) {
297312 },
298313 {
299314 name : "matrix with histogram and not cortex internal, empty bucket" ,
300- data : & v1.QueryData {
301- ResultType : parser .ValueTypeMatrix ,
302- Result : promql.Matrix {
303- promql.Series {
304- Histograms : []promql.HPoint {{H : floatHistogramWithEmptyBucket , T : 1000 }},
305- Metric : labels .FromStrings ("__name__" , "foo" ),
315+ resp : & v1.Response {
316+ Status : tripperware .StatusSuccess ,
317+ Data : & v1.QueryData {
318+ ResultType : parser .ValueTypeMatrix ,
319+ Result : promql.Matrix {
320+ promql.Series {
321+ Histograms : []promql.HPoint {{H : floatHistogramWithEmptyBucket , T : 1000 }},
322+ Metric : labels .FromStrings ("__name__" , "foo" ),
323+ },
306324 },
307325 },
308326 },
@@ -338,13 +356,16 @@ func TestProtobufCodec_Encode(t *testing.T) {
338356 },
339357 {
340358 name : "vector with histogram and not cortex internal" ,
341- data : & v1.QueryData {
342- ResultType : parser .ValueTypeVector ,
343- Result : promql.Vector {
344- promql.Sample {
345- Metric : labels .FromStrings ("__name__" , "foo" ),
346- T : 1000 ,
347- H : testFloatHistogram ,
359+ resp : & v1.Response {
360+ Status : tripperware .StatusSuccess ,
361+ Data : & v1.QueryData {
362+ ResultType : parser .ValueTypeVector ,
363+ Result : promql.Vector {
364+ promql.Sample {
365+ Metric : labels .FromStrings ("__name__" , "foo" ),
366+ T : 1000 ,
367+ H : testFloatHistogram ,
368+ },
348369 },
349370 },
350371 },
@@ -427,13 +448,16 @@ func TestProtobufCodec_Encode(t *testing.T) {
427448 },
428449 {
429450 name : "vector with histogram with and not cortex internal, empty bucket" ,
430- data : & v1.QueryData {
431- ResultType : parser .ValueTypeVector ,
432- Result : promql.Vector {
433- promql.Sample {
434- Metric : labels .FromStrings ("__name__" , "foo" ),
435- T : 1000 ,
436- H : floatHistogramWithEmptyBucket ,
451+ resp : & v1.Response {
452+ Status : tripperware .StatusSuccess ,
453+ Data : & v1.QueryData {
454+ ResultType : parser .ValueTypeVector ,
455+ Result : promql.Vector {
456+ promql.Sample {
457+ Metric : labels .FromStrings ("__name__" , "foo" ),
458+ T : 1000 ,
459+ H : floatHistogramWithEmptyBucket ,
460+ },
437461 },
438462 },
439463 },
@@ -468,13 +492,16 @@ func TestProtobufCodec_Encode(t *testing.T) {
468492 {
469493 name : "vector with histogram and cortex internal" ,
470494 cortexInternal : true ,
471- data : & v1.QueryData {
472- ResultType : parser .ValueTypeVector ,
473- Result : promql.Vector {
474- promql.Sample {
475- Metric : labels .FromStrings ("__name__" , "foo" ),
476- T : 1000 ,
477- H : testFloatHistogram ,
495+ resp : & v1.Response {
496+ Status : tripperware .StatusSuccess ,
497+ Data : & v1.QueryData {
498+ ResultType : parser .ValueTypeVector ,
499+ Result : promql.Vector {
500+ promql.Sample {
501+ Metric : labels .FromStrings ("__name__" , "foo" ),
502+ T : 1000 ,
503+ H : testFloatHistogram ,
504+ },
478505 },
479506 },
480507 },
@@ -499,17 +526,42 @@ func TestProtobufCodec_Encode(t *testing.T) {
499526 },
500527 },
501528 },
529+ {
530+ name : "all fields except data" ,
531+ cortexInternal : true ,
532+ resp : & v1.Response {
533+ Status : tripperware .StatusSuccess ,
534+ Data : & v1.QueryData {
535+ ResultType : parser .ValueTypeVector ,
536+ Result : promql.Vector {},
537+ },
538+ ErrorType : "internal" ,
539+ Error : "some internal error" ,
540+ Warnings : []string {"PromQL warning: warning 1" , "PromQL warning: warning 2" },
541+ Infos : []string {"PromQL info: info 1" , "PromQL info: info 2" },
542+ },
543+ expected : & tripperware.PrometheusResponse {
544+ Status : tripperware .StatusSuccess ,
545+ Data : tripperware.PrometheusData {
546+ ResultType : model .ValVector .String (),
547+ Result : tripperware.PrometheusQueryResult {
548+ Result : & tripperware.PrometheusQueryResult_Vector {Vector : & tripperware.Vector {}},
549+ },
550+ },
551+ ErrorType : "internal" ,
552+ Error : "some internal error" ,
553+ Warnings : []string {"PromQL warning: warning 1" , "PromQL warning: warning 2" },
554+ Infos : []string {"PromQL info: info 1" , "PromQL info: info 2" },
555+ },
556+ },
502557 }
503558
504559 for _ , test := range tests {
505560 t .Run (test .name , func (t * testing.T ) {
506561 reg := prometheus .NewPedanticRegistry ()
507562 cm := NewInstrumentedCodecMetrics (reg )
508563 codec := NewInstrumentedCodec (ProtobufCodec {CortexInternal : test .cortexInternal }, cm )
509- body , err := codec .Encode (& v1.Response {
510- Status : tripperware .StatusSuccess ,
511- Data : test .data ,
512- })
564+ body , err := codec .Encode (test .resp )
513565 require .NoError (t , err )
514566 b , err := proto .Marshal (test .expected )
515567 require .NoError (t , err )
0 commit comments