Skip to content

Commit

Permalink
Merge pull request #443 from abursavich/expfmt-decode
Browse files Browse the repository at this point in the history
expfmt: only call TextParser.TextToMetricFamilies once from textDecoder
  • Loading branch information
roidelapluie authored Feb 20, 2023
2 parents f9c1994 + 5be97a0 commit d888bfa
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions expfmt/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,32 +115,28 @@ func (d *protoDecoder) Decode(v *dto.MetricFamily) error {
// textDecoder implements the Decoder interface for the text protocol.
type textDecoder struct {
r io.Reader
p TextParser
fams []*dto.MetricFamily
fams map[string]*dto.MetricFamily
err error
}

// Decode implements the Decoder interface.
func (d *textDecoder) Decode(v *dto.MetricFamily) error {
// TODO(fabxc): Wrap this as a line reader to make streaming safer.
if len(d.fams) == 0 {
// No cached metric families, read everything and parse metrics.
fams, err := d.p.TextToMetricFamilies(d.r)
if err != nil {
return err
}
if len(fams) == 0 {
return io.EOF
}
d.fams = make([]*dto.MetricFamily, 0, len(fams))
for _, f := range fams {
d.fams = append(d.fams, f)
if d.err == nil {
// Read all metrics in one shot.
var p TextParser
d.fams, d.err = p.TextToMetricFamilies(d.r)
// If we don't get an error, store io.EOF for the end.
if d.err == nil {
d.err = io.EOF
}
}

*v = *d.fams[0]
d.fams = d.fams[1:]

return nil
// Pick off one MetricFamily per Decode until there's nothing left.
for key, fam := range d.fams {
*v = *fam
delete(d.fams, key)
return nil
}
return d.err
}

// SampleDecoder wraps a Decoder to extract samples from the metric families
Expand Down

0 comments on commit d888bfa

Please sign in to comment.