From f2b0ea6722df6d6f6d512b9e31a8e6af153229a9 Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Mon, 2 May 2016 12:17:20 -0600 Subject: [PATCH] value parser: doc & string handling --- docs/DATA_FORMATS_INPUT.md | 7 ++++++- plugins/parsers/value/parser.go | 25 ++++++++++++++++--------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/docs/DATA_FORMATS_INPUT.md b/docs/DATA_FORMATS_INPUT.md index 07134e979..7d3fbf5de 100644 --- a/docs/DATA_FORMATS_INPUT.md +++ b/docs/DATA_FORMATS_INPUT.md @@ -156,7 +156,12 @@ as the parsed metric. #### Value Configuration: You **must** tell Telegraf what type of metric to collect by using the -`data_type` configuration option. +`data_type` configuration option. Available options are: + +1. integer +2. float or long +3. string +4. boolean **Note:** It is also recommended that you set `name_override` to a measurement name that makes sense for your metric, otherwise it will just be set to the diff --git a/plugins/parsers/value/parser.go b/plugins/parsers/value/parser.go index 00673eced..0ff6866ee 100644 --- a/plugins/parsers/value/parser.go +++ b/plugins/parsers/value/parser.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "strconv" + "strings" "time" "github.com/influxdata/telegraf" @@ -16,24 +17,30 @@ type ValueParser struct { } func (v *ValueParser) Parse(buf []byte) ([]telegraf.Metric, error) { - // separate out any fields in the buffer, ignore anything but the last. - values := bytes.Fields(buf) - if len(values) < 1 { - return []telegraf.Metric{}, nil + // 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 len(values) < 1 { + return []telegraf.Metric{}, nil + } + vStr = string(values[len(values)-1]) } - valueStr := string(values[len(values)-1]) var value interface{} var err error switch v.DataType { case "", "int", "integer": - value, err = strconv.Atoi(valueStr) + value, err = strconv.Atoi(vStr) case "float", "long": - value, err = strconv.ParseFloat(valueStr, 64) + value, err = strconv.ParseFloat(vStr, 64) case "str", "string": - value = valueStr + value = vStr case "bool", "boolean": - value, err = strconv.ParseBool(valueStr) + value, err = strconv.ParseBool(vStr) } if err != nil { return nil, err