Support uint fields in aerospike input (#4851)

This commit is contained in:
Daniel Nelson 2018-10-12 14:37:30 -07:00 committed by GitHub
parent 27bd51b9ac
commit 38e644ff12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 30 deletions

View File

@ -2,8 +2,6 @@ package aerospike
import (
"crypto/tls"
"errors"
"log"
"net"
"strconv"
"strings"
@ -120,12 +118,8 @@ func (a *Aerospike) gatherServer(hostport string, acc telegraf.Accumulator) erro
return err
}
for k, v := range stats {
val, err := parseValue(v)
if err == nil {
fields[strings.Replace(k, "-", "_", -1)] = val
} else {
log.Printf("I! skipping aerospike field %v with int64 overflow: %q", k, v)
}
val := parseValue(v)
fields[strings.Replace(k, "-", "_", -1)] = val
}
acc.AddFields("aerospike_node", fields, tags, time.Now())
@ -152,12 +146,8 @@ func (a *Aerospike) gatherServer(hostport string, acc telegraf.Accumulator) erro
if len(parts) < 2 {
continue
}
val, err := parseValue(parts[1])
if err == nil {
nFields[strings.Replace(parts[0], "-", "_", -1)] = val
} else {
log.Printf("I! skipping aerospike field %v with int64 overflow: %q", parts[0], parts[1])
}
val := parseValue(parts[1])
nFields[strings.Replace(parts[0], "-", "_", -1)] = val
}
acc.AddFields("aerospike_namespace", nFields, nTags, time.Now())
}
@ -165,16 +155,16 @@ func (a *Aerospike) gatherServer(hostport string, acc telegraf.Accumulator) erro
return nil
}
func parseValue(v string) (interface{}, error) {
func parseValue(v string) interface{} {
if parsed, err := strconv.ParseInt(v, 10, 64); err == nil {
return parsed, nil
} else if _, err := strconv.ParseUint(v, 10, 64); err == nil {
// int64 overflow, yet valid uint64
return nil, errors.New("Number is too large")
return parsed
} else if parsed, err := strconv.ParseUint(v, 10, 64); err == nil {
return parsed
} else if parsed, err := strconv.ParseBool(v); err == nil {
return parsed, nil
return parsed
} else {
return v, nil
// leave as string
return v
}
}

View File

@ -52,17 +52,14 @@ func TestAerospikeStatisticsPartialErr(t *testing.T) {
func TestAerospikeParseValue(t *testing.T) {
// uint64 with value bigger than int64 max
val, err := parseValue("18446744041841121751")
assert.Nil(t, val)
assert.Error(t, err)
val := parseValue("18446744041841121751")
require.Equal(t, uint64(18446744041841121751), val)
// int values
val, err = parseValue("42")
assert.NoError(t, err)
assert.Equal(t, val, int64(42), "must be parsed as int")
val = parseValue("42")
require.Equal(t, val, int64(42), "must be parsed as int")
// string values
val, err = parseValue("BB977942A2CA502")
assert.NoError(t, err)
assert.Equal(t, val, `BB977942A2CA502`, "must be left as string")
val = parseValue("BB977942A2CA502")
require.Equal(t, val, `BB977942A2CA502`, "must be left as string")
}