-
Notifications
You must be signed in to change notification settings - Fork 806
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow ruler to retrieve proto format query response
Signed-off-by: SungJin1212 <[email protected]>
- Loading branch information
1 parent
7fb98ab
commit 6915258
Showing
21 changed files
with
648 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
package codec | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cortexproject/cortex/pkg/cortexpb" | ||
"github.com/cortexproject/cortex/pkg/querier/tripperware" | ||
"github.com/gogo/protobuf/proto" | ||
"github.com/prometheus/prometheus/model/labels" | ||
"github.com/prometheus/prometheus/promql" | ||
v1 "github.com/prometheus/prometheus/web/api/v1" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPrometheusResponse_ProtoMarshalUnMarshal(t *testing.T) { | ||
var tests = []struct { | ||
name string | ||
resp *v1.Response | ||
expectedUnmarshalResp tripperware.PrometheusResponse | ||
}{ | ||
{ | ||
name: "vector", | ||
resp: &v1.Response{ | ||
Status: "success", | ||
Data: &v1.QueryData{ | ||
ResultType: "vector", | ||
Result: promql.Vector{ | ||
{ | ||
Metric: labels.FromStrings("name", "value"), | ||
T: 1234, | ||
F: 5.67, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedUnmarshalResp: tripperware.PrometheusResponse{ | ||
Status: "success", | ||
Data: tripperware.PrometheusData{ | ||
ResultType: "vector", | ||
Result: tripperware.PrometheusQueryResult{ | ||
Result: &tripperware.PrometheusQueryResult_Vector{ | ||
Vector: &tripperware.Vector{ | ||
Samples: []tripperware.Sample{ | ||
{ | ||
Labels: cortexpb.FromLabelsToLabelAdapters(labels.FromStrings("name", "value")), | ||
Sample: &cortexpb.Sample{ | ||
TimestampMs: 1234, | ||
Value: 5.67, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "matrix", | ||
resp: &v1.Response{ | ||
Status: "success", | ||
Data: &v1.QueryData{ | ||
ResultType: "matrix", | ||
Result: promql.Matrix{ | ||
{ | ||
Metric: labels.FromStrings("name1", "value1"), | ||
Floats: []promql.FPoint{ | ||
{T: 12, F: 3.4}, | ||
{T: 56, F: 7.8}, | ||
}, | ||
}, | ||
{ | ||
Metric: labels.FromStrings("name2", "value2"), | ||
Floats: []promql.FPoint{ | ||
{T: 12, F: 3.4}, | ||
{T: 56, F: 7.8}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedUnmarshalResp: tripperware.PrometheusResponse{ | ||
Status: "success", | ||
Data: tripperware.PrometheusData{ | ||
ResultType: "matrix", | ||
Result: tripperware.PrometheusQueryResult{ | ||
Result: &tripperware.PrometheusQueryResult_Matrix{ | ||
Matrix: &tripperware.Matrix{ | ||
SampleStreams: []tripperware.SampleStream{ | ||
{ | ||
Labels: cortexpb.FromLabelsToLabelAdapters(labels.FromStrings("name1", "value1")), | ||
Samples: []cortexpb.Sample{ | ||
{ | ||
TimestampMs: 12, | ||
Value: 3.4, | ||
}, | ||
{ | ||
TimestampMs: 56, | ||
Value: 7.8, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Labels: cortexpb.FromLabelsToLabelAdapters(labels.FromStrings("name2", "value2")), | ||
Samples: []cortexpb.Sample{ | ||
{ | ||
TimestampMs: 12, | ||
Value: 3.4, | ||
}, | ||
{ | ||
TimestampMs: 56, | ||
Value: 7.8, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "scalar", | ||
resp: &v1.Response{ | ||
Status: "success", | ||
Data: &v1.QueryData{ | ||
ResultType: "scalar", | ||
Result: promql.Scalar{T: 1000, V: 2}, | ||
}, | ||
}, | ||
expectedUnmarshalResp: tripperware.PrometheusResponse{ | ||
Status: "success", | ||
Data: tripperware.PrometheusData{ | ||
ResultType: "scalar", | ||
Result: tripperware.PrometheusQueryResult{ | ||
Result: &tripperware.PrometheusQueryResult_RawBytes{ | ||
RawBytes: []byte(`{"resultType":"scalar","result":[1,"2"]}`), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "string", | ||
resp: &v1.Response{ | ||
Status: "success", | ||
Data: &v1.QueryData{ | ||
ResultType: "string", | ||
Result: promql.String{T: 1000, V: "2"}, | ||
}, | ||
}, | ||
expectedUnmarshalResp: tripperware.PrometheusResponse{ | ||
Status: "success", | ||
Data: tripperware.PrometheusData{ | ||
ResultType: "string", | ||
Result: tripperware.PrometheusQueryResult{ | ||
Result: &tripperware.PrometheusQueryResult_RawBytes{ | ||
RawBytes: []byte(`{"resultType":"string","result":[1,"2"]}`), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
prometheusResponse, err := createPrometheusQueryResponse(test.resp) | ||
require.NoError(t, err) | ||
|
||
b, err := proto.Marshal(prometheusResponse) | ||
require.NoError(t, err) | ||
|
||
var resp tripperware.PrometheusResponse | ||
err = proto.Unmarshal(b, &resp) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, test.expectedUnmarshalResp, resp) | ||
}) | ||
} | ||
} |
Oops, something went wrong.