Reduce the cpu/memory used by the graphite parser (#5841)

This commit is contained in:
Charlie Vieth 2019-06-14 15:45:07 -04:00 committed by Daniel Nelson
parent 1f2cb85354
commit b35beb2fba
1 changed files with 24 additions and 31 deletions

View File

@ -1,10 +1,9 @@
package graphite package graphite
import ( import (
"bufio"
"bytes" "bytes"
"errors"
"fmt" "fmt"
"io"
"math" "math"
"strconv" "strconv"
"strings" "strings"
@ -63,42 +62,36 @@ func NewGraphiteParser(
func (p *GraphiteParser) Parse(buf []byte) ([]telegraf.Metric, error) { func (p *GraphiteParser) Parse(buf []byte) ([]telegraf.Metric, error) {
// parse even if the buffer begins with a newline // parse even if the buffer begins with a newline
buf = bytes.TrimPrefix(buf, []byte("\n")) if len(buf) != 0 && buf[0] == '\n' {
// add newline to end if not exists: buf = buf[1:]
if len(buf) > 0 && !bytes.HasSuffix(buf, []byte("\n")) {
buf = append(buf, []byte("\n")...)
} }
metrics := make([]telegraf.Metric, 0) var metrics []telegraf.Metric
var errs []string
var errStr string
buffer := bytes.NewBuffer(buf)
reader := bufio.NewReader(buffer)
for { for {
// Read up to the next newline. n := bytes.IndexByte(buf, '\n')
buf, err := reader.ReadBytes('\n') var line []byte
if err == io.EOF { if n >= 0 {
break line = bytes.TrimSpace(buf[:n:n])
} else {
line = bytes.TrimSpace(buf) // last line
} }
if err != nil && err != io.EOF { if len(line) != 0 {
return metrics, err metric, err := p.ParseLine(string(line))
}
// Trim the buffer, even though there should be no padding
line := strings.TrimSpace(string(buf))
if line == "" {
continue
}
metric, err := p.ParseLine(line)
if err == nil { if err == nil {
metrics = append(metrics, metric) metrics = append(metrics, metric)
} else { } else {
errStr += err.Error() + "\n" errs = append(errs, err.Error())
} }
} }
if n < 0 {
if errStr != "" { break
return metrics, fmt.Errorf(strings.TrimSpace(errStr)) }
buf = buf[n+1:]
}
if len(errs) != 0 {
return metrics, errors.New(strings.Join(errs, "\n"))
} }
return metrics, nil return metrics, nil
} }