Skip to content

Commit 4e9e3f5

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

File tree

3 files changed

+32
-28
lines changed

3 files changed

+32
-28
lines changed

model/value_float.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,15 @@ import (
1818
"fmt"
1919
"math"
2020
"strconv"
21+
"unsafe"
22+
23+
jsoniter "github.com/json-iterator/go"
2124
)
2225

26+
func init() {
27+
jsoniter.RegisterTypeEncoderFunc("model.SamplePair", marshalSamplePairJSON, marshalJSONIsEmpty)
28+
}
29+
2330
var (
2431
// ZeroSamplePair is the pseudo zero-value of SamplePair used to signal a
2532
// non-existing sample pair. It is a SamplePair with timestamp Earliest and
@@ -71,17 +78,18 @@ type SamplePair struct {
7178
Value SampleValue
7279
}
7380

74-
// MarshalJSON implements json.Marshaler.
81+
// marshalSamplePairJSON writes `[ts, "val"]`.
82+
func marshalSamplePairJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
83+
p := *((*SamplePair)(ptr))
84+
stream.WriteArrayStart()
85+
MarshalTimestamp(int64(p.Timestamp), stream)
86+
stream.WriteMore()
87+
MarshalValue(float64(p.Value), stream)
88+
stream.WriteArrayEnd()
89+
}
90+
7591
func (s SamplePair) MarshalJSON() ([]byte, error) {
76-
t, err := json.Marshal(s.Timestamp)
77-
if err != nil {
78-
return nil, err
79-
}
80-
v, err := json.Marshal(s.Value)
81-
if err != nil {
82-
return nil, err
83-
}
84-
return []byte(fmt.Sprintf("[%s,%s]", t, v)), nil
92+
return jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(s)
8593
}
8694

8795
// UnmarshalJSON implements json.Unmarshaler.

model/value_histogram.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import (
2424
)
2525

2626
func init() {
27-
jsoniter.RegisterTypeEncoderFunc("model.HistogramBucket", marshalHistogramBucketJSON, marshalHistogramBucketJSONIsEmpty)
28-
jsoniter.RegisterTypeEncoderFunc("model.SampleHistogramPair", marshalSampleHistogramPairJSON, marshalSampleHistogramPairJSONIsEmpty)
27+
jsoniter.RegisterTypeEncoderFunc("model.HistogramBucket", marshalHistogramBucketJSON, marshalJSONIsEmpty)
28+
jsoniter.RegisterTypeEncoderFunc("model.SampleHistogramPair", marshalSampleHistogramPairJSON, marshalJSONIsEmpty)
2929
}
3030

3131
type FloatString float64
@@ -63,10 +63,6 @@ func marshalHistogramBucketJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
6363
MarshalHistogramBucket(b, stream)
6464
}
6565

66-
func marshalHistogramBucketJSONIsEmpty(ptr unsafe.Pointer) bool {
67-
return false
68-
}
69-
7066
func (s *HistogramBucket) UnmarshalJSON(buf []byte) error {
7167
tmp := []interface{}{&s.Boundaries, &s.Lower, &s.Upper, &s.Count}
7268
wantLen := len(tmp)
@@ -147,10 +143,6 @@ func marshalSampleHistogramPairJSON(ptr unsafe.Pointer, stream *jsoniter.Stream)
147143
stream.WriteArrayEnd()
148144
}
149145

150-
func marshalSampleHistogramPairJSONIsEmpty(ptr unsafe.Pointer) bool {
151-
return false
152-
}
153-
154146
func (s SampleHistogramPair) MarshalJSON() ([]byte, error) {
155147
if s.Histogram == nil {
156148
return nil, fmt.Errorf("histogram is nil")

model/value_marshal.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ package model
1616
import (
1717
"math"
1818
"strconv"
19+
"unsafe"
1920

2021
jsoniter "github.com/json-iterator/go"
2122
)
2223

24+
func marshalJSONIsEmpty(ptr unsafe.Pointer) bool {
25+
return false
26+
}
27+
2328
// from https://github.com/prometheus/prometheus/blob/main/util/jsonutil/marshal.go
2429
// MarshalTimestamp marshals a point timestamp using the passed jsoniter stream.
2530
func MarshalTimestamp(t int64, stream *jsoniter.Stream) {
@@ -43,10 +48,9 @@ func MarshalTimestamp(t int64, stream *jsoniter.Stream) {
4348
}
4449
}
4550

46-
// adapted from https://github.com/prometheus/prometheus/blob/main/util/jsonutil/marshal.go
51+
// from https://github.com/prometheus/prometheus/blob/main/util/jsonutil/marshal.go
4752
// MarshalValue marshals a point value using the passed jsoniter stream.
48-
func MarshalValue(f FloatString, stream *jsoniter.Stream) {
49-
v := float64(f)
53+
func MarshalValue(v float64, stream *jsoniter.Stream) {
5054
stream.WriteRaw(`"`)
5155
// Taken from https://github.com/json-iterator/go/blob/master/stream_float.go#L71 as a workaround
5256
// to https://github.com/json-iterator/go/issues/365 (jsoniter, to follow json standard, doesn't allow inf/nan).
@@ -69,22 +73,22 @@ func MarshalHistogramBucket(b HistogramBucket, stream *jsoniter.Stream) {
6973
stream.WriteArrayStart()
7074
stream.WriteInt32(b.Boundaries)
7175
stream.WriteMore()
72-
MarshalValue(b.Lower, stream)
76+
MarshalValue(float64(b.Lower), stream)
7377
stream.WriteMore()
74-
MarshalValue(b.Upper, stream)
78+
MarshalValue(float64(b.Upper), stream)
7579
stream.WriteMore()
76-
MarshalValue(b.Count, stream)
80+
MarshalValue(float64(b.Count), stream)
7781
stream.WriteArrayEnd()
7882
}
7983

8084
// adapted from https://github.com/prometheus/prometheus/blob/main/web/api/v1/api.go
8185
func MarshalHistogram(h SampleHistogram, stream *jsoniter.Stream) {
8286
stream.WriteObjectStart()
8387
stream.WriteObjectField(`count`)
84-
MarshalValue(h.Count, stream)
88+
MarshalValue(float64(h.Count), stream)
8589
stream.WriteMore()
8690
stream.WriteObjectField(`sum`)
87-
MarshalValue(h.Sum, stream)
91+
MarshalValue(float64(h.Sum), stream)
8892

8993
bucketFound := false
9094
for _, bucket := range h.Buckets {

0 commit comments

Comments
 (0)