Skip to content

Commit

Permalink
Trim null characters in Value data format (#2049)
Browse files Browse the repository at this point in the history
* Trim null characters in Value data format

Some producers (such as the paho embedded c mqtt client) add a null
character "\x00" to the end of a message.  The Value parser would fail on
any message from such a producer.

* Trim whitespace and null in all Value data formats

* No unnecessary reassignments in Value data format parser

* Update change log for Value data format fix
  • Loading branch information
zan-xhipe authored and sparrc committed Nov 16, 2016
1 parent 94ce67c commit 196509c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Bugfixes

- [#2049](https://github.com/influxdata/telegraf/pull/2049): Fix the Value data format not trimming null characters from input.
- [#1949](https://github.com/influxdata/telegraf/issues/1949): Fix windows `net` plugin.
- [#1775](https://github.com/influxdata/telegraf/issues/1775): Cache & expire metrics for delivery to prometheus

Expand Down
9 changes: 4 additions & 5 deletions plugins/parsers/value/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ type ValueParser struct {
}

func (v *ValueParser) Parse(buf []byte) ([]telegraf.Metric, error) {
vStr := string(bytes.TrimSpace(bytes.Trim(buf, "\x00")))

// unless it's a string, separate out any fields in the buffer,
// ignore anything but the last.
var vStr string
if v.DataType == "string" {
vStr = strings.TrimSpace(string(buf))
} else {
values := bytes.Fields(buf)
if v.DataType != "string" {
values := strings.Fields(vStr)
if len(values) < 1 {
return []telegraf.Metric{}, nil
}
Expand Down
15 changes: 15 additions & 0 deletions plugins/parsers/value/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,18 @@ func TestParseValidValuesDefaultTags(t *testing.T) {
}, metrics[0].Fields())
assert.Equal(t, map[string]string{"test": "tag"}, metrics[0].Tags())
}

func TestParseValuesWithNullCharacter(t *testing.T) {
parser := ValueParser{
MetricName: "value_test",
DataType: "integer",
}
metrics, err := parser.Parse([]byte("55\x00"))
assert.NoError(t, err)
assert.Len(t, metrics, 1)
assert.Equal(t, "value_test", metrics[0].Name())
assert.Equal(t, map[string]interface{}{
"value": int64(55),
}, metrics[0].Fields())
assert.Equal(t, map[string]string{}, metrics[0].Tags())
}

0 comments on commit 196509c

Please sign in to comment.