cleanup and fix tests

Signed-off-by: Owen Williams <owen.williams@grafana.com>
This commit is contained in:
Owen Williams 2023-12-08 11:03:17 -05:00
parent 64b3665994
commit 21cf437e8b
4 changed files with 47 additions and 49 deletions

View file

@ -226,12 +226,11 @@ func (p *OpenMetricsParser) nextToken() token {
}
func (p *OpenMetricsParser) parseError(exp string, got token) error {
e := p.l.i + 80
e := p.l.i + 1
if len(p.l.b) < e {
e = len(p.l.b)
}
start := int(math.Max(0, float64(p.start-80)))
return fmt.Errorf("%s, got %q (%q) while parsing: %q", exp, p.l.b[p.l.start:e], got, p.l.b[start:e])
return fmt.Errorf("%s, got %q (%q) while parsing: %q", exp, p.l.b[p.l.start:e], got, p.l.b[p.start:e])
}
// Next advances the parser to the next sample. It returns false if no
@ -255,7 +254,6 @@ func (p *OpenMetricsParser) Next() (Entry, error) {
case tEOF:
return EntryInvalid, errors.New("data does not end with # EOF")
case tHelp, tType, tUnit:
tStart := p.l.start
switch t2 := p.nextToken(); t2 {
case tMName:
mStart := p.l.start
@ -266,7 +264,7 @@ func (p *OpenMetricsParser) Next() (Entry, error) {
}
p.offsets = append(p.offsets, mStart, mEnd)
default:
return EntryInvalid, p.parseError("expected metric name after "+t.String()+" "+string(p.l.b), t2)
return EntryInvalid, p.parseError("expected metric name after "+t.String(), t2)
}
switch t2 := p.nextToken(); t2 {
case tText:
@ -276,11 +274,7 @@ func (p *OpenMetricsParser) Next() (Entry, error) {
p.text = []byte{}
}
default:
end := tStart + 40
if end >= len(p.l.b) {
end = len(p.l.b) - 1
}
return EntryInvalid, fmt.Errorf("expected text in %s: got %v (%v)", t.String(), t2.String(), string(p.l.b[tStart:end]))
return EntryInvalid, fmt.Errorf("expected text in %s", t.String())
}
switch t {
case tType:
@ -468,6 +462,10 @@ func (p *OpenMetricsParser) parseLVals(offsets []int) ([]int, error) {
// parseMetricSuffix parses the end of the line after the metric name and
// labels. It starts parsing with the provided token.
func (p *OpenMetricsParser) parseMetricSuffix(t token) (Entry, error) {
if p.offsets[0] == -1 {
return EntryInvalid, fmt.Errorf("metric name not set while parsing: %q", p.l.b[p.start:p.l.i])
}
var err error
p.val, err = p.getFloatValue(t, "metric")
if err != nil {

View file

@ -466,19 +466,19 @@ func TestOpenMetricsParseErrors(t *testing.T) {
},
{
input: "a\n#EOF\n",
err: "expected value after metric, got \"\\n#EOF\\n\" (\"INVALID\") while parsing: \"a\\n#EOF\\n\"",
err: "expected value after metric, got \"\\n\" (\"INVALID\") while parsing: \"a\\n\"",
},
{
input: "\n\n#EOF\n",
err: "expected a valid start token, got \"\\n\\n#EOF\\n\" (\"INVALID\") while parsing: \"\\n\\n#EOF\\n\"",
err: "expected a valid start token, got \"\\n\" (\"INVALID\") while parsing: \"\\n\"",
},
{
input: " a 1\n#EOF\n",
err: "expected a valid start token, got \" a 1\\n#EOF\\n\" (\"INVALID\") while parsing: \" a 1\\n#EOF\\n\"",
err: "expected a valid start token, got \" \" (\"INVALID\") while parsing: \" \"",
},
{
input: "9\n#EOF\n",
err: "expected a valid start token, got \"9\\n#EOF\\n\" (\"INVALID\") while parsing: \"9\\n#EOF\\n\"",
err: "expected a valid start token, got \"9\" (\"INVALID\") while parsing: \"9\"",
},
{
input: "# TYPE u untyped\n#EOF\n",
@ -490,15 +490,15 @@ func TestOpenMetricsParseErrors(t *testing.T) {
},
{
input: "# TYPE c counter\n#EOF\n",
err: "expected a valid start token, got \"# TYPE c counter\\n#EOF\\n\" (\"INVALID\") while parsing: \"# TYPE c counter\\n#EOF\\n\"",
err: "expected a valid start token, got \"# \" (\"INVALID\") while parsing: \"# \"",
},
{
input: "# TYPE \n#EOF\n",
err: "expected metric name after TYPE # TYPE \n#EOF\n, got \"\\n#EOF\\n\" (\"INVALID\") while parsing: \"# TYPE \\n#EOF\\n\"",
err: "expected metric name after TYPE, got \"\\n\" (\"INVALID\") while parsing: \"# TYPE \\n\"",
},
{
input: "# TYPE m\n#EOF\n",
err: "expected text in TYPE: got INVALID (# TYPE m\n#EOF)",
err: "expected text in TYPE",
},
{
input: "# UNIT metric suffix\n#EOF\n",
@ -514,23 +514,23 @@ func TestOpenMetricsParseErrors(t *testing.T) {
},
{
input: "# UNIT \n#EOF\n",
err: "expected metric name after UNIT # UNIT \n#EOF\n, got \"\\n#EOF\\n\" (\"INVALID\") while parsing: \"# UNIT \\n#EOF\\n\"",
err: "expected metric name after UNIT, got \"\\n\" (\"INVALID\") while parsing: \"# UNIT \\n\"",
},
{
input: "# UNIT m\n#EOF\n",
err: "expected text in UNIT: got INVALID (# UNIT m\n#EOF)",
err: "expected text in UNIT",
},
{
input: "# HELP \n#EOF\n",
err: "expected metric name after HELP # HELP \n#EOF\n, got \"\\n#EOF\\n\" (\"INVALID\") while parsing: \"# HELP \\n#EOF\\n\"",
err: "expected metric name after HELP, got \"\\n\" (\"INVALID\") while parsing: \"# HELP \\n\"",
},
{
input: "# HELP m\n#EOF\n",
err: "expected text in HELP: got INVALID (# HELP m\n#EOF)",
err: "expected text in HELP",
},
{
input: "a\t1\n#EOF\n",
err: "expected value after metric, got \"\\t1\\n#EOF\\n\" (\"INVALID\") while parsing: \"a\\t1\\n#EOF\\n\"",
err: "expected value after metric, got \"\\t\" (\"INVALID\") while parsing: \"a\\t\"",
},
{
input: "a 1\t2\n#EOF\n",
@ -538,11 +538,11 @@ func TestOpenMetricsParseErrors(t *testing.T) {
},
{
input: "a 1 2 \n#EOF\n",
err: "expected next entry after timestamp, got \" \\n#EOF\\n\" (\"INVALID\") while parsing: \"a 1 2 \\n#EOF\\n\"",
err: "expected next entry after timestamp, got \" \\n\" (\"INVALID\") while parsing: \"a 1 2 \\n\"",
},
{
input: "a 1 2 #\n#EOF\n",
err: "expected next entry after timestamp, got \" #\\n#EOF\\n\" (\"TIMESTAMP\") while parsing: \"a 1 2 #\\n#EOF\\n\"",
err: "expected next entry after timestamp, got \" #\\n\" (\"TIMESTAMP\") while parsing: \"a 1 2 #\\n\"",
},
{
input: "a 1 1z\n#EOF\n",
@ -550,7 +550,7 @@ func TestOpenMetricsParseErrors(t *testing.T) {
},
{
input: " # EOF\n",
err: "expected a valid start token, got \" # EOF\\n\" (\"INVALID\") while parsing: \" # EOF\\n\"",
err: "expected a valid start token, got \" \" (\"INVALID\") while parsing: \" \"",
},
{
input: "# EOF\na 1",
@ -566,7 +566,7 @@ func TestOpenMetricsParseErrors(t *testing.T) {
},
{
input: "#\tTYPE c counter\n",
err: "expected a valid start token, got \"#\\tTYPE c counter\\n\" (\"INVALID\") while parsing: \"#\\tTYPE c counter\\n\"",
err: "expected a valid start token, got \"#\\t\" (\"INVALID\") while parsing: \"#\\t\"",
},
{
input: "# TYPE c counter\n",
@ -574,31 +574,31 @@ func TestOpenMetricsParseErrors(t *testing.T) {
},
{
input: "a 1 1 1\n# EOF\n",
err: "expected next entry after timestamp, got \" 1\\n# EOF\\n\" (\"TIMESTAMP\") while parsing: \"a 1 1 1\\n# EOF\\n\"",
err: "expected next entry after timestamp, got \" 1\\n\" (\"TIMESTAMP\") while parsing: \"a 1 1 1\\n\"",
},
{
input: "a{b='c'} 1\n# EOF\n",
err: "expected label value, got \"'c'} 1\\n# EOF\\n\" (\"INVALID\") while parsing: \"a{b='c'} 1\\n# EOF\\n\"",
err: "expected label value, got \"'\" (\"INVALID\") while parsing: \"a{b='\"",
},
{
input: "a{,b=\"c\"} 1\n# EOF\n",
err: "expected label name, got \",b=\\\"c\\\"} 1\\n# EOF\\n\" (\"COMMA\") while parsing: \"a{,b=\\\"c\\\"} 1\\n# EOF\\n\"",
err: "expected label name, got \",b\" (\"COMMA\") while parsing: \"a{,b\"",
},
{
input: "a{b=\"c\"d=\"e\"} 1\n# EOF\n",
err: "expected comma or brace close, got \"d=\\\"e\\\"} 1\\n# EOF\\n\" (\"LNAME\") while parsing: \"a{b=\\\"c\\\"d=\\\"e\\\"} 1\\n# EOF\\n\"",
err: "expected comma or brace close, got \"d=\" (\"LNAME\") while parsing: \"a{b=\\\"c\\\"d=\"",
},
{
input: "a{b=\"c\",,d=\"e\"} 1\n# EOF\n",
err: "expected label name, got \",d=\\\"e\\\"} 1\\n# EOF\\n\" (\"COMMA\") while parsing: \"a{b=\\\"c\\\",,d=\\\"e\\\"} 1\\n# EOF\\n\"",
err: "expected label name, got \",d\" (\"COMMA\") while parsing: \"a{b=\\\"c\\\",,d\"",
},
{
input: "a{b=\n# EOF\n",
err: "expected label value, got \"\\n# EOF\\n\" (\"INVALID\") while parsing: \"a{b=\\n# EOF\\n\"",
err: "expected label value, got \"\\n\" (\"INVALID\") while parsing: \"a{b=\\n\"",
},
{
input: "a{\xff=\"foo\"} 1\n# EOF\n",
err: "expected label name, got \"\\xff=\\\"foo\\\"} 1\\n# EOF\\n\" (\"INVALID\") while parsing: \"a{\\xff=\\\"foo\\\"} 1\\n# EOF\\n\"",
err: "expected label name, got \"\\xff\" (\"INVALID\") while parsing: \"a{\\xff\"",
},
{
input: "a{b=\"\xff\"} 1\n# EOF\n",
@ -610,11 +610,11 @@ func TestOpenMetricsParseErrors(t *testing.T) {
},
{
input: "something_weird{problem=\"\n# EOF\n",
err: "expected label value, got \"\\\"\\n# EOF\\n\" (\"INVALID\") while parsing: \"something_weird{problem=\\\"\\n# EOF\\n\"",
err: "expected label value, got \"\\\"\\n\" (\"INVALID\") while parsing: \"something_weird{problem=\\\"\\n\"",
},
{
input: "empty_label_name{=\"\"} 0\n# EOF\n",
err: "expected label name, got \"=\\\"\\\"} 0\\n# EOF\\n\" (\"EQUAL\") while parsing: \"empty_label_name{=\\\"\\\"} 0\\n# EOF\\n\"",
err: "expected label name, got \"=\\\"\" (\"EQUAL\") while parsing: \"empty_label_name{=\\\"\"",
},
{
input: "foo 1_2\n\n# EOF\n",
@ -634,11 +634,11 @@ func TestOpenMetricsParseErrors(t *testing.T) {
},
{
input: "custom_metric_total 1 # {aa=bb}\n# EOF\n",
err: "expected label value, got \"bb}\\n# EOF\\n\" (\"INVALID\") while parsing: \"custom_metric_total 1 # {aa=bb}\\n# EOF\\n\"",
err: "expected label value, got \"b\" (\"INVALID\") while parsing: \"custom_metric_total 1 # {aa=b\"",
},
{
input: "custom_metric_total 1 # {aa=\"bb\"}\n# EOF\n",
err: "expected value after exemplar labels, got \"\\n# EOF\\n\" (\"INVALID\") while parsing: \"custom_metric_total 1 # {aa=\\\"bb\\\"}\\n# EOF\\n\"",
err: "expected value after exemplar labels, got \"\\n\" (\"INVALID\") while parsing: \"custom_metric_total 1 # {aa=\\\"bb\\\"}\\n\"",
},
{
input: `custom_metric_total 1 # {aa="bb"}`,
@ -646,7 +646,7 @@ func TestOpenMetricsParseErrors(t *testing.T) {
},
{
input: `custom_metric_total 1 # {aa="bb",,cc="dd"} 1`,
err: "expected label name, got \",cc=\\\"dd\\\"} 1\" (\"COMMA\") while parsing: \"custom_metric_total 1 # {aa=\\\"bb\\\",,cc=\\\"dd\\\"} 1\"",
err: "expected label name, got \",c\" (\"COMMA\") while parsing: \"custom_metric_total 1 # {aa=\\\"bb\\\",,c\"",
},
{
input: `custom_metric_total 1 # {aa="bb"} 1_2`,
@ -666,11 +666,11 @@ func TestOpenMetricsParseErrors(t *testing.T) {
},
{
input: `custom_metric_total 1 # {aa=\"\xff\"} 9.0`,
err: "expected label value, got \"\\\\\\\"\\\\xff\\\\\\\"} 9.0\" (\"INVALID\") while parsing: \"custom_metric_total 1 # {aa=\\\\\\\"\\\\xff\\\\\\\"} 9.0\"",
err: "expected label value, got \"\\\\\" (\"INVALID\") while parsing: \"custom_metric_total 1 # {aa=\\\\\"",
},
{
input: `{b="c",} 1`,
err: "data does not end with # EOF",
err: "metric name not set while parsing: \"{b=\\\"c\\\",} 1\"",
},
{
input: `a 1 NaN`,
@ -731,7 +731,7 @@ func TestOMNullByteHandling(t *testing.T) {
},
{
input: "a{b=\x00\"ssss\"} 1\n# EOF\n",
err: "expected label value, got \"\\x00\\\"ssss\\\"} 1\\n# EOF\\n\" (\"INVALID\") while parsing: \"a{b=\\x00\\\"ssss\\\"} 1\\n# EOF\\n\"",
err: "expected label value, got \"\\x00\" (\"INVALID\") while parsing: \"a{b=\\x00\"",
},
{
input: "a{b=\"\x00",
@ -739,11 +739,11 @@ func TestOMNullByteHandling(t *testing.T) {
},
{
input: "a{b\x00=\"hiih\"} 1",
err: "expected equal, got \"\\x00=\\\"hiih\\\"}\\t1\" (\"INVALID\") while parsing: \"a{b\\x00=\\\"hiih\\\"}\\t1\"",
err: "expected equal, got \"\\x00\" (\"INVALID\") while parsing: \"a{b\\x00\"",
},
{
input: "a\x00{b=\"ddd\"} 1",
err: "expected value after metric, got \"\\x00{b=\\\"ddd\\\"} 1\" (\"INVALID\") while parsing: \"a\\x00{b=\\\"ddd\\\"} 1\"",
err: "expected value after metric, got \"\\x00\" (\"INVALID\") while parsing: \"a\\x00\"",
},
{
input: "#",
@ -755,11 +755,11 @@ func TestOMNullByteHandling(t *testing.T) {
},
{
input: "custom_metric_total 1 # {b=\x00\"ssss\"} 1\n",
err: "expected label value, got \"\\x00\\\"ssss\\\"} 1\\n\" (\"INVALID\") while parsing: \"custom_metric_total 1 # {b=\\x00\\\"ssss\\\"} 1\\n\"",
err: "expected label value, got \"\\x00\" (\"INVALID\") while parsing: \"custom_metric_total 1 # {b=\\x00\"",
},
{
input: "custom_metric_total 1 # {b=\"\x00ss\"} 1\n",
err: "expected label value, got \"\\\"\\x00ss\\\"} 1\\n\" (\"INVALID\") while parsing: \"custom_metric_total 1 # {b=\\\"\\x00ss\\\"} 1\\n\"",
err: "expected label value, got \"\\\"\\x00\" (\"INVALID\") while parsing: \"custom_metric_total 1 # {b=\\\"\\x00\"",
},
}

View file

@ -454,6 +454,9 @@ func (p *PromParser) parseLVals() error {
// parseMetricSuffix parses the end of the line after the metric name and
// labels. It starts parsing with the provided token.
func (p *PromParser) parseMetricSuffix(t token) (Entry, error) {
if p.offsets[0] == -1 {
return EntryInvalid, fmt.Errorf("metric name not set while parsing: %q", p.l.b[p.start:p.l.i])
}
if t != tValue {
return EntryInvalid, p.parseError("expected value after metric", t)
}
@ -481,9 +484,6 @@ func (p *PromParser) parseMetricSuffix(t token) (Entry, error) {
return EntryInvalid, p.parseError("expected timestamp or new record", t)
}
if p.offsets[0] == -1 {
return EntryInvalid, fmt.Errorf("metric name not set while parsing: %q", p.l.b[p.start:p.l.i])
}
return EntrySeries, nil
}

View file

@ -392,7 +392,7 @@ func TestPromParseErrors(t *testing.T) {
},
{
input: `{a="ok"} 1`,
err: "metric name not set while parsing: \"{a=\\\"ok\\\"} 1\\n\"",
err: "metric name not set while parsing: \"{a=\\\"ok\\\"} 1\"",
},
{
input: "# TYPE #\n#EOF\n",