From 9357059aefd9e191c0f577f8b0cf2fb8393a314e Mon Sep 17 00:00:00 2001 From: Seua Polyakov Date: Wed, 30 Aug 2017 01:59:38 +0300 Subject: [PATCH] Skip non-numerical values in graphite format (#3179) --- docs/DATA_FORMATS_OUTPUT.md | 2 ++ plugins/serializers/graphite/graphite.go | 13 ++++++----- plugins/serializers/graphite/graphite_test.go | 22 +++++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/docs/DATA_FORMATS_OUTPUT.md b/docs/DATA_FORMATS_OUTPUT.md index 2ede82305..15a4e39eb 100644 --- a/docs/DATA_FORMATS_OUTPUT.md +++ b/docs/DATA_FORMATS_OUTPUT.md @@ -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 diff --git a/plugins/serializers/graphite/graphite.go b/plugins/serializers/graphite/graphite.go index 57ddea76f..7cebff4e6 100644 --- a/plugins/serializers/graphite/graphite.go +++ b/plugins/serializers/graphite/graphite.go @@ -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 diff --git a/plugins/serializers/graphite/graphite_test.go b/plugins/serializers/graphite/graphite_test.go index 9f9ff3ca8..da622732f 100644 --- a/plugins/serializers/graphite/graphite_test.go +++ b/plugins/serializers/graphite/graphite_test.go @@ -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()