From ec048aa8b0474601a0899119d1239ca27c7633f3 Mon Sep 17 00:00:00 2001 From: Banana Duck Date: Tue, 5 Aug 2025 23:23:38 +0300 Subject: [PATCH] expfmt: throw error when metric's name not provided Signed-off-by: Banana Duck --- expfmt/text_parse.go | 5 +++-- expfmt/text_parse_test.go | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/expfmt/text_parse.go b/expfmt/text_parse.go index 4067978a1..181927f5a 100644 --- a/expfmt/text_parse.go +++ b/expfmt/text_parse.go @@ -99,7 +99,7 @@ type TextParser struct { // Summaries and histograms are rather special beasts. You would probably not // use them in the simple text format anyway. This method can deal with // summaries and histograms if they are presented in exactly the way the -// text.Create function creates them. +// text. Create function creates them. // // This method must not be called concurrently. If you want to parse different // input concurrently, instantiate a separate Parser for each goroutine. @@ -142,6 +142,7 @@ func (p *TextParser) reset(in io.Reader) { p.currentQuantile = math.NaN() p.currentBucket = math.NaN() p.currentMF = nil + p.currentMetric = nil } // startOfLine represents the state where the next byte read from p.buf is the @@ -417,7 +418,7 @@ func (p *TextParser) startLabelValue() stateFn { return p.startLabelName case '}': - if p.currentMF == nil { + if p.currentMF == nil || p.currentMetric == nil { p.parseError("invalid metric name") return nil } diff --git a/expfmt/text_parse_test.go b/expfmt/text_parse_test.go index fac60ba6f..64c64f411 100644 --- a/expfmt/text_parse_test.go +++ b/expfmt/text_parse_test.go @@ -934,6 +934,16 @@ line"="bla"} 3.14 `, err: `text format parsing error in line 2: label name "new" contains unescaped new-line`, }, + // 40: Metric's name missing. + { + in: ` +# HELP backupmonitor_size The size of the given backup. +# TYPE backupmonitor_size counter +{host="local", dir="alpha"} 1834194837 +{host="remote", dir="beta"} 133638016 +`, + err: `text format parsing error in line 4: invalid metric name`, + }, } for i, scenario := range scenarios { _, err := parser.TextToMetricFamilies(strings.NewReader(scenario.in))