Skip to content

Commit

Permalink
Continue to use json-iterator to improve marshalling of SamplePair
Browse files Browse the repository at this point in the history
Signed-off-by: Jeanette Tan <[email protected]>
  • Loading branch information
zenador committed Feb 1, 2023
1 parent f71df30 commit 4e9e3f5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
28 changes: 18 additions & 10 deletions model/value_float.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ import (
"fmt"
"math"
"strconv"
"unsafe"

jsoniter "github.com/json-iterator/go"
)

func init() {
jsoniter.RegisterTypeEncoderFunc("model.SamplePair", marshalSamplePairJSON, marshalJSONIsEmpty)
}

var (
// ZeroSamplePair is the pseudo zero-value of SamplePair used to signal a
// non-existing sample pair. It is a SamplePair with timestamp Earliest and
Expand Down Expand Up @@ -71,17 +78,18 @@ type SamplePair struct {
Value SampleValue
}

// MarshalJSON implements json.Marshaler.
// marshalSamplePairJSON writes `[ts, "val"]`.
func marshalSamplePairJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
p := *((*SamplePair)(ptr))
stream.WriteArrayStart()
MarshalTimestamp(int64(p.Timestamp), stream)
stream.WriteMore()
MarshalValue(float64(p.Value), stream)
stream.WriteArrayEnd()
}

func (s SamplePair) MarshalJSON() ([]byte, error) {
t, err := json.Marshal(s.Timestamp)
if err != nil {
return nil, err
}
v, err := json.Marshal(s.Value)
if err != nil {
return nil, err
}
return []byte(fmt.Sprintf("[%s,%s]", t, v)), nil
return jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(s)
}

// UnmarshalJSON implements json.Unmarshaler.
Expand Down
12 changes: 2 additions & 10 deletions model/value_histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
)

func init() {
jsoniter.RegisterTypeEncoderFunc("model.HistogramBucket", marshalHistogramBucketJSON, marshalHistogramBucketJSONIsEmpty)
jsoniter.RegisterTypeEncoderFunc("model.SampleHistogramPair", marshalSampleHistogramPairJSON, marshalSampleHistogramPairJSONIsEmpty)
jsoniter.RegisterTypeEncoderFunc("model.HistogramBucket", marshalHistogramBucketJSON, marshalJSONIsEmpty)
jsoniter.RegisterTypeEncoderFunc("model.SampleHistogramPair", marshalSampleHistogramPairJSON, marshalJSONIsEmpty)
}

type FloatString float64
Expand Down Expand Up @@ -63,10 +63,6 @@ func marshalHistogramBucketJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
MarshalHistogramBucket(b, stream)
}

func marshalHistogramBucketJSONIsEmpty(ptr unsafe.Pointer) bool {
return false
}

func (s *HistogramBucket) UnmarshalJSON(buf []byte) error {
tmp := []interface{}{&s.Boundaries, &s.Lower, &s.Upper, &s.Count}
wantLen := len(tmp)
Expand Down Expand Up @@ -147,10 +143,6 @@ func marshalSampleHistogramPairJSON(ptr unsafe.Pointer, stream *jsoniter.Stream)
stream.WriteArrayEnd()
}

func marshalSampleHistogramPairJSONIsEmpty(ptr unsafe.Pointer) bool {
return false
}

func (s SampleHistogramPair) MarshalJSON() ([]byte, error) {
if s.Histogram == nil {
return nil, fmt.Errorf("histogram is nil")
Expand Down
20 changes: 12 additions & 8 deletions model/value_marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ package model
import (
"math"
"strconv"
"unsafe"

jsoniter "github.com/json-iterator/go"
)

func marshalJSONIsEmpty(ptr unsafe.Pointer) bool {
return false
}

// from https://github.com/prometheus/prometheus/blob/main/util/jsonutil/marshal.go
// MarshalTimestamp marshals a point timestamp using the passed jsoniter stream.
func MarshalTimestamp(t int64, stream *jsoniter.Stream) {
Expand All @@ -43,10 +48,9 @@ func MarshalTimestamp(t int64, stream *jsoniter.Stream) {
}
}

// adapted from https://github.com/prometheus/prometheus/blob/main/util/jsonutil/marshal.go
// from https://github.com/prometheus/prometheus/blob/main/util/jsonutil/marshal.go
// MarshalValue marshals a point value using the passed jsoniter stream.
func MarshalValue(f FloatString, stream *jsoniter.Stream) {
v := float64(f)
func MarshalValue(v float64, stream *jsoniter.Stream) {
stream.WriteRaw(`"`)
// Taken from https://github.com/json-iterator/go/blob/master/stream_float.go#L71 as a workaround
// to https://github.com/json-iterator/go/issues/365 (jsoniter, to follow json standard, doesn't allow inf/nan).
Expand All @@ -69,22 +73,22 @@ func MarshalHistogramBucket(b HistogramBucket, stream *jsoniter.Stream) {
stream.WriteArrayStart()
stream.WriteInt32(b.Boundaries)
stream.WriteMore()
MarshalValue(b.Lower, stream)
MarshalValue(float64(b.Lower), stream)
stream.WriteMore()
MarshalValue(b.Upper, stream)
MarshalValue(float64(b.Upper), stream)
stream.WriteMore()
MarshalValue(b.Count, stream)
MarshalValue(float64(b.Count), stream)
stream.WriteArrayEnd()
}

// adapted from https://github.com/prometheus/prometheus/blob/main/web/api/v1/api.go
func MarshalHistogram(h SampleHistogram, stream *jsoniter.Stream) {
stream.WriteObjectStart()
stream.WriteObjectField(`count`)
MarshalValue(h.Count, stream)
MarshalValue(float64(h.Count), stream)
stream.WriteMore()
stream.WriteObjectField(`sum`)
MarshalValue(h.Sum, stream)
MarshalValue(float64(h.Sum), stream)

bucketFound := false
for _, bucket := range h.Buckets {
Expand Down

0 comments on commit 4e9e3f5

Please sign in to comment.