diff --git a/model/textparse/openmetricsparse.go b/model/textparse/openmetricsparse.go index bbd39e3d5..38656e519 100644 --- a/model/textparse/openmetricsparse.go +++ b/model/textparse/openmetricsparse.go @@ -241,13 +241,13 @@ func (p *OpenMetricsParser) Next() (Entry, error) { case tEOF: return EntryInvalid, errors.New("data does not end with # EOF") case tHelp, tType, tUnit: - switch t := p.nextToken(); t { + switch t2 := p.nextToken(); t2 { case tMName: p.offsets = append(p.offsets, p.l.start, p.l.i) default: - return EntryInvalid, parseError("expected metric name after HELP", t) + return EntryInvalid, parseError("expected metric name after "+t.String(), t2) } - switch t := p.nextToken(); t { + switch t2 := p.nextToken(); t2 { case tText: if len(p.l.buf()) > 1 { p.text = p.l.buf()[1 : len(p.l.buf())-1] @@ -255,7 +255,7 @@ func (p *OpenMetricsParser) Next() (Entry, error) { p.text = []byte{} } default: - return EntryInvalid, parseError("expected text in HELP", t) + return EntryInvalid, fmt.Errorf("expected text in %s", t.String()) } switch t { case tType: diff --git a/model/textparse/openmetricsparse_test.go b/model/textparse/openmetricsparse_test.go index b460dd9f5..534dc6082 100644 --- a/model/textparse/openmetricsparse_test.go +++ b/model/textparse/openmetricsparse_test.go @@ -336,6 +336,14 @@ func TestOpenMetricsParseErrors(t *testing.T) { input: "# TYPE c counter\n#EOF\n", err: "\"INVALID\" \" \" is not a valid start token", }, + { + input: "# TYPE \n#EOF\n", + err: "expected metric name after TYPE, got \"INVALID\"", + }, + { + input: "# TYPE m\n#EOF\n", + err: "expected text in TYPE", + }, { input: "# UNIT metric suffix\n#EOF\n", err: "unit not a suffix of metric \"metric\"", @@ -348,9 +356,21 @@ func TestOpenMetricsParseErrors(t *testing.T) { input: "# UNIT m suffix\n#EOF\n", err: "unit not a suffix of metric \"m\"", }, + { + input: "# UNIT \n#EOF\n", + err: "expected metric name after UNIT, got \"INVALID\"", + }, + { + input: "# UNIT m\n#EOF\n", + err: "expected text in UNIT", + }, + { + input: "# HELP \n#EOF\n", + err: "expected metric name after HELP, got \"INVALID\"", + }, { input: "# HELP m\n#EOF\n", - err: "expected text in HELP, got \"INVALID\"", + err: "expected text in HELP", }, { input: "a\t1\n#EOF\n", diff --git a/model/textparse/promparse.go b/model/textparse/promparse.go index 8dc23a6b0..946cd2bc8 100644 --- a/model/textparse/promparse.go +++ b/model/textparse/promparse.go @@ -271,13 +271,13 @@ func (p *PromParser) Next() (Entry, error) { return p.Next() case tHelp, tType: - switch t := p.nextToken(); t { + switch t2 := p.nextToken(); t2 { case tMName: p.offsets = append(p.offsets, p.l.start, p.l.i) default: - return EntryInvalid, parseError("expected metric name after HELP", t) + return EntryInvalid, parseError("expected metric name after "+t.String(), t2) } - switch t := p.nextToken(); t { + switch t2 := p.nextToken(); t2 { case tText: if len(p.l.buf()) > 1 { p.text = p.l.buf()[1:] @@ -285,7 +285,7 @@ func (p *PromParser) Next() (Entry, error) { p.text = []byte{} } default: - return EntryInvalid, parseError("expected text in HELP", t) + return EntryInvalid, fmt.Errorf("expected text in %s", t.String()) } switch t { case tType: diff --git a/model/textparse/promparse_test.go b/model/textparse/promparse_test.go index b6e57108b..b9a037de1 100644 --- a/model/textparse/promparse_test.go +++ b/model/textparse/promparse_test.go @@ -270,6 +270,14 @@ func TestPromParseErrors(t *testing.T) { input: `{a="ok"} 1`, err: `"INVALID" is not a valid start token`, }, + { + input: "# TYPE #\n#EOF\n", + err: "expected metric name after TYPE, got \"INVALID\"", + }, + { + input: "# HELP #\n#EOF\n", + err: "expected metric name after HELP, got \"INVALID\"", + }, } for i, c := range cases {