Skip to content

Commit

Permalink
switch to protodelim package (which pbutil now calls)
Browse files Browse the repository at this point in the history
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:

matttproud/golang_protobuf_extensions@7c0096c

https://matttproud.com/blog/posts/retiring-pbutil.html

Therefore, this change calls protodelim directly,
allowing us to get rid of one module dependency :)
  • Loading branch information
stapelberg committed Jan 12, 2024
1 parent 21a37ca commit a56bfc1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
25 changes: 18 additions & 7 deletions expfmt/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
package expfmt

import (
"bufio"
"bytes"
"compress/gzip"
"errors"
"io"
"os"
"testing"

"github.com/matttproud/golang_protobuf_extensions/v2/pbutil"
"google.golang.org/protobuf/encoding/protodelim"

dto "github.com/prometheus/client_model/go"
)
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
10 changes: 6 additions & 4 deletions expfmt/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
package expfmt

import (
"bufio"
"fmt"
"io"
"math"
"mime"
"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"
)
Expand Down Expand Up @@ -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())) {
Expand Down
4 changes: 2 additions & 2 deletions expfmt/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 },
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit a56bfc1

Please sign in to comment.