Fix boolean handling in splunkmetric serializer (#5008)

This commit is contained in:
Lance O'Connor 2018-11-21 17:43:42 -08:00 committed by Daniel Nelson
parent f57b019e22
commit 85ee354255
2 changed files with 61 additions and 7 deletions

View File

@ -68,14 +68,16 @@ func (s *serializer) createObject(metric telegraf.Metric) (metricGroup []byte, e
for _, field := range metric.FieldList() { for _, field := range metric.FieldList() {
if !verifyValue(field.Value) { value, valid := verifyValue(field.Value)
if !valid {
log.Printf("D! Can not parse value: %v for key: %v", field.Value, field.Key) log.Printf("D! Can not parse value: %v for key: %v", field.Value, field.Key)
continue continue
} }
obj := map[string]interface{}{} obj := map[string]interface{}{}
obj["metric_name"] = metric.Name() + "." + field.Key obj["metric_name"] = metric.Name() + "." + field.Key
obj["_value"] = field.Value obj["_value"] = value
dataGroup.Event = "metric" dataGroup.Event = "metric"
// Convert ns to float seconds since epoch. // Convert ns to float seconds since epoch.
@ -94,8 +96,6 @@ func (s *serializer) createObject(metric telegraf.Metric) (metricGroup []byte, e
dataGroup.Fields[n] = t dataGroup.Fields[n] = t
} }
} }
dataGroup.Fields["metric_name"] = metric.Name() + "." + field.Key
dataGroup.Fields["_value"] = field.Value
switch s.HecRouting { switch s.HecRouting {
case true: case true:
@ -117,10 +117,24 @@ func (s *serializer) createObject(metric telegraf.Metric) (metricGroup []byte, e
return metricGroup, nil return metricGroup, nil
} }
func verifyValue(v interface{}) bool { func verifyValue(v interface{}) (value interface{}, valid bool) {
switch v.(type) { switch v.(type) {
case string: case string:
return false valid = false
value = v
case bool:
if v == bool(true) {
// Store 1 for a "true" value
valid = true
value = 1
} else {
// Otherwise store 0
valid = true
value = 0
} }
return true default:
valid = true
value = v
}
return value, valid
} }

View File

@ -97,6 +97,46 @@ func TestSerializeMetricIntHec(t *testing.T) {
assert.Equal(t, string(expS), string(buf)) 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) { func TestSerializeMetricString(t *testing.T) {
now := time.Unix(0, 0) now := time.Unix(0, 0)
tags := map[string]string{ tags := map[string]string{