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 ( import (
"crypto/tls" "crypto/tls"
"errors"
"log"
"net" "net"
"strconv" "strconv"
"strings" "strings"
@ -120,12 +118,8 @@ func (a *Aerospike) gatherServer(hostport string, acc telegraf.Accumulator) erro
return err return err
} }
for k, v := range stats { for k, v := range stats {
val, err := parseValue(v) val := parseValue(v)
if err == nil { fields[strings.Replace(k, "-", "_", -1)] = val
fields[strings.Replace(k, "-", "_", -1)] = val
} else {
log.Printf("I! skipping aerospike field %v with int64 overflow: %q", k, v)
}
} }
acc.AddFields("aerospike_node", fields, tags, time.Now()) 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 { if len(parts) < 2 {
continue continue
} }
val, err := parseValue(parts[1]) val := parseValue(parts[1])
if err == nil { nFields[strings.Replace(parts[0], "-", "_", -1)] = val
nFields[strings.Replace(parts[0], "-", "_", -1)] = val
} else {
log.Printf("I! skipping aerospike field %v with int64 overflow: %q", parts[0], parts[1])
}
} }
acc.AddFields("aerospike_namespace", nFields, nTags, time.Now()) acc.AddFields("aerospike_namespace", nFields, nTags, time.Now())
} }
@ -165,16 +155,16 @@ func (a *Aerospike) gatherServer(hostport string, acc telegraf.Accumulator) erro
return nil return nil
} }
func parseValue(v string) (interface{}, error) { func parseValue(v string) interface{} {
if parsed, err := strconv.ParseInt(v, 10, 64); err == nil { if parsed, err := strconv.ParseInt(v, 10, 64); err == nil {
return parsed, nil return parsed
} else if _, err := strconv.ParseUint(v, 10, 64); err == nil { } else if parsed, err := strconv.ParseUint(v, 10, 64); err == nil {
// int64 overflow, yet valid uint64 return parsed
return nil, errors.New("Number is too large")
} else if parsed, err := strconv.ParseBool(v); err == nil { } else if parsed, err := strconv.ParseBool(v); err == nil {
return parsed, nil return parsed
} else { } 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) { func TestAerospikeParseValue(t *testing.T) {
// uint64 with value bigger than int64 max // uint64 with value bigger than int64 max
val, err := parseValue("18446744041841121751") val := parseValue("18446744041841121751")
assert.Nil(t, val) require.Equal(t, uint64(18446744041841121751), val)
assert.Error(t, err)
// int values // int values
val, err = parseValue("42") val = parseValue("42")
assert.NoError(t, err) require.Equal(t, val, int64(42), "must be parsed as int")
assert.Equal(t, val, int64(42), "must be parsed as int")
// string values // string values
val, err = parseValue("BB977942A2CA502") val = parseValue("BB977942A2CA502")
assert.NoError(t, err) require.Equal(t, val, `BB977942A2CA502`, "must be left as string")
assert.Equal(t, val, `BB977942A2CA502`, "must be left as string")
} }