value parser: doc & string handling
This commit is contained in:
parent
46f4be88a6
commit
f2b0ea6722
|
@ -156,7 +156,12 @@ as the parsed metric.
|
||||||
#### Value Configuration:
|
#### Value Configuration:
|
||||||
|
|
||||||
You **must** tell Telegraf what type of metric to collect by using the
|
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
|
**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
|
name that makes sense for your metric, otherwise it will just be set to the
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
|
@ -16,24 +17,30 @@ type ValueParser struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *ValueParser) Parse(buf []byte) ([]telegraf.Metric, error) {
|
func (v *ValueParser) Parse(buf []byte) ([]telegraf.Metric, error) {
|
||||||
// separate out any fields in the buffer, ignore anything but the last.
|
// unless it's a string, separate out any fields in the buffer,
|
||||||
values := bytes.Fields(buf)
|
// ignore anything but the last.
|
||||||
if len(values) < 1 {
|
var vStr string
|
||||||
return []telegraf.Metric{}, nil
|
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 value interface{}
|
||||||
var err error
|
var err error
|
||||||
switch v.DataType {
|
switch v.DataType {
|
||||||
case "", "int", "integer":
|
case "", "int", "integer":
|
||||||
value, err = strconv.Atoi(valueStr)
|
value, err = strconv.Atoi(vStr)
|
||||||
case "float", "long":
|
case "float", "long":
|
||||||
value, err = strconv.ParseFloat(valueStr, 64)
|
value, err = strconv.ParseFloat(vStr, 64)
|
||||||
case "str", "string":
|
case "str", "string":
|
||||||
value = valueStr
|
value = vStr
|
||||||
case "bool", "boolean":
|
case "bool", "boolean":
|
||||||
value, err = strconv.ParseBool(valueStr)
|
value, err = strconv.ParseBool(vStr)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
Loading…
Reference in New Issue