Refactor plugin scrape test

Replace retired[0] pbutil library with protodelim package.

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

Signed-off-by: SuperQ <superq@gmail.com>
This commit is contained in:
SuperQ
2024-03-10 13:26:21 +01:00
parent 218ceb3737
commit bcd0f5694f
3 changed files with 12 additions and 6 deletions

View File

@@ -22,15 +22,17 @@
package test
import (
"bufio"
"bytes"
"fmt"
"io"
"mime"
"net/http"
"strconv"
"github.com/matttproud/golang_protobuf_extensions/pbutil"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
"google.golang.org/protobuf/encoding/protodelim"
)
type (
@@ -234,9 +236,17 @@ func fetchMetricFamilies(url string, ch chan<- *dto.MetricFamily) {
if err == nil && mediatype == "application/vnd.google.protobuf" &&
params["encoding"] == "delimited" &&
params["proto"] == "io.prometheus.client.MetricFamily" {
unmarshaler := protodelim.UnmarshalOptions{
MaxSize: -1,
}
for {
mf := &dto.MetricFamily{}
if _, err = pbutil.ReadDelimited(resp.Body, mf); err != nil {
body, err := io.ReadAll(resp.Body)
if err != nil {
return
}
in := bufio.NewReader(bytes.NewReader(body))
if err = unmarshaler.UnmarshalFrom(in, mf); err != nil {
if err == io.EOF {
break
}