Skip to content

Commit

Permalink
Ensure tags use the correct escape codes.
Browse files Browse the repository at this point in the history
Similar to measurement names, some characters were being escaped
incorrectly.
  • Loading branch information
jwilder committed Aug 19, 2015
1 parent 29ceca6 commit 5b11d25
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
28 changes: 24 additions & 4 deletions tsdb/points.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ var (
',': []byte(`\,`),
' ': []byte(`\ `),
}

tagEscapeCodes = map[byte][]byte{
',': []byte(`\,`),
' ': []byte(`\ `),
'=': []byte(`\=`),
}
)

func init() {
Expand Down Expand Up @@ -846,6 +852,20 @@ func unescapeMeasurement(in []byte) []byte {
return in
}

func escapeTag(in []byte) []byte {
for b, esc := range tagEscapeCodes {
in = bytes.Replace(in, []byte{b}, esc, -1)
}
return in
}

func unescapeTag(in []byte) []byte {
for b, esc := range tagEscapeCodes {
in = bytes.Replace(in, esc, []byte{b}, -1)
}
return in
}

func escape(in []byte) []byte {
for b, esc := range escapeCodes {
in = bytes.Replace(in, []byte{b}, esc, -1)
Expand Down Expand Up @@ -956,7 +976,7 @@ func (p *point) Tags() Tags {
i, key = scanTo(p.key, i, '=')
i, value = scanTagValue(p.key, i+1)

tags[string(unescape(key))] = string(unescape(value))
tags[string(unescapeTag(key))] = string(unescapeTag(value))

i += 1
}
Expand Down Expand Up @@ -1061,9 +1081,9 @@ func (t Tags) HashKey() []byte {

escaped := Tags{}
for k, v := range t {
ek := escapeString(k)
ev := escapeString(v)
escaped[ek] = ev
ek := escapeTag([]byte(k))
ev := escapeTag([]byte(v))
escaped[string(ek)] = string(ev)
}

// Extract keys and determine final size.
Expand Down
15 changes: 15 additions & 0 deletions tsdb/points_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,21 @@ func TestParsePointQuotedMeasurement(t *testing.T) {
)
}

func TestParsePointQuotedTags(t *testing.T) {
test(t, `cpu,"host"="serverA",region=us-east value=1.0 1000000000`,
tsdb.NewPoint(
"cpu",
tsdb.Tags{
`"host"`: `"serverA"`,
"region": "us-east",
},
tsdb.Fields{
"value": 1.0,
},
time.Unix(1, 0)),
)
}

func TestParsePointEscapedStringsAndCommas(t *testing.T) {
// non-escaped comma and quotes
test(t, `cpu,host=serverA,region=us-east value="{Hello\"{,}\" World}" 1000000000`,
Expand Down

0 comments on commit 5b11d25

Please sign in to comment.