Sanitize invalid opentsdb characters

closes #1098
This commit is contained in:
Cameron Sparr 2016-04-28 17:00:06 -06:00
parent 3e1026286b
commit 3ed006d216
2 changed files with 23 additions and 9 deletions

View File

@ -21,6 +21,9 @@ type OpenTSDB struct {
Debug bool Debug bool
} }
var sanitizedChars = strings.NewReplacer("@", "-", "*", "-", " ", "_",
`%`, "-", "#", "-", "$", "-")
var sampleConfig = ` var sampleConfig = `
## prefix for metrics keys ## prefix for metrics keys
prefix = "my.specific.prefix." prefix = "my.specific.prefix."
@ -94,8 +97,8 @@ func buildTags(mTags map[string]string) []string {
tags := make([]string, len(mTags)) tags := make([]string, len(mTags))
index := 0 index := 0
for k, v := range mTags { for k, v := range mTags {
tags[index] = fmt.Sprintf("%s=%s", k, v) tags[index] = sanitizedChars.Replace(fmt.Sprintf("%s=%s", k, v))
index += 1 index++
} }
sort.Strings(tags) sort.Strings(tags)
return tags return tags
@ -105,7 +108,8 @@ func buildMetrics(m telegraf.Metric, now time.Time, prefix string) []*MetricLine
ret := []*MetricLine{} ret := []*MetricLine{}
for fieldName, value := range m.Fields() { for fieldName, value := range m.Fields() {
metric := &MetricLine{ 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(), Timestamp: now.Unix(),
} }

View File

@ -25,6 +25,10 @@ func TestBuildTagsTelnet(t *testing.T) {
map[string]string{"one": "two", "aaa": "bbb"}, map[string]string{"one": "two", "aaa": "bbb"},
[]string{"aaa=bbb", "one=two"}, []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{}, map[string]string{},
[]string{}, []string{},
@ -59,13 +63,19 @@ func TestWrite(t *testing.T) {
// Verify postive and negative test cases of writing data // Verify postive and negative test cases of writing data
metrics := testutil.MockMetrics() metrics := testutil.MockMetrics()
metrics = append(metrics, testutil.TestMetric(float64(1.0), "justametric.float")) metrics = append(metrics, testutil.TestMetric(float64(1.0),
metrics = append(metrics, testutil.TestMetric(int64(123456789), "justametric.int")) "justametric.float"))
metrics = append(metrics, testutil.TestMetric(uint64(123456789012345), "justametric.uint")) metrics = append(metrics, testutil.TestMetric(int64(123456789),
metrics = append(metrics, testutil.TestMetric("Lorem Ipsum", "justametric.string")) "justametric.int"))
metrics = append(metrics, testutil.TestMetric(float64(42.0), "justametric.anotherfloat")) 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) err = o.Write(metrics)
require.NoError(t, err) require.NoError(t, err)
} }