Skip to content

Commit

Permalink
Reduce the cpu/memory used by the graphite parser (influxdata#5841)
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie Vieth authored and idohalevi committed Sep 23, 2020
1 parent c2edd26 commit a650c2a
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions plugins/parsers/graphite/parser.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package graphite

import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"math"
"strconv"
"strings"
Expand Down Expand Up @@ -63,42 +62,36 @@ func NewGraphiteParser(

func (p *GraphiteParser) Parse(buf []byte) ([]telegraf.Metric, error) {
// parse even if the buffer begins with a newline
buf = bytes.TrimPrefix(buf, []byte("\n"))
// add newline to end if not exists:
if len(buf) > 0 && !bytes.HasSuffix(buf, []byte("\n")) {
buf = append(buf, []byte("\n")...)
if len(buf) != 0 && buf[0] == '\n' {
buf = buf[1:]
}

metrics := make([]telegraf.Metric, 0)
var metrics []telegraf.Metric
var errs []string

var errStr string
buffer := bytes.NewBuffer(buf)
reader := bufio.NewReader(buffer)
for {
// Read up to the next newline.
buf, err := reader.ReadBytes('\n')
if err == io.EOF {
break
}
if err != nil && err != io.EOF {
return metrics, err
n := bytes.IndexByte(buf, '\n')
var line []byte
if n >= 0 {
line = bytes.TrimSpace(buf[:n:n])
} else {
line = bytes.TrimSpace(buf) // last line
}

// Trim the buffer, even though there should be no padding
line := strings.TrimSpace(string(buf))
if line == "" {
continue
if len(line) != 0 {
metric, err := p.ParseLine(string(line))
if err == nil {
metrics = append(metrics, metric)
} else {
errs = append(errs, err.Error())
}
}
metric, err := p.ParseLine(line)
if err == nil {
metrics = append(metrics, metric)
} else {
errStr += err.Error() + "\n"
if n < 0 {
break
}
buf = buf[n+1:]
}

if errStr != "" {
return metrics, fmt.Errorf(strings.TrimSpace(errStr))
if len(errs) != 0 {
return metrics, errors.New(strings.Join(errs, "\n"))
}
return metrics, nil
}
Expand Down

0 comments on commit a650c2a

Please sign in to comment.