From 54dcd2d8cb0caf2aa8589a8ba9ddcba629e7e07c Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Tue, 12 Nov 2024 18:05:11 +0100 Subject: [PATCH] chore(parsers.prometheus): Do not warn about unknown format without headers (#16166) --- plugins/parsers/prometheus/parser.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/plugins/parsers/prometheus/parser.go b/plugins/parsers/prometheus/parser.go index 3d78c4f2d..e2d5a937c 100644 --- a/plugins/parsers/prometheus/parser.go +++ b/plugins/parsers/prometheus/parser.go @@ -33,16 +33,19 @@ func (p *Parser) SetDefaultTags(tags map[string]string) { func (p *Parser) Parse(data []byte) ([]telegraf.Metric, error) { // Determine the metric transport-type derived from the response header and // create a matching decoder. - format := expfmt.ResponseFormat(p.Header) - switch format.FormatType() { - case expfmt.TypeProtoText: - // 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")...) + format := expfmt.NewFormat(expfmt.TypeProtoCompact) + if len(p.Header) > 0 { + format = expfmt.ResponseFormat(p.Header) + switch format.FormatType() { + case expfmt.TypeProtoText: + // 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.TypeUnknown: + p.Log.Debugf("Unknown format %q... Trying to continue...", p.Header.Get("Content-Type")) } - case expfmt.TypeUnknown: - p.Log.Debugf("Unknown format %q... Trying to continue...", p.Header.Get("Content-Type")) } buf := bytes.NewBuffer(data) decoder := expfmt.NewDecoder(buf, format)