bugfix: add missing comma in vector/matrix array (#14047)

* bugfix: Add missing comma when encoding JSON results in web API

---------

Signed-off-by: Amir Vejahat <amir.vejahat.av@gmail.com>
Co-authored-by: Arthur Silva Sens <arthur.sens@coralogix.com>
This commit is contained in:
AVejahat 2024-05-03 19:07:49 +02:00 committed by GitHub
parent f170a014f3
commit 56fd8a1e4a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 42 additions and 2 deletions

View file

@ -241,8 +241,11 @@ func labelsIsEmpty(ptr unsafe.Pointer) bool {
func unsafeMarshalVectorJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
v := *((*promql.Vector)(ptr))
stream.WriteArrayStart()
for _, s := range v {
for i, s := range v {
marshalSampleJSON(s, stream)
if i != len(v)-1 {
stream.WriteMore()
}
}
stream.WriteArrayEnd()
}
@ -251,8 +254,11 @@ func unsafeMarshalVectorJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
func unsafeMarshalMatrixJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
m := *((*promql.Matrix)(ptr))
stream.WriteArrayStart()
for _, s := range m {
for i, s := range m {
marshalSeriesJSON(s, stream)
if i != len(m)-1 {
stream.WriteMore()
}
}
stream.WriteArrayEnd()
}

View file

@ -29,6 +29,40 @@ func TestJsonCodec_Encode(t *testing.T) {
response interface{}
expected string
}{
{
response: &QueryData{
ResultType: parser.ValueTypeVector,
Result: promql.Vector{
promql.Sample{
Metric: labels.FromStrings("__name__", "foo"),
T: 1000,
F: 1,
},
promql.Sample{
Metric: labels.FromStrings("__name__", "bar"),
T: 2000,
F: 2,
},
},
},
expected: `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"foo"},"value":[1,"1"]},{"metric":{"__name__":"bar"},"value":[2,"2"]}]}}`,
},
{
response: &QueryData{
ResultType: parser.ValueTypeMatrix,
Result: promql.Matrix{
promql.Series{
Metric: labels.FromStrings("__name__", "foo"),
Floats: []promql.FPoint{{F: 1, T: 1000}},
},
promql.Series{
Metric: labels.FromStrings("__name__", "bar"),
Floats: []promql.FPoint{{F: 2, T: 2000}},
},
},
},
expected: `{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"__name__":"foo"},"values":[[1,"1"]]},{"metric":{"__name__":"bar"},"values":[[2,"2"]]}]}}`,
},
{
response: &QueryData{
ResultType: parser.ValueTypeMatrix,