From 1143fec8e222297675791067bc9654c153a6a1f0 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Fri, 12 Jan 2024 10:42:08 +0100 Subject: [PATCH] switch to protodelim package (which pbutil now calls) Go Protobuf v1.30.0 includes the new protodelim package. github.com/matttproud/golang_protobuf_extensions/v2/pbutil is, at this point, just a wrapper around the protodelim package: https://github.com/matttproud/golang_protobuf_extensions/commit/7c0096c86977a6345402445d1048134557c72820 https://matttproud.com/blog/posts/retiring-pbutil.html Therefore, this change calls protodelim directly, allowing us to get rid of one module dependency :) Signed-off-by: Michael Stapelberg --- expfmt/bench_test.go | 25 ++++++++++++++++++------- expfmt/decode.go | 10 ++++++---- expfmt/encode.go | 4 ++-- go.mod | 1 - go.sum | 2 -- sigv4/go.mod | 1 - sigv4/go.sum | 2 -- 7 files changed, 26 insertions(+), 19 deletions(-) diff --git a/expfmt/bench_test.go b/expfmt/bench_test.go index dea89aee..6857f9a1 100644 --- a/expfmt/bench_test.go +++ b/expfmt/bench_test.go @@ -14,6 +14,7 @@ package expfmt import ( + "bufio" "bytes" "compress/gzip" "errors" @@ -21,7 +22,7 @@ import ( "os" "testing" - "github.com/matttproud/golang_protobuf_extensions/v2/pbutil" + "google.golang.org/protobuf/encoding/protodelim" dto "github.com/prometheus/client_model/go" ) @@ -98,10 +99,13 @@ func BenchmarkParseProto(b *testing.B) { for i := 0; i < b.N; i++ { family := &dto.MetricFamily{} - in := bytes.NewReader(data) + in := bufio.NewReader(bytes.NewReader(data)) + unmarshaler := protodelim.UnmarshalOptions{ + MaxSize: -1, + } for { family.Reset() - if _, err := pbutil.ReadDelimited(in, family); err != nil { + if err := unmarshaler.UnmarshalFrom(in, family); err != nil { if errors.Is(err, io.EOF) { break } @@ -123,13 +127,17 @@ func BenchmarkParseProtoGzip(b *testing.B) { for i := 0; i < b.N; i++ { family := &dto.MetricFamily{} - in, err := gzip.NewReader(bytes.NewReader(data)) + gz, err := gzip.NewReader(bytes.NewReader(data)) if err != nil { b.Fatal(err) } + in := bufio.NewReader(gz) + unmarshaler := protodelim.UnmarshalOptions{ + MaxSize: -1, + } for { family.Reset() - if _, err := pbutil.ReadDelimited(in, family); err != nil { + if err := unmarshaler.UnmarshalFrom(in, family); err != nil { if errors.Is(err, io.EOF) { break } @@ -153,10 +161,13 @@ func BenchmarkParseProtoMap(b *testing.B) { for i := 0; i < b.N; i++ { families := map[string]*dto.MetricFamily{} - in := bytes.NewReader(data) + in := bufio.NewReader(bytes.NewReader(data)) + unmarshaler := protodelim.UnmarshalOptions{ + MaxSize: -1, + } for { family := &dto.MetricFamily{} - if _, err := pbutil.ReadDelimited(in, family); err != nil { + if err := unmarshaler.UnmarshalFrom(in, family); err != nil { if errors.Is(err, io.EOF) { break } diff --git a/expfmt/decode.go b/expfmt/decode.go index 5090a167..a909b171 100644 --- a/expfmt/decode.go +++ b/expfmt/decode.go @@ -14,6 +14,7 @@ package expfmt import ( + "bufio" "fmt" "io" "math" @@ -21,8 +22,7 @@ import ( "net/http" dto "github.com/prometheus/client_model/go" - - "github.com/matttproud/golang_protobuf_extensions/v2/pbutil" + "google.golang.org/protobuf/encoding/protodelim" "github.com/prometheus/common/model" ) @@ -87,8 +87,10 @@ type protoDecoder struct { // Decode implements the Decoder interface. func (d *protoDecoder) Decode(v *dto.MetricFamily) error { - _, err := pbutil.ReadDelimited(d.r, v) - if err != nil { + opts := protodelim.UnmarshalOptions{ + MaxSize: -1, + } + if err := opts.UnmarshalFrom(bufio.NewReader(d.r), v); err != nil { return err } if !model.IsValidMetricName(model.LabelValue(v.GetName())) { diff --git a/expfmt/encode.go b/expfmt/encode.go index 318531f5..02b7a5e8 100644 --- a/expfmt/encode.go +++ b/expfmt/encode.go @@ -18,7 +18,7 @@ import ( "io" "net/http" - "github.com/matttproud/golang_protobuf_extensions/v2/pbutil" + "google.golang.org/protobuf/encoding/protodelim" "google.golang.org/protobuf/encoding/prototext" "github.com/prometheus/common/internal/bitbucket.org/ww/goautoneg" @@ -121,7 +121,7 @@ func NewEncoder(w io.Writer, format Format) Encoder { case FmtProtoDelim: return encoderCloser{ encode: func(v *dto.MetricFamily) error { - _, err := pbutil.WriteDelimited(w, v) + _, err := protodelim.MarshalTo(w, v) return err }, close: func() error { return nil }, diff --git a/go.mod b/go.mod index e76adb1a..c751f448 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ require ( github.com/alecthomas/kingpin/v2 v2.4.0 github.com/go-kit/log v0.2.1 github.com/julienschmidt/httprouter v1.3.0 - github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f github.com/prometheus/client_golang v1.18.0 github.com/prometheus/client_model v0.5.0 diff --git a/go.sum b/go.sum index 4559f570..20ff8679 100644 --- a/go.sum +++ b/go.sum @@ -27,8 +27,6 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 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/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg= -github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k= 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= diff --git a/sigv4/go.mod b/sigv4/go.mod index b585bfa1..46470a51 100644 --- a/sigv4/go.mod +++ b/sigv4/go.mod @@ -20,7 +20,6 @@ require ( github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/jpillora/backoff v1.0.0 // indirect github.com/kr/text v0.2.0 // indirect - github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_model v0.5.0 // indirect diff --git a/sigv4/go.sum b/sigv4/go.sum index 68c4639a..5a539828 100644 --- a/sigv4/go.sum +++ b/sigv4/go.sum @@ -23,8 +23,6 @@ github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg= -github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k= 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=