Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure correct number of tags parsed when commas used #9573

Merged
merged 1 commit into from
Mar 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions models/points.go
Original file line number Diff line number Diff line change
Expand Up @@ -1551,12 +1551,12 @@ func parseTags(buf []byte) Tags {
return nil
}

tags := make(Tags, bytes.Count(buf, []byte(",")))
p := 0
// Series keys can contain escaped commas, therefore the number of commas
// in a series key only gives an estimation of the upper bound on the number
// of tags.
tags := make(Tags, 0, bytes.Count(buf, []byte(",")))
walkTags(buf, func(key, value []byte) bool {
tags[p].Key = key
tags[p].Value = value
p++
tags = append(tags, Tag{Key: key, Value: value})
return true
})
return tags
Expand Down
36 changes: 36 additions & 0 deletions models/points_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,42 @@ func BenchmarkMarshal(b *testing.B) {
tags.HashKey()
}
}
func TestPoint_Tags(t *testing.T) {
examples := []struct {
Point string
Tags models.Tags
}{
{`cpu value=1`, models.Tags{}},
{"cpu,tag0=v0 value=1", models.NewTags(map[string]string{"tag0": "v0"})},
{"cpu,tag0=v0,tag1=v0 value=1", models.NewTags(map[string]string{"tag0": "v0", "tag1": "v0"})},
{`cpu,tag0=v\ 0 value=1`, models.NewTags(map[string]string{"tag0": "v 0"})},
{`cpu,tag0=v\ 0\ 1,tag1=v2 value=1`, models.NewTags(map[string]string{"tag0": "v 0 1", "tag1": "v2"})},
{`cpu,tag0=\, value=1`, models.NewTags(map[string]string{"tag0": ","})},
{`cpu,ta\ g0=\, value=1`, models.NewTags(map[string]string{"ta g0": ","})},
{`cpu,tag0=\,1 value=1`, models.NewTags(map[string]string{"tag0": ",1"})},
{`cpu,tag0=1\"\",t=k value=1`, models.NewTags(map[string]string{"tag0": `1\"\"`, "t": "k"})},
}

for _, example := range examples {
t.Run(example.Point, func(t *testing.T) {
pts, err := models.ParsePointsString(example.Point)
if err != nil {
t.Fatal(err)
} else if len(pts) != 1 {
t.Fatalf("parsed %d points, expected 1", len(pts))
}

// Repeat to test Tags() caching
for i := 0; i < 2; i++ {
tags := pts[0].Tags()
if !reflect.DeepEqual(tags, example.Tags) {
t.Fatalf("got %#v (%s), expected %#v", tags, tags.String(), example.Tags)
}
}

})
}
}

func TestPoint_StringSize(t *testing.T) {
testPoint_cube(t, func(p models.Point) {
Expand Down