Check if tag value is empty before allocation

closes #2390
closes #2404
This commit is contained in:
Leandro Piccilli 2017-02-14 00:56:48 +01:00 committed by Cameron Sparr
parent dfddcc5146
commit df402e885d
5 changed files with 34 additions and 7 deletions

View File

@ -61,6 +61,7 @@ be deprecated eventually.
- [#2341](https://github.com/influxdata/telegraf/issues/2341): telegraf swallowing panics in --test mode. - [#2341](https://github.com/influxdata/telegraf/issues/2341): telegraf swallowing panics in --test mode.
- [#2358](https://github.com/influxdata/telegraf/pull/2358): Create pidfile with 644 permissions & defer file deletion. - [#2358](https://github.com/influxdata/telegraf/pull/2358): Create pidfile with 644 permissions & defer file deletion.
- [#2282](https://github.com/influxdata/telegraf/issues/2282): Reloading telegraf freezes prometheus output. - [#2282](https://github.com/influxdata/telegraf/issues/2282): Reloading telegraf freezes prometheus output.
- [#2390](https://github.com/influxdata/telegraf/issues/2390): Empty tag value causes error on InfluxDB output.
## v1.2.1 [2017-02-01] ## v1.2.1 [2017-02-01]

View File

@ -44,13 +44,18 @@ func New(
// pre-allocate exact size of the tags slice // pre-allocate exact size of the tags slice
taglen := 0 taglen := 0
for k, v := range tags { for k, v := range tags {
// TODO check that length of tag key & value are > 0 if len(k) == 0 || len(v) == 0 {
continue
}
taglen += 2 + len(escape(k, "tagkey")) + len(escape(v, "tagval")) taglen += 2 + len(escape(k, "tagkey")) + len(escape(v, "tagval"))
} }
m.tags = make([]byte, taglen) m.tags = make([]byte, taglen)
i := 0 i := 0
for k, v := range tags { for k, v := range tags {
if len(k) == 0 || len(v) == 0 {
continue
}
m.tags[i] = ',' m.tags[i] = ','
i++ i++
i += copy(m.tags[i:], escape(k, "tagkey")) i += copy(m.tags[i:], escape(k, "tagkey"))

View File

@ -625,3 +625,26 @@ func TestNewMetricFailNaN(t *testing.T) {
_, err := New("cpu", tags, fields, now) _, err := New("cpu", tags, fields, now)
assert.NoError(t, err) assert.NoError(t, err)
} }
func TestEmptyTagValueOrKey(t *testing.T) {
now := time.Now()
tags := map[string]string{
"host": "localhost",
"emptytag": "",
"": "valuewithoutkey",
}
fields := map[string]interface{}{
"usage_idle": float64(99),
}
m, err := New("cpu", tags, fields, now)
assert.True(t, m.HasTag("host"))
assert.False(t, m.HasTag("emptytag"))
assert.Equal(t,
fmt.Sprintf("cpu,host=localhost usage_idle=99 %d\n", now.UnixNano()),
m.String())
assert.NoError(t, err)
}

View File

@ -114,8 +114,6 @@ func TestParseValidPrometheus(t *testing.T) {
"osVersion": "CentOS Linux 7 (Core)", "osVersion": "CentOS Linux 7 (Core)",
"dockerVersion": "1.8.2", "dockerVersion": "1.8.2",
"kernelVersion": "3.10.0-229.20.1.el7.x86_64", "kernelVersion": "3.10.0-229.20.1.el7.x86_64",
"cadvisorRevision": "",
"cadvisorVersion": "",
}, metrics[0].Tags()) }, metrics[0].Tags())
// Counter value // Counter value

View File

@ -67,7 +67,7 @@ func TestParseValidOutput(t *testing.T) {
assert.Equal(t, map[string]interface{}{ assert.Equal(t, map[string]interface{}{
"value": float64(0.008457), "value": float64(0.008457),
}, metrics[0].Fields()) }, metrics[0].Fields())
assert.Equal(t, map[string]string{"unit": ""}, metrics[0].Tags()) assert.Equal(t, map[string]string{}, metrics[0].Tags())
} }
func TestParseInvalidOutput(t *testing.T) { func TestParseInvalidOutput(t *testing.T) {