Skip to content

Commit

Permalink
fix(parsers.prometheus): Do not touch input data for protocol-buffers (
Browse files Browse the repository at this point in the history
  • Loading branch information
srebhan authored Jan 22, 2024
1 parent 98cd149 commit 57021be
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions plugins/parsers/prometheus/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,20 @@ func (p *Parser) SetDefaultTags(tags map[string]string) {
}

func (p *Parser) Parse(data []byte) ([]telegraf.Metric, error) {
// Make sure we have a finishing newline but no trailing one
data = bytes.TrimPrefix(data, []byte("\n"))
if !bytes.HasSuffix(data, []byte("\n")) {
data = append(data, []byte("\n")...)
}
buf := bytes.NewBuffer(data)

// Determine the metric transport-type derived from the response header and
// create a matching decoder.
format := expfmt.ResponseFormat(p.Header)
if format == expfmt.FmtUnknown {
switch format {
case expfmt.FmtProtoText:
// Make sure we have a finishing newline but no trailing one
data = bytes.TrimPrefix(data, []byte("\n"))
if !bytes.HasSuffix(data, []byte("\n")) {
data = append(data, []byte("\n")...)
}
case expfmt.FmtUnknown:
p.Log.Warnf("Unknown format %q... Trying to continue...", p.Header.Get("Content-Type"))
}
buf := bytes.NewBuffer(data)
decoder := expfmt.NewDecoder(buf, format)

// Decode the input data into prometheus metrics
Expand Down

0 comments on commit 57021be

Please sign in to comment.