Skip to content

Commit f71df30

Browse files
committed
Continue to use json-iterator to improve marshalling of SampleHistogramPair
Signed-off-by: Jeanette Tan <[email protected]>
1 parent eda7de1 commit f71df30

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

model/value_histogram.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
func init() {
2727
jsoniter.RegisterTypeEncoderFunc("model.HistogramBucket", marshalHistogramBucketJSON, marshalHistogramBucketJSONIsEmpty)
28+
jsoniter.RegisterTypeEncoderFunc("model.SampleHistogramPair", marshalSampleHistogramPairJSON, marshalSampleHistogramPairJSONIsEmpty)
2829
}
2930

3031
type FloatString float64
@@ -136,20 +137,25 @@ type SampleHistogramPair struct {
136137
Histogram *SampleHistogram
137138
}
138139

140+
// marshalSampleHistogramPairJSON writes `[ts, "val"]`.
141+
func marshalSampleHistogramPairJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
142+
p := *((*SampleHistogramPair)(ptr))
143+
stream.WriteArrayStart()
144+
MarshalTimestamp(int64(p.Timestamp), stream)
145+
stream.WriteMore()
146+
MarshalHistogram(*p.Histogram, stream)
147+
stream.WriteArrayEnd()
148+
}
149+
150+
func marshalSampleHistogramPairJSONIsEmpty(ptr unsafe.Pointer) bool {
151+
return false
152+
}
153+
139154
func (s SampleHistogramPair) MarshalJSON() ([]byte, error) {
140-
jsoni := jsoniter.ConfigCompatibleWithStandardLibrary
141-
t, err := jsoni.Marshal(s.Timestamp)
142-
if err != nil {
143-
return nil, err
144-
}
145155
if s.Histogram == nil {
146156
return nil, fmt.Errorf("histogram is nil")
147157
}
148-
v, err := jsoni.Marshal(s.Histogram)
149-
if err != nil {
150-
return nil, err
151-
}
152-
return []byte(fmt.Sprintf("[%s,%s]", t, v)), nil
158+
return jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(s)
153159
}
154160

155161
func (s *SampleHistogramPair) UnmarshalJSON(buf []byte) error {

model/value_marshal.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,31 @@ func MarshalHistogramBucket(b HistogramBucket, stream *jsoniter.Stream) {
7676
MarshalValue(b.Count, stream)
7777
stream.WriteArrayEnd()
7878
}
79+
80+
// adapted from https://github.com/prometheus/prometheus/blob/main/web/api/v1/api.go
81+
func MarshalHistogram(h SampleHistogram, stream *jsoniter.Stream) {
82+
stream.WriteObjectStart()
83+
stream.WriteObjectField(`count`)
84+
MarshalValue(h.Count, stream)
85+
stream.WriteMore()
86+
stream.WriteObjectField(`sum`)
87+
MarshalValue(h.Sum, stream)
88+
89+
bucketFound := false
90+
for _, bucket := range h.Buckets {
91+
if bucket.Count == 0 {
92+
continue // No need to expose empty buckets in JSON.
93+
}
94+
stream.WriteMore()
95+
if !bucketFound {
96+
stream.WriteObjectField(`buckets`)
97+
stream.WriteArrayStart()
98+
}
99+
bucketFound = true
100+
MarshalHistogramBucket(*bucket, stream)
101+
}
102+
if bucketFound {
103+
stream.WriteArrayEnd()
104+
}
105+
stream.WriteObjectEnd()
106+
}

0 commit comments

Comments
 (0)