Specify type in metadata parser errors (#10269)

This commit is contained in:
Levi Harrison 2022-03-08 08:04:11 -05:00 committed by GitHub
parent 51a023dc24
commit a9e2ffb717
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 9 deletions

View file

@ -241,13 +241,13 @@ func (p *OpenMetricsParser) Next() (Entry, error) {
case tEOF: case tEOF:
return EntryInvalid, errors.New("data does not end with # EOF") return EntryInvalid, errors.New("data does not end with # EOF")
case tHelp, tType, tUnit: case tHelp, tType, tUnit:
switch t := p.nextToken(); t { switch t2 := p.nextToken(); t2 {
case tMName: case tMName:
p.offsets = append(p.offsets, p.l.start, p.l.i) p.offsets = append(p.offsets, p.l.start, p.l.i)
default: 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: case tText:
if len(p.l.buf()) > 1 { if len(p.l.buf()) > 1 {
p.text = p.l.buf()[1 : 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{} p.text = []byte{}
} }
default: default:
return EntryInvalid, parseError("expected text in HELP", t) return EntryInvalid, fmt.Errorf("expected text in %s", t.String())
} }
switch t { switch t {
case tType: case tType:

View file

@ -336,6 +336,14 @@ func TestOpenMetricsParseErrors(t *testing.T) {
input: "# TYPE c counter\n#EOF\n", input: "# TYPE c counter\n#EOF\n",
err: "\"INVALID\" \" \" is not a valid start token", 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", input: "# UNIT metric suffix\n#EOF\n",
err: "unit not a suffix of metric \"metric\"", err: "unit not a suffix of metric \"metric\"",
@ -348,9 +356,21 @@ func TestOpenMetricsParseErrors(t *testing.T) {
input: "# UNIT m suffix\n#EOF\n", input: "# UNIT m suffix\n#EOF\n",
err: "unit not a suffix of metric \"m\"", 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", input: "# HELP m\n#EOF\n",
err: "expected text in HELP, got \"INVALID\"", err: "expected text in HELP",
}, },
{ {
input: "a\t1\n#EOF\n", input: "a\t1\n#EOF\n",

View file

@ -271,13 +271,13 @@ func (p *PromParser) Next() (Entry, error) {
return p.Next() return p.Next()
case tHelp, tType: case tHelp, tType:
switch t := p.nextToken(); t { switch t2 := p.nextToken(); t2 {
case tMName: case tMName:
p.offsets = append(p.offsets, p.l.start, p.l.i) p.offsets = append(p.offsets, p.l.start, p.l.i)
default: 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: case tText:
if len(p.l.buf()) > 1 { if len(p.l.buf()) > 1 {
p.text = p.l.buf()[1:] p.text = p.l.buf()[1:]
@ -285,7 +285,7 @@ func (p *PromParser) Next() (Entry, error) {
p.text = []byte{} p.text = []byte{}
} }
default: default:
return EntryInvalid, parseError("expected text in HELP", t) return EntryInvalid, fmt.Errorf("expected text in %s", t.String())
} }
switch t { switch t {
case tType: case tType:

View file

@ -270,6 +270,14 @@ func TestPromParseErrors(t *testing.T) {
input: `{a="ok"} 1`, input: `{a="ok"} 1`,
err: `"INVALID" is not a valid start token`, 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 { for i, c := range cases {