fix(parsers.prometheus): Do not touch input data for protocol-buffers (#14606)

This commit is contained in:
Sven Rebhan 2024-01-22 20:25:03 +01:00 committed by GitHub
parent 98cd14987a
commit 57021be82c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 8 deletions

View File

@ -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