Skip to content
Snippets Groups Projects
Unverified Commit 8225bd01 authored by Daniel Nelson's avatar Daniel Nelson
Browse files

Convert bool fields to int in graphite serializer

parent 3806424a
No related branches found
No related tags found
No related merge requests found
...@@ -96,7 +96,8 @@ tars.cpu-total.us-east-1.cpu.usage_user 0.89 1455320690 ...@@ -96,7 +96,8 @@ tars.cpu-total.us-east-1.cpu.usage_user 0.89 1455320690
tars.cpu-total.us-east-1.cpu.usage_idle 98.09 1455320690 tars.cpu-total.us-east-1.cpu.usage_idle 98.09 1455320690
``` ```
Fields with non-numeric values will be skipped. Fields with string values will be skipped. Boolean fields will be converted
to 1 (true) or 0 (false).
### Graphite Configuration: ### Graphite Configuration:
......
...@@ -32,9 +32,15 @@ func (s *GraphiteSerializer) Serialize(metric telegraf.Metric) ([]byte, error) { ...@@ -32,9 +32,15 @@ func (s *GraphiteSerializer) Serialize(metric telegraf.Metric) ([]byte, error) {
} }
for fieldName, value := range metric.Fields() { for fieldName, value := range metric.Fields() {
switch value.(type) { switch v := value.(type) {
case string: case string:
continue continue
case bool:
if v {
value = 1
} else {
value = 0
}
} }
metricString := fmt.Sprintf("%s %#v %d\n", metricString := fmt.Sprintf("%s %#v %d\n",
// insert "field" section of template // insert "field" section of template
......
...@@ -187,6 +187,34 @@ func TestSerializeValueString(t *testing.T) { ...@@ -187,6 +187,34 @@ func TestSerializeValueString(t *testing.T) {
assert.Equal(t, "", mS[0]) assert.Equal(t, "", mS[0])
} }
func TestSerializeValueBoolean(t *testing.T) {
now := time.Now()
tags := map[string]string{
"host": "localhost",
"cpu": "cpu0",
"datacenter": "us-west-2",
}
fields := map[string]interface{}{
"enabled": true,
"disabled": false,
}
m, err := metric.New("cpu", tags, fields, now)
assert.NoError(t, err)
s := GraphiteSerializer{
Template: "host.field.tags.measurement",
}
buf, _ := s.Serialize(m)
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
assert.NoError(t, err)
expS := []string{
fmt.Sprintf("localhost.enabled.cpu0.us-west-2.cpu 1 %d", now.Unix()),
fmt.Sprintf("localhost.disabled.cpu0.us-west-2.cpu 0 %d", now.Unix()),
}
assert.Equal(t, expS, mS)
}
// test that fields with spaces get fixed. // test that fields with spaces get fixed.
func TestSerializeFieldWithSpaces(t *testing.T) { func TestSerializeFieldWithSpaces(t *testing.T) {
now := time.Now() now := time.Now()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment