mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-24 05:04:05 -08:00
Add jsoniter encoder for Labels
Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
parent
54e1046616
commit
bb528d4a55
|
@ -19,6 +19,7 @@ import (
|
|||
jsoniter "github.com/json-iterator/go"
|
||||
|
||||
"github.com/prometheus/prometheus/model/exemplar"
|
||||
"github.com/prometheus/prometheus/model/labels"
|
||||
"github.com/prometheus/prometheus/promql"
|
||||
"github.com/prometheus/prometheus/util/jsonutil"
|
||||
)
|
||||
|
@ -29,6 +30,7 @@ func init() {
|
|||
jsoniter.RegisterTypeEncoderFunc("promql.FPoint", marshalFPointJSON, marshalPointJSONIsEmpty)
|
||||
jsoniter.RegisterTypeEncoderFunc("promql.HPoint", marshalHPointJSON, marshalPointJSONIsEmpty)
|
||||
jsoniter.RegisterTypeEncoderFunc("exemplar.Exemplar", marshalExemplarJSON, marshalExemplarJSONEmpty)
|
||||
jsoniter.RegisterTypeEncoderFunc("labels.Labels", unsafeMarshalLabelsJSON, labelsIsEmpty)
|
||||
}
|
||||
|
||||
// JSONCodec is a Codec that encodes API responses as JSON.
|
||||
|
@ -217,3 +219,28 @@ func marshalExemplarJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
|
|||
func marshalExemplarJSONEmpty(unsafe.Pointer) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func unsafeMarshalLabelsJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
|
||||
labelsPtr := (*labels.Labels)(ptr)
|
||||
marshalLabelsJSON(*labelsPtr, stream)
|
||||
}
|
||||
|
||||
func marshalLabelsJSON(lbls labels.Labels, stream *jsoniter.Stream) {
|
||||
stream.WriteObjectStart()
|
||||
i := 0
|
||||
lbls.Range(func(v labels.Label) {
|
||||
if i != 0 {
|
||||
stream.WriteMore()
|
||||
}
|
||||
i++
|
||||
stream.WriteString(v.Name)
|
||||
stream.WriteRaw(`:`)
|
||||
stream.WriteString(v.Value)
|
||||
})
|
||||
stream.WriteObjectEnd()
|
||||
}
|
||||
|
||||
func labelsIsEmpty(ptr unsafe.Pointer) bool {
|
||||
labelsPtr := (*labels.Labels)(ptr)
|
||||
return labelsPtr.IsEmpty()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue