Skip to content

Commit 94c865c

Browse files
authored
Merge pull request #453 from prometheus/beorn7/model
model: Remove json-iterator usage (again)
2 parents ab87968 + 456cf05 commit 94c865c

File tree

5 files changed

+36
-184
lines changed

5 files changed

+36
-184
lines changed

go.mod

-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require (
66
github.com/alecthomas/kingpin/v2 v2.3.1
77
github.com/go-kit/log v0.2.1
88
github.com/golang/protobuf v1.5.2
9-
github.com/json-iterator/go v1.1.12
109
github.com/julienschmidt/httprouter v1.3.0
1110
github.com/matttproud/golang_protobuf_extensions v1.0.4
1211
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f
@@ -23,8 +22,6 @@ require (
2322
github.com/cespare/xxhash/v2 v2.1.2 // indirect
2423
github.com/go-logfmt/logfmt v0.5.1 // indirect
2524
github.com/jpillora/backoff v1.0.0 // indirect
26-
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
27-
github.com/modern-go/reflect2 v1.0.2 // indirect
2825
github.com/prometheus/procfs v0.8.0 // indirect
2926
github.com/stretchr/testify v1.8.0 // indirect
3027
github.com/xhit/go-str2duration v1.2.0 // indirect

go.sum

-9
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@ github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw
2222
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
2323
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
2424
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
25-
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
2625
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
2726
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
28-
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
29-
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
3027
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
3128
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
3229
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
@@ -36,11 +33,6 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
3633
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
3734
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
3835
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
39-
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
40-
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
41-
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
42-
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
43-
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
4436
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU=
4537
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
4638
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -54,7 +46,6 @@ github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0ua
5446
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5547
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
5648
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
57-
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
5849
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
5950
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
6051
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=

model/value_float.go

+9-18
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,8 @@ import (
1818
"fmt"
1919
"math"
2020
"strconv"
21-
"unsafe"
22-
23-
jsoniter "github.com/json-iterator/go"
2421
)
2522

26-
func init() {
27-
jsoniter.RegisterTypeEncoderFunc("model.SamplePair", marshalSamplePairJSON, marshalJSONIsEmpty)
28-
}
29-
3023
var (
3124
// ZeroSamplePair is the pseudo zero-value of SamplePair used to signal a
3225
// non-existing sample pair. It is a SamplePair with timestamp Earliest and
@@ -78,18 +71,16 @@ type SamplePair struct {
7871
Value SampleValue
7972
}
8073

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-
9174
func (s SamplePair) MarshalJSON() ([]byte, error) {
92-
return jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(s)
75+
t, err := json.Marshal(s.Timestamp)
76+
if err != nil {
77+
return nil, err
78+
}
79+
v, err := json.Marshal(s.Value)
80+
if err != nil {
81+
return nil, err
82+
}
83+
return []byte(fmt.Sprintf("[%s,%s]", t, v)), nil
9384
}
9485

9586
// UnmarshalJSON implements json.Unmarshaler.

model/value_histogram.go

+27-23
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,8 @@ import (
1818
"fmt"
1919
"strconv"
2020
"strings"
21-
"unsafe"
22-
23-
jsoniter "github.com/json-iterator/go"
2421
)
2522

26-
func init() {
27-
jsoniter.RegisterTypeEncoderFunc("model.HistogramBucket", marshalHistogramBucketJSON, marshalJSONIsEmpty)
28-
jsoniter.RegisterTypeEncoderFunc("model.SampleHistogramPair", marshalSampleHistogramPairJSON, marshalJSONIsEmpty)
29-
}
30-
3123
type FloatString float64
3224

3325
func (v FloatString) String() string {
@@ -57,10 +49,24 @@ type HistogramBucket struct {
5749
Count FloatString
5850
}
5951

60-
// marshalHistogramBucketJSON writes fmt.Sprintf("[%s,%s,%s,%s]", b.Boundaries, b.Lower, b.Upper, b.Count).
61-
func marshalHistogramBucketJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
62-
b := *((*HistogramBucket)(ptr))
63-
MarshalHistogramBucket(b, stream)
52+
func (s HistogramBucket) MarshalJSON() ([]byte, error) {
53+
b, err := json.Marshal(s.Boundaries)
54+
if err != nil {
55+
return nil, err
56+
}
57+
l, err := json.Marshal(s.Lower)
58+
if err != nil {
59+
return nil, err
60+
}
61+
u, err := json.Marshal(s.Upper)
62+
if err != nil {
63+
return nil, err
64+
}
65+
c, err := json.Marshal(s.Count)
66+
if err != nil {
67+
return nil, err
68+
}
69+
return []byte(fmt.Sprintf("[%s,%s,%s,%s]", b, l, u, c)), nil
6470
}
6571

6672
func (s *HistogramBucket) UnmarshalJSON(buf []byte) error {
@@ -133,21 +139,19 @@ type SampleHistogramPair struct {
133139
Histogram *SampleHistogram
134140
}
135141

136-
// marshalSampleHistogramPairJSON writes `[ts, "val"]`.
137-
func marshalSampleHistogramPairJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
138-
p := *((*SampleHistogramPair)(ptr))
139-
stream.WriteArrayStart()
140-
MarshalTimestamp(int64(p.Timestamp), stream)
141-
stream.WriteMore()
142-
MarshalHistogram(*p.Histogram, stream)
143-
stream.WriteArrayEnd()
144-
}
145-
146142
func (s SampleHistogramPair) MarshalJSON() ([]byte, error) {
147143
if s.Histogram == nil {
148144
return nil, fmt.Errorf("histogram is nil")
149145
}
150-
return jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(s)
146+
t, err := json.Marshal(s.Timestamp)
147+
if err != nil {
148+
return nil, err
149+
}
150+
v, err := json.Marshal(s.Histogram)
151+
if err != nil {
152+
return nil, err
153+
}
154+
return []byte(fmt.Sprintf("[%s,%s]", t, v)), nil
151155
}
152156

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

model/value_marshal.go

-131
This file was deleted.

0 commit comments

Comments
 (0)