Skip to content

Commit

Permalink
Convert bool fields to int in graphite serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnelson committed Aug 29, 2017
1 parent 9077cb8 commit f96cbb4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/DATA_FORMATS_OUTPUT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

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:

Expand Down
8 changes: 7 additions & 1 deletion plugins/serializers/graphite/graphite.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ func (s *GraphiteSerializer) Serialize(metric telegraf.Metric) ([]byte, error) {
}

for fieldName, value := range metric.Fields() {
switch value.(type) {
switch v := value.(type) {
case string:
continue
case bool:
if v {
value = 1
} else {
value = 0
}
}
metricString := fmt.Sprintf("%s %#v %d\n",
// insert "field" section of template
Expand Down
28 changes: 28 additions & 0 deletions plugins/serializers/graphite/graphite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,34 @@ func TestSerializeValueString(t *testing.T) {
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.
func TestSerializeFieldWithSpaces(t *testing.T) {
now := time.Now()
Expand Down

0 comments on commit f96cbb4

Please sign in to comment.