bugfix: API: encode empty Vector/Matrix as []
If the underlying data is `nil` the default encoding will render `"null"` which is not accepted by (some) Prometheus client libraries. Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
parent
00247b5d87
commit
5c8ffaa77c
|
@ -25,6 +25,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
jsoniter.RegisterTypeEncoderFunc("promql.Vector", unsafeMarshalVectorJSON, neverEmpty)
|
||||||
|
jsoniter.RegisterTypeEncoderFunc("promql.Matrix", unsafeMarshalMatrixJSON, neverEmpty)
|
||||||
jsoniter.RegisterTypeEncoderFunc("promql.Series", unsafeMarshalSeriesJSON, neverEmpty)
|
jsoniter.RegisterTypeEncoderFunc("promql.Series", unsafeMarshalSeriesJSON, neverEmpty)
|
||||||
jsoniter.RegisterTypeEncoderFunc("promql.Sample", unsafeMarshalSampleJSON, neverEmpty)
|
jsoniter.RegisterTypeEncoderFunc("promql.Sample", unsafeMarshalSampleJSON, neverEmpty)
|
||||||
jsoniter.RegisterTypeEncoderFunc("promql.FPoint", unsafeMarshalFPointJSON, neverEmpty)
|
jsoniter.RegisterTypeEncoderFunc("promql.FPoint", unsafeMarshalFPointJSON, neverEmpty)
|
||||||
|
@ -234,3 +236,23 @@ func labelsIsEmpty(ptr unsafe.Pointer) bool {
|
||||||
labelsPtr := (*labels.Labels)(ptr)
|
labelsPtr := (*labels.Labels)(ptr)
|
||||||
return labelsPtr.IsEmpty()
|
return labelsPtr.IsEmpty()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Marshal a Vector as `[sample,sample,...]` - empty Vector is `[]`.
|
||||||
|
func unsafeMarshalVectorJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
|
||||||
|
v := *((*promql.Vector)(ptr))
|
||||||
|
stream.WriteArrayStart()
|
||||||
|
for _, s := range v {
|
||||||
|
marshalSampleJSON(s, stream)
|
||||||
|
}
|
||||||
|
stream.WriteArrayEnd()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Marshal a Matrix as `[series,series,...]` - empty Matrix is `[]`.
|
||||||
|
func unsafeMarshalMatrixJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
|
||||||
|
m := *((*promql.Matrix)(ptr))
|
||||||
|
stream.WriteArrayStart()
|
||||||
|
for _, s := range m {
|
||||||
|
marshalSeriesJSON(s, stream)
|
||||||
|
}
|
||||||
|
stream.WriteArrayEnd()
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue