Skip to content

Commit

Permalink
Refactor value verification to manage bools.
Browse files Browse the repository at this point in the history
Strings are still dropped, bools are set to 1 for true and 0 for false
Solves: influxdata#5006
  • Loading branch information
ronnocol committed Nov 19, 2018
1 parent 7a779a7 commit 47b5730
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 12 deletions.
25 changes: 13 additions & 12 deletions plugins/serializers/splunkmetric/splunkmetric.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,19 @@ func (s *serializer) createObject(metric telegraf.Metric) (metricGroup []byte, e

for _, field := range metric.FieldList() {

if !verifyValue(field.Value) {
log.Printf("D! Can not parse value: %v for key: %v", field.Value, field.Key)
continue
}
switch field.Value.(type) {
case string:
log.Printf("D! Can not parse value: %v for key: %v", field.Value, field.Key)
continue
case bool:
if field.Value == bool(true) {
// Store 1 for a "true" value
field.Value = 1
} else {
// Otherwise store 0
field.Value = 0
}
}

obj := map[string]interface{}{}
obj["metric_name"] = metric.Name() + "." + field.Key
Expand Down Expand Up @@ -116,11 +125,3 @@ func (s *serializer) createObject(metric telegraf.Metric) (metricGroup []byte, e

return metricGroup, nil
}

func verifyValue(v interface{}) bool {
switch v.(type) {
case string:
return false
}
return true
}
40 changes: 40 additions & 0 deletions plugins/serializers/splunkmetric/splunkmetric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,46 @@ func TestSerializeMetricIntHec(t *testing.T) {
assert.Equal(t, string(expS), string(buf))
}

func TestSerializeMetricBool(t *testing.T) {
now := time.Unix(0, 0)
tags := map[string]string{
"container-name": "telegraf-test",
}
fields := map[string]interface{}{
"oomkiller": bool(true),
}
m, err := metric.New("docker", tags, fields, now)
assert.NoError(t, err)

s, _ := NewSerializer(false)
var buf []byte
buf, err = s.Serialize(m)
assert.NoError(t, err)

expS := `{"_value":1,"container-name":"telegraf-test","metric_name":"docker.oomkiller","time":0}`
assert.Equal(t, string(expS), string(buf))
}

func TestSerializeMetricBoolHec(t *testing.T) {
now := time.Unix(0, 0)
tags := map[string]string{
"container-name": "telegraf-test",
}
fields := map[string]interface{}{
"oomkiller": bool(false),
}
m, err := metric.New("docker", tags, fields, now)
assert.NoError(t, err)

s, _ := NewSerializer(true)
var buf []byte
buf, err = s.Serialize(m)
assert.NoError(t, err)

expS := `{"time":0,"event":"metric","fields":{"_value":0,"container-name":"telegraf-test","metric_name":"docker.oomkiller"}}`
assert.Equal(t, string(expS), string(buf))
}

func TestSerializeMetricString(t *testing.T) {
now := time.Unix(0, 0)
tags := map[string]string{
Expand Down

0 comments on commit 47b5730

Please sign in to comment.