Skip to content

Commit

Permalink
Merge pull request #567 from stapelberg/protodelim
Browse files Browse the repository at this point in the history
switch to protodelim package (which pbutil now calls)
  • Loading branch information
beorn7 authored Jan 13, 2024
2 parents 53f76e7 + 1143fec commit 0e201e4
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 19 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
1 change: 0 additions & 1 deletion sigv4/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions sigv4/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 0e201e4

Please sign in to comment.