lib/protoparser/prometheus: allow trailing comma in tags list

The trailing comma is generated by cloudwatch exporter.
See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/350
This commit is contained in:
Aliaksandr Valialkin 2020-03-02 22:21:50 +02:00
parent d23df53ba2
commit c3b239eb1a
2 changed files with 22 additions and 9 deletions

View File

@ -193,12 +193,12 @@ func unmarshalRow(dst []Row, s string, tagsPool []Tag, noEscapes bool, errLogger
var invalidLines = metrics.NewCounter(`vm_rows_invalid_total{type="prometheus"}`) var invalidLines = metrics.NewCounter(`vm_rows_invalid_total{type="prometheus"}`)
func unmarshalTags(dst []Tag, s string, noEscapes bool) (string, []Tag, error) { func unmarshalTags(dst []Tag, s string, noEscapes bool) (string, []Tag, error) {
s = skipLeadingWhitespace(s)
if len(s) > 0 && s[0] == '}' {
// End of tags found.
return s[1:], dst, nil
}
for { for {
s = skipLeadingWhitespace(s)
if len(s) > 0 && s[0] == '}' {
// End of tags found.
return s[1:], dst, nil
}
n := strings.IndexByte(s, '=') n := strings.IndexByte(s, '=')
if n < 0 { if n < 0 {
return s, dst, fmt.Errorf("missing value for tag %q", s) return s, dst, fmt.Errorf("missing value for tag %q", s)
@ -248,7 +248,7 @@ func unmarshalTags(dst []Tag, s string, noEscapes bool) (string, []Tag, error) {
if len(s) == 0 || s[0] != ',' { if len(s) == 0 || s[0] != ',' {
return s, dst, fmt.Errorf("missing comma after tag %s=%q", key, value) return s, dst, fmt.Errorf("missing comma after tag %s=%q", key, value)
} }
s = skipLeadingWhitespace(s[1:]) s = s[1:]
} }
} }

View File

@ -111,15 +111,14 @@ func TestRowsUnmarshalFailure(t *testing.T) {
f("a{") f("a{")
f("a { ") f("a { ")
f("a {foo") f("a {foo")
f("a {foo}") f("a {foo} 3")
f("a {foo =") f("a {foo =")
f(`a {foo ="bar`) f(`a {foo ="bar`)
f(`a {foo ="b\ar`) f(`a {foo ="b\ar`)
f(`a {foo = "bar"`) f(`a {foo = "bar"`)
f(`a {foo ="bar",`) f(`a {foo ="bar",`)
f(`a {foo ="bar" , `) f(`a {foo ="bar" , `)
f(`a {foo ="bar" , }`) f(`a {foo ="bar" , baz } 2`)
f(`a {foo ="bar" , baz }`)
// empty metric name // empty metric name
f(`{foo="bar"}`) f(`{foo="bar"}`)
@ -232,6 +231,20 @@ func TestRowsUnmarshalSuccess(t *testing.T) {
}}, }},
}) })
// Trailing comma after tag
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/350
f(`foo{bar="baz",} 1 2`, &Rows{
Rows: []Row{{
Metric: "foo",
Tags: []Tag{{
Key: "bar",
Value: "baz",
}},
Value: 1,
Timestamp: 2,
}},
})
// Multi lines // Multi lines
f("# foo\n # bar ba zzz\nfoo 0.3 2\naaa 3\nbar.baz 0.34 43\n", &Rows{ f("# foo\n # bar ba zzz\nfoo 0.3 2\naaa 3\nbar.baz 0.34 43\n", &Rows{
Rows: []Row{ Rows: []Row{