From 3ed006d2166b93c8eebdcd505c675292d604625c Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Thu, 28 Apr 2016 17:00:06 -0600 Subject: [PATCH] Sanitize invalid opentsdb characters closes #1098 --- plugins/outputs/opentsdb/opentsdb.go | 10 +++++++--- plugins/outputs/opentsdb/opentsdb_test.go | 22 ++++++++++++++++------ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/plugins/outputs/opentsdb/opentsdb.go b/plugins/outputs/opentsdb/opentsdb.go index 83a3429e3..5e94ca340 100644 --- a/plugins/outputs/opentsdb/opentsdb.go +++ b/plugins/outputs/opentsdb/opentsdb.go @@ -21,6 +21,9 @@ type OpenTSDB struct { Debug bool } +var sanitizedChars = strings.NewReplacer("@", "-", "*", "-", " ", "_", + `%`, "-", "#", "-", "$", "-") + var sampleConfig = ` ## prefix for metrics keys prefix = "my.specific.prefix." @@ -94,8 +97,8 @@ func buildTags(mTags map[string]string) []string { tags := make([]string, len(mTags)) index := 0 for k, v := range mTags { - tags[index] = fmt.Sprintf("%s=%s", k, v) - index += 1 + tags[index] = sanitizedChars.Replace(fmt.Sprintf("%s=%s", k, v)) + index++ } sort.Strings(tags) return tags @@ -105,7 +108,8 @@ func buildMetrics(m telegraf.Metric, now time.Time, prefix string) []*MetricLine ret := []*MetricLine{} for fieldName, value := range m.Fields() { metric := &MetricLine{ - Metric: fmt.Sprintf("%s%s_%s", prefix, m.Name(), fieldName), + Metric: sanitizedChars.Replace(fmt.Sprintf("%s%s_%s", + prefix, m.Name(), fieldName)), Timestamp: now.Unix(), } diff --git a/plugins/outputs/opentsdb/opentsdb_test.go b/plugins/outputs/opentsdb/opentsdb_test.go index 900c9f123..30323725b 100644 --- a/plugins/outputs/opentsdb/opentsdb_test.go +++ b/plugins/outputs/opentsdb/opentsdb_test.go @@ -25,6 +25,10 @@ func TestBuildTagsTelnet(t *testing.T) { map[string]string{"one": "two", "aaa": "bbb"}, []string{"aaa=bbb", "one=two"}, }, + { + map[string]string{"Sp%ci@l Chars": "g$t repl#ced"}, + []string{"Sp-ci-l_Chars=g-t_repl-ced"}, + }, { map[string]string{}, []string{}, @@ -59,13 +63,19 @@ func TestWrite(t *testing.T) { // Verify postive and negative test cases of writing data metrics := testutil.MockMetrics() - metrics = append(metrics, testutil.TestMetric(float64(1.0), "justametric.float")) - metrics = append(metrics, testutil.TestMetric(int64(123456789), "justametric.int")) - metrics = append(metrics, testutil.TestMetric(uint64(123456789012345), "justametric.uint")) - metrics = append(metrics, testutil.TestMetric("Lorem Ipsum", "justametric.string")) - metrics = append(metrics, testutil.TestMetric(float64(42.0), "justametric.anotherfloat")) + metrics = append(metrics, testutil.TestMetric(float64(1.0), + "justametric.float")) + metrics = append(metrics, testutil.TestMetric(int64(123456789), + "justametric.int")) + metrics = append(metrics, testutil.TestMetric(uint64(123456789012345), + "justametric.uint")) + metrics = append(metrics, testutil.TestMetric("Lorem Ipsum", + "justametric.string")) + metrics = append(metrics, testutil.TestMetric(float64(42.0), + "justametric.anotherfloat")) + metrics = append(metrics, testutil.TestMetric(float64(42.0), + "metric w/ specialchars")) err = o.Write(metrics) require.NoError(t, err) - }