Skip to content

Commit

Permalink
Support uint fields in aerospike input (#4851)
Browse files Browse the repository at this point in the history
(cherry picked from commit 38e644f)
  • Loading branch information
danielnelson committed Oct 12, 2018
1 parent 3e94c15 commit 261ed4e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 30 deletions.
32 changes: 11 additions & 21 deletions plugins/inputs/aerospike/aerospike.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package aerospike

import (
"crypto/tls"
"errors"
"log"
"net"
"strconv"
"strings"
Expand Down Expand Up @@ -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())

Expand All @@ -152,29 +146,25 @@ 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())
}
}
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
}
}

Expand Down
15 changes: 6 additions & 9 deletions plugins/inputs/aerospike/aerospike_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit 261ed4e

Please sign in to comment.