Skip to content

Commit

Permalink
Skip non-numerical values in graphite format (#3179)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlok authored and danielnelson committed Aug 29, 2017
1 parent 2af953c commit 9357059
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/DATA_FORMATS_OUTPUT.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +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.

### Graphite Configuration:

```toml
Expand Down
13 changes: 8 additions & 5 deletions plugins/serializers/graphite/graphite.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ func (s *GraphiteSerializer) Serialize(metric telegraf.Metric) ([]byte, error) {
}

for fieldName, value := range metric.Fields() {
// Convert value to string
valueS := fmt.Sprintf("%#v", value)
point := []byte(fmt.Sprintf("%s %s %d\n",
switch value.(type) {
case string:
continue
}
metricString := fmt.Sprintf("%s %#v %d\n",
// insert "field" section of template
sanitizedChars.Replace(InsertField(bucket, fieldName)),
sanitizedChars.Replace(valueS),
timestamp))
value,
timestamp)
point := []byte(metricString)
out = append(out, point...)
}
return out, nil
Expand Down
22 changes: 22 additions & 0 deletions plugins/serializers/graphite/graphite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,28 @@ func TestSerializeValueField2(t *testing.T) {
assert.Equal(t, expS, mS)
}

func TestSerializeValueString(t *testing.T) {
now := time.Now()
tags := map[string]string{
"host": "localhost",
"cpu": "cpu0",
"datacenter": "us-west-2",
}
fields := map[string]interface{}{
"value": "asdasd",
}
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)
assert.Equal(t, "", mS[0])
}

// test that fields with spaces get fixed.
func TestSerializeFieldWithSpaces(t *testing.T) {
now := time.Now()
Expand Down

0 comments on commit 9357059

Please sign in to comment.