-
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 872120e
Showing
24 changed files
with
972 additions
and
154 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,93 @@ | ||
package codec | ||
|
||
import ( | ||
"testing" | ||
|
||
jsoniter "github.com/json-iterator/go" | ||
"github.com/prometheus/prometheus/model/labels" | ||
"github.com/prometheus/prometheus/promql" | ||
"github.com/prometheus/prometheus/promql/parser" | ||
v1 "github.com/prometheus/prometheus/web/api/v1" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var ( | ||
json = jsoniter.Config{ | ||
EscapeHTML: false, // No HTML in our responses. | ||
SortMapKeys: true, | ||
ValidateJsonRawMessage: false, | ||
}.Froze() | ||
) | ||
|
||
func TestPrometheusResponse_JsonMarshal(t *testing.T) { | ||
var tests = []struct { | ||
name string | ||
resp *v1.Response | ||
expectedJson string | ||
}{ | ||
{ | ||
name: "scalar", | ||
resp: &v1.Response{ | ||
Status: "success", | ||
Data: &v1.QueryData{ | ||
ResultType: "scalar", | ||
Result: promql.Scalar{T: 1000, V: 2}, | ||
}, | ||
}, | ||
expectedJson: `{"status":"success","data":{"resultType":"scalar","result":[1,"2"]}}`, | ||
}, | ||
{ | ||
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, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedJson: `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"name":"value"},"value":[1.234,"5.67"]}]}}`, | ||
}, | ||
{ | ||
name: "matrix", | ||
resp: &v1.Response{ | ||
Status: "success", | ||
Data: &v1.QueryData{ | ||
ResultType: parser.ValueTypeMatrix, | ||
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}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedJson: `{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"name1":"value1"},"values":[[0.012,"3.4"],[0.056,"7.8"]]},{"metric":{"name2":"value2"},"values":[[0.012,"3.4"],[0.056,"7.8"]]}]}}`, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
prometheusResponse, err := createPrometheusQueryResponse(test.resp) | ||
require.NoError(t, err) | ||
|
||
b, err := json.Marshal(prometheusResponse) | ||
require.NoError(t, err) | ||
require.Equal(t, test.expectedJson, string(b)) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.