From 456cf052739dda325a3ac5cba28e1572f2bb937b Mon Sep 17 00:00:00 2001 From: beorn7 Date: Thu, 23 Feb 2023 22:47:01 +0100 Subject: [PATCH] model: Remove json-iterator usage (again) This seemingly undoes #440, but all the json-iterator usage was actually pulled up into client_golang in https://github.com/prometheus/client_golang/pull/1225 . Detailed explanation there. In short, we would like to keep heavy dependencies like json-iterator out of prometheus/common/model since many importers of this package aren't even interested in the JSON marshaling. Signed-off-by: beorn7 --- go.mod | 3 - go.sum | 9 --- model/value_float.go | 27 +++----- model/value_histogram.go | 50 ++++++++------- model/value_marshal.go | 131 --------------------------------------- 5 files changed, 36 insertions(+), 184 deletions(-) delete mode 100644 model/value_marshal.go diff --git a/go.mod b/go.mod index dd4cdb28..5647d558 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ require ( github.com/alecthomas/kingpin/v2 v2.3.1 github.com/go-kit/log v0.2.1 github.com/golang/protobuf v1.5.2 - github.com/json-iterator/go v1.1.12 github.com/julienschmidt/httprouter v1.3.0 github.com/matttproud/golang_protobuf_extensions v1.0.4 github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f @@ -23,8 +22,6 @@ require ( github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/go-logfmt/logfmt v0.5.1 // indirect github.com/jpillora/backoff v1.0.0 // indirect - github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect - github.com/modern-go/reflect2 v1.0.2 // indirect github.com/prometheus/procfs v0.8.0 // indirect github.com/stretchr/testify v1.8.0 // indirect github.com/xhit/go-str2duration v1.2.0 // indirect diff --git a/go.sum b/go.sum index 07181d81..b059be28 100644 --- a/go.sum +++ b/go.sum @@ -22,11 +22,8 @@ github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= -github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= -github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= -github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= 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= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= -github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= -github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= 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 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= diff --git a/model/value_float.go b/model/value_float.go index 8b59571a..0f615a70 100644 --- a/model/value_float.go +++ b/model/value_float.go @@ -18,15 +18,8 @@ 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 @@ -78,18 +71,16 @@ type SamplePair struct { Value SampleValue } -// 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) { - return jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(s) + 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 } // UnmarshalJSON implements json.Unmarshaler. diff --git a/model/value_histogram.go b/model/value_histogram.go index cc221a88..54bb038c 100644 --- a/model/value_histogram.go +++ b/model/value_histogram.go @@ -18,16 +18,8 @@ import ( "fmt" "strconv" "strings" - "unsafe" - - jsoniter "github.com/json-iterator/go" ) -func init() { - jsoniter.RegisterTypeEncoderFunc("model.HistogramBucket", marshalHistogramBucketJSON, marshalJSONIsEmpty) - jsoniter.RegisterTypeEncoderFunc("model.SampleHistogramPair", marshalSampleHistogramPairJSON, marshalJSONIsEmpty) -} - type FloatString float64 func (v FloatString) String() string { @@ -57,10 +49,24 @@ type HistogramBucket struct { Count FloatString } -// marshalHistogramBucketJSON writes fmt.Sprintf("[%s,%s,%s,%s]", b.Boundaries, b.Lower, b.Upper, b.Count). -func marshalHistogramBucketJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) { - b := *((*HistogramBucket)(ptr)) - MarshalHistogramBucket(b, stream) +func (s HistogramBucket) MarshalJSON() ([]byte, error) { + b, err := json.Marshal(s.Boundaries) + if err != nil { + return nil, err + } + l, err := json.Marshal(s.Lower) + if err != nil { + return nil, err + } + u, err := json.Marshal(s.Upper) + if err != nil { + return nil, err + } + c, err := json.Marshal(s.Count) + if err != nil { + return nil, err + } + return []byte(fmt.Sprintf("[%s,%s,%s,%s]", b, l, u, c)), nil } func (s *HistogramBucket) UnmarshalJSON(buf []byte) error { @@ -133,21 +139,19 @@ type SampleHistogramPair struct { Histogram *SampleHistogram } -// marshalSampleHistogramPairJSON writes `[ts, "val"]`. -func marshalSampleHistogramPairJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) { - p := *((*SampleHistogramPair)(ptr)) - stream.WriteArrayStart() - MarshalTimestamp(int64(p.Timestamp), stream) - stream.WriteMore() - MarshalHistogram(*p.Histogram, stream) - stream.WriteArrayEnd() -} - func (s SampleHistogramPair) MarshalJSON() ([]byte, error) { if s.Histogram == nil { return nil, fmt.Errorf("histogram is nil") } - return jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(s) + t, err := json.Marshal(s.Timestamp) + if err != nil { + return nil, err + } + v, err := json.Marshal(s.Histogram) + if err != nil { + return nil, err + } + return []byte(fmt.Sprintf("[%s,%s]", t, v)), nil } func (s *SampleHistogramPair) UnmarshalJSON(buf []byte) error { diff --git a/model/value_marshal.go b/model/value_marshal.go deleted file mode 100644 index df193bcb..00000000 --- a/model/value_marshal.go +++ /dev/null @@ -1,131 +0,0 @@ -// Copyright 2013 The Prometheus Authors -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package model - -import ( - "math" - "strconv" - "unsafe" - - jsoniter "github.com/json-iterator/go" -) - -func marshalJSONIsEmpty(ptr unsafe.Pointer) bool { - return false -} - -// MarshalTimestamp marshals a point timestamp using the passed jsoniter stream. -func MarshalTimestamp(t int64, stream *jsoniter.Stream) { - // Write out the timestamp as a float divided by 1000. - // This is ~3x faster than converting to a float. - if t < 0 { - stream.WriteRaw(`-`) - t = -t - } - stream.WriteInt64(t / 1000) - fraction := t % 1000 - if fraction != 0 { - stream.WriteRaw(`.`) - if fraction < 100 { - stream.WriteRaw(`0`) - } - if fraction < 10 { - stream.WriteRaw(`0`) - } - stream.WriteInt64(fraction) - } -} - -// MarshalValue marshals a point value using the passed jsoniter stream. -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). - buf := stream.Buffer() - abs := math.Abs(v) - fmt := byte('f') - // Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right. - if abs != 0 { - if abs < 1e-6 || abs >= 1e21 { - fmt = 'e' - } - } - buf = strconv.AppendFloat(buf, v, fmt, -1, 64) - stream.SetBuffer(buf) - stream.WriteRaw(`"`) -} - -// MarshalHistogramBucket writes something like: [ 3, "-0.25", "0.25", "3"] -// See MarshalHistogram to understand what the numbers mean -func MarshalHistogramBucket(b HistogramBucket, stream *jsoniter.Stream) { - stream.WriteArrayStart() - stream.WriteInt32(b.Boundaries) - stream.WriteMore() - MarshalValue(float64(b.Lower), stream) - stream.WriteMore() - MarshalValue(float64(b.Upper), stream) - stream.WriteMore() - MarshalValue(float64(b.Count), stream) - stream.WriteArrayEnd() -} - -// MarshalHistogram writes something like: -// -// { -// "count": "42", -// "sum": "34593.34", -// "buckets": [ -// [ 3, "-0.25", "0.25", "3"], -// [ 0, "0.25", "0.5", "12"], -// [ 0, "0.5", "1", "21"], -// [ 0, "2", "4", "6"] -// ] -// } -// -// The 1st element in each bucket array determines if the boundaries are -// inclusive (AKA closed) or exclusive (AKA open): -// -// 0: lower exclusive, upper inclusive -// 1: lower inclusive, upper exclusive -// 2: both exclusive -// 3: both inclusive -// -// The 2nd and 3rd elements are the lower and upper boundary. The 4th element is -// the bucket count. -func MarshalHistogram(h SampleHistogram, stream *jsoniter.Stream) { - stream.WriteObjectStart() - stream.WriteObjectField(`count`) - MarshalValue(float64(h.Count), stream) - stream.WriteMore() - stream.WriteObjectField(`sum`) - MarshalValue(float64(h.Sum), stream) - - bucketFound := false - for _, bucket := range h.Buckets { - if bucket.Count == 0 { - continue // No need to expose empty buckets in JSON. - } - stream.WriteMore() - if !bucketFound { - stream.WriteObjectField(`buckets`) - stream.WriteArrayStart() - } - bucketFound = true - MarshalHistogramBucket(*bucket, stream) - } - if bucketFound { - stream.WriteArrayEnd() - } - stream.WriteObjectEnd() -}