lib/protoparser/opentsdb: accept multiple spaces between fields in a row as a deliminator. (#1575)

This commit is contained in:
envzhu 2021-08-29 17:38:32 +09:00 committed by Aliaksandr Valialkin
parent ca61d7c82b
commit 00dddfe02f
2 changed files with 91 additions and 3 deletions

View file

@ -57,10 +57,11 @@ func (r *Row) reset() {
func (r *Row) unmarshal(s string, tagsPool []Tag) ([]Tag, error) {
r.reset()
s = strings.TrimSpace(s)
if !strings.HasPrefix(s, "put ") {
return tagsPool, fmt.Errorf("missing `put ` prefix in %q", s)
}
s = s[len("put "):]
s = strings.TrimSpace(s[len("put "):])
n := strings.IndexByte(s, ' ')
if n < 0 {
return tagsPool, fmt.Errorf("cannot find whitespace between metric and timestamp in %q", s)
@ -69,7 +70,7 @@ func (r *Row) unmarshal(s string, tagsPool []Tag) ([]Tag, error) {
if len(r.Metric) == 0 {
return tagsPool, fmt.Errorf("metric cannot be empty")
}
tail := s[n+1:]
tail := strings.TrimSpace(s[n+1:])
n = strings.IndexByte(tail, ' ')
if n < 0 {
return tagsPool, fmt.Errorf("cannot find whitespace between timestamp and value in %q", s)
@ -79,7 +80,7 @@ func (r *Row) unmarshal(s string, tagsPool []Tag) ([]Tag, error) {
return tagsPool, fmt.Errorf("cannot parse timestamp from %q: %w", tail[:n], err)
}
r.Timestamp = int64(timestamp)
tail = tail[n+1:]
tail = strings.TrimSpace(tail[n+1:])
n = strings.IndexByte(tail, ' ')
if n < 0 {
return tagsPool, fmt.Errorf("cannot find whitespace between value and the first tag in %q", s)
@ -148,6 +149,7 @@ func unmarshalTags(dst []Tag, s string) ([]Tag, error) {
}
tag := &dst[len(dst)-1]
s = strings.TrimSpace(s)
n := strings.IndexByte(s, ' ')
if n < 0 {
// The last tag found

View file

@ -221,4 +221,90 @@ func TestRowsUnmarshalSuccess(t *testing.T) {
},
},
})
// Multi spaces
f("put foobar 789 -123.456 a=b", &Rows{
Rows: []Row{{
Metric: "foobar",
Value: -123.456,
Timestamp: 789,
Tags: []Tag{
{
Key: "a",
Value: "b",
},
},
}},
})
f("put foobar 789 -123.456 a=b", &Rows{
Rows: []Row{{
Metric: "foobar",
Value: -123.456,
Timestamp: 789,
Tags: []Tag{
{
Key: "a",
Value: "b",
},
},
}},
})
f("put foobar 789 -123.456 a=b", &Rows{
Rows: []Row{{
Metric: "foobar",
Value: -123.456,
Timestamp: 789,
Tags: []Tag{
{
Key: "a",
Value: "b",
},
},
}},
})
f("put foobar 789 -123.456 a=b", &Rows{
Rows: []Row{{
Metric: "foobar",
Value: -123.456,
Timestamp: 789,
Tags: []Tag{
{
Key: "a",
Value: "b",
},
},
}},
})
f("put foobar 789 -123.456 a=b c=d", &Rows{
Rows: []Row{{
Metric: "foobar",
Value: -123.456,
Timestamp: 789,
Tags: []Tag{
{
Key: "a",
Value: "b",
},
{
Key: "c",
Value: "d",
},
},
}},
})
// Soace after tags
f("put foobar 789 -123.456 a=b ", &Rows{
Rows: []Row{{
Metric: "foobar",
Value: -123.456,
Timestamp: 789,
Tags: []Tag{
{
Key: "a",
Value: "b",
},
},
}},
})
}