Browse Source

OpenMetrics: force input to end with # EOF (#6505)

Fixes #1169

Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>
pull/6515/head
Julien Pivotto 5 years ago committed by Brian Brazil
parent
commit
9398b9ac1b
  1. 12
      pkg/textparse/openmetricsparse.go
  2. 105
      pkg/textparse/openmetricsparse_test.go

12
pkg/textparse/openmetricsparse.go

@ -90,6 +90,7 @@ type OpenMetricsParser struct {
hasTS bool hasTS bool
start int start int
offsets []int offsets []int
prev token
eOffsets []int eOffsets []int
exemplar []byte exemplar []byte
@ -232,13 +233,18 @@ func (p *OpenMetricsParser) Next() (Entry, error) {
p.exemplarVal = 0 p.exemplarVal = 0
p.hasExemplarTs = false p.hasExemplarTs = false
switch t := p.nextToken(); t { t := p.nextToken()
defer func() { p.prev = t }()
switch t {
case tEofWord: case tEofWord:
if t := p.nextToken(); t != tEOF { if t := p.nextToken(); t != tEOF {
return EntryInvalid, errors.New("unexpected data after # EOF") return EntryInvalid, errors.New("unexpected data after # EOF")
} }
return EntryInvalid, io.EOF return EntryInvalid, io.EOF
case tEOF: case tEOF:
if p.prev != tEofWord {
return EntryInvalid, errors.New("data does not end with # EOF")
}
return EntryInvalid, io.EOF return EntryInvalid, io.EOF
case tHelp, tType, tUnit: case tHelp, tType, tUnit:
switch t := p.nextToken(); t { switch t := p.nextToken(); t {
@ -322,7 +328,7 @@ func (p *OpenMetricsParser) Next() (Entry, error) {
p.hasTS = false p.hasTS = false
switch t2 := p.nextToken(); t2 { switch t2 := p.nextToken(); t2 {
case tEOF: case tEOF:
return EntryInvalid, io.EOF return EntryInvalid, errors.New("data does not end with # EOF")
case tLinebreak: case tLinebreak:
break break
case tComment: case tComment:
@ -382,7 +388,7 @@ func (p *OpenMetricsParser) parseComment() error {
p.hasExemplarTs = false p.hasExemplarTs = false
switch t2 := p.nextToken(); t2 { switch t2 := p.nextToken(); t2 {
case tEOF: case tEOF:
return io.EOF return errors.New("data does not end with # EOF")
case tLinebreak: case tLinebreak:
break break
case tTimestamp: case tTimestamp:

105
pkg/textparse/openmetricsparse_test.go

@ -1,4 +1,4 @@
// Copyright 2017 The OMetheus Authors // Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
// You may obtain a copy of the License at // You may obtain a copy of the License at
@ -271,72 +271,103 @@ func TestOpenMetricsParseErrors(t *testing.T) {
input string input string
err string err string
}{ }{
// Happy cases. EOF is returned by the parser at the end of valid
// data.
{ {
input: "", input: "# EOF",
err: "EOF", err: "EOF",
}, },
{ {
input: "a", input: "# EOF\n",
err: "expected value after metric, got \"EOF\"", err: "EOF",
},
// Unhappy cases.
{
input: "",
err: "data does not end with # EOF",
}, },
{ {
input: "\n", input: "\n",
err: "\"INVALID\" \"\\n\" is not a valid start token", err: "\"INVALID\" \"\\n\" is not a valid start token",
}, },
{ {
input: " a 1\n", input: "metric",
err: "expected value after metric, got \"EOF\"",
},
{
input: "metric 1",
err: "data does not end with # EOF",
},
{
input: "metric 1\n",
err: "data does not end with # EOF",
},
{
input: "metric_total 1 # {aa=\"bb\"} 4",
err: "data does not end with # EOF",
},
{
input: "a\n#EOF\n",
err: "expected value after metric, got \"INVALID\"",
},
{
input: "\n\n#EOF\n",
err: "\"INVALID\" \"\\n\" is not a valid start token",
},
{
input: " a 1\n#EOF\n",
err: "\"INVALID\" \" \" is not a valid start token", err: "\"INVALID\" \" \" is not a valid start token",
}, },
{ {
input: "9\n", input: "9\n#EOF\n",
err: "\"INVALID\" \"9\" is not a valid start token", err: "\"INVALID\" \"9\" is not a valid start token",
}, },
{ {
input: "# TYPE u untyped\n", input: "# TYPE u untyped\n#EOF\n",
err: "invalid metric type \"untyped\"", err: "invalid metric type \"untyped\"",
}, },
{ {
input: "# TYPE c counter \n", input: "# TYPE c counter \n#EOF\n",
err: "invalid metric type \"counter \"", err: "invalid metric type \"counter \"",
}, },
{ {
input: "# TYPE c counter\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: "# UNIT metric suffix\n", input: "# UNIT metric suffix\n#EOF\n",
err: "unit not a suffix of metric \"metric\"", err: "unit not a suffix of metric \"metric\"",
}, },
{ {
input: "# UNIT metricsuffix suffix\n", input: "# UNIT metricsuffix suffix\n#EOF\n",
err: "unit not a suffix of metric \"metricsuffix\"", err: "unit not a suffix of metric \"metricsuffix\"",
}, },
{ {
input: "# UNIT m suffix\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: "# HELP m\n", input: "# HELP m\n#EOF\n",
err: "expected text in HELP, got \"INVALID\"", err: "expected text in HELP, got \"INVALID\"",
}, },
{ {
input: "a\t1\n", input: "a\t1\n#EOF\n",
err: "expected value after metric, got \"INVALID\"", err: "expected value after metric, got \"INVALID\"",
}, },
{ {
input: "a 1\t2\n", input: "a 1\t2\n#EOF\n",
err: "strconv.ParseFloat: parsing \"1\\t2\": invalid syntax", err: "strconv.ParseFloat: parsing \"1\\t2\": invalid syntax",
}, },
{ {
input: "a 1 2 \n", input: "a 1 2 \n#EOF\n",
err: "expected next entry after timestamp, got \"INVALID\"", err: "expected next entry after timestamp, got \"INVALID\"",
}, },
{ {
input: "a 1 2 #\n", input: "a 1 2 #\n#EOF\n",
err: "expected next entry after timestamp, got \"TIMESTAMP\"", err: "expected next entry after timestamp, got \"TIMESTAMP\"",
}, },
{ {
input: "a 1 1z\n", input: "a 1 1z\n#EOF\n",
err: "strconv.ParseFloat: parsing \"1z\": invalid syntax", err: "strconv.ParseFloat: parsing \"1z\": invalid syntax",
}, },
{ {
@ -364,39 +395,39 @@ func TestOpenMetricsParseErrors(t *testing.T) {
err: "invalid metric type \" counter\"", err: "invalid metric type \" counter\"",
}, },
{ {
input: "a 1 1 1\n", input: "a 1 1 1\n# EOF\n",
err: "expected next entry after timestamp, got \"TIMESTAMP\"", err: "expected next entry after timestamp, got \"TIMESTAMP\"",
}, },
{ {
input: "a{b='c'} 1\n", input: "a{b='c'} 1\n# EOF\n",
err: "expected label value, got \"INVALID\"", err: "expected label value, got \"INVALID\"",
}, },
{ {
input: "a{b=\"c\",} 1\n", input: "a{b=\"c\",} 1\n# EOF\n",
err: "expected label name, got \"BCLOSE\"", err: "expected label name, got \"BCLOSE\"",
}, },
{ {
input: "a{,b=\"c\"} 1\n", input: "a{,b=\"c\"} 1\n# EOF\n",
err: "expected label name or left brace, got \"COMMA\"", err: "expected label name or left brace, got \"COMMA\"",
}, },
{ {
input: "a{b=\"c\"d=\"e\"} 1\n", input: "a{b=\"c\"d=\"e\"} 1\n# EOF\n",
err: "expected comma, got \"LNAME\"", err: "expected comma, got \"LNAME\"",
}, },
{ {
input: "a{b=\"c\",,d=\"e\"} 1\n", input: "a{b=\"c\",,d=\"e\"} 1\n# EOF\n",
err: "expected label name, got \"COMMA\"", err: "expected label name, got \"COMMA\"",
}, },
{ {
input: "a{b=\n", input: "a{b=\n# EOF\n",
err: "expected label value, got \"INVALID\"", err: "expected label value, got \"INVALID\"",
}, },
{ {
input: "a{\xff=\"foo\"} 1\n", input: "a{\xff=\"foo\"} 1\n# EOF\n",
err: "expected label name or left brace, got \"INVALID\"", err: "expected label name or left brace, got \"INVALID\"",
}, },
{ {
input: "a{b=\"\xff\"} 1\n", input: "a{b=\"\xff\"} 1\n# EOF\n",
err: "invalid UTF-8 label value", err: "invalid UTF-8 label value",
}, },
{ {
@ -404,33 +435,37 @@ func TestOpenMetricsParseErrors(t *testing.T) {
err: "strconv.ParseFloat: parsing \"true\": invalid syntax", err: "strconv.ParseFloat: parsing \"true\": invalid syntax",
}, },
{ {
input: "something_weird{problem=\"", input: "something_weird{problem=\"\n# EOF\n",
err: "expected label value, got \"INVALID\"", err: "expected label value, got \"INVALID\"",
}, },
{ {
input: "empty_label_name{=\"\"} 0", input: "empty_label_name{=\"\"} 0\n# EOF\n",
err: "expected label name or left brace, got \"EQUAL\"", err: "expected label name or left brace, got \"EQUAL\"",
}, },
{ {
input: "foo 1_2\n", input: "foo 1_2\n\n# EOF\n",
err: "unsupported character in float", err: "unsupported character in float",
}, },
{ {
input: "foo 0x1p-3\n", input: "foo 0x1p-3\n\n# EOF\n",
err: "unsupported character in float", err: "unsupported character in float",
}, },
{ {
input: "foo 0x1P-3\n", input: "foo 0x1P-3\n\n# EOF\n",
err: "unsupported character in float", err: "unsupported character in float",
}, },
{ {
input: "foo 0 1_2\n", input: "foo 0 1_2\n\n# EOF\n",
err: "unsupported character in float", err: "unsupported character in float",
}, },
{ {
input: "custom_metric_total 1 # {aa=bb}", input: "custom_metric_total 1 # {aa=bb}\n# EOF\n",
err: "expected label value, got \"INVALID\"", err: "expected label value, got \"INVALID\"",
}, },
{
input: "custom_metric_total 1 # {aa=\"bb\"}\n# EOF\n",
err: "expected value after exemplar labels, got \"INVALID\"",
},
{ {
input: `custom_metric_total 1 # {aa="bb"}`, input: `custom_metric_total 1 # {aa="bb"}`,
err: "expected value after exemplar labels, got \"EOF\"", err: "expected value after exemplar labels, got \"EOF\"",
@ -471,7 +506,7 @@ func TestOpenMetricsParseErrors(t *testing.T) {
for err == nil { for err == nil {
_, err = p.Next() _, err = p.Next()
} }
testutil.Equals(t, c.err, err.Error(), "test %d", i) testutil.Equals(t, c.err, err.Error(), "test %d: %s", i, c.input)
} }
} }

Loading…
Cancel
Save