Trim null characters in Value data format (#2049)
* 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
This commit is contained in:
parent
94ce67cc67
commit
196509cc53
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue