diff --git a/plugins/outputs/opentsdb/opentsdb.go b/plugins/outputs/opentsdb/opentsdb.go index 2be6f066a..1d0a38e9b 100644 --- a/plugins/outputs/opentsdb/opentsdb.go +++ b/plugins/outputs/opentsdb/opentsdb.go @@ -3,6 +3,7 @@ package opentsdb import ( "fmt" "net" + "sort" "strconv" "strings" @@ -49,12 +50,14 @@ var sampleConfig = ` type TagSet map[string]string func (t TagSet) ToLineFormat() string { - var line string + tags := make([]string, len(t)) + index := 0 for k, v := range t { - line += fmt.Sprintf(" %s=%s", k, v) + tags[index] = fmt.Sprintf("%s=%s", k, v) + index++ } - - return strings.TrimLeft(line, " ") + sort.Strings(tags) + return strings.Join(tags, " ") } func (o *OpenTSDB) Connect() error { diff --git a/plugins/outputs/opentsdb/opentsdb_test.go b/plugins/outputs/opentsdb/opentsdb_test.go index 6c141d463..31d4ace1a 100644 --- a/plugins/outputs/opentsdb/opentsdb_test.go +++ b/plugins/outputs/opentsdb/opentsdb_test.go @@ -7,34 +7,60 @@ import ( // "github.com/stretchr/testify/require" ) -func TestBuildTagsTelnet(t *testing.T) { +func TestCleanTags(t *testing.T) { var tagtests = []struct { ptIn map[string]string - outTags []string + outTags TagSet }{ { map[string]string{"one": "two", "three": "four"}, - []string{"one=two", "three=four"}, + TagSet{"one": "two", "three": "four"}, }, { map[string]string{"aaa": "bbb"}, - []string{"aaa=bbb"}, - }, - { - map[string]string{"one": "two", "aaa": "bbb"}, - []string{"aaa=bbb", "one=two"}, + TagSet{"aaa": "bbb"}, }, { map[string]string{"Sp%ci@l Chars": "g$t repl#ced"}, - []string{"Sp-ci-l_Chars=g-t_repl-ced"}, + TagSet{"Sp-ci-l_Chars": "g-t_repl-ced"}, }, { map[string]string{}, - []string{}, + TagSet{}, }, } for _, tt := range tagtests { - tags := buildTags(tt.ptIn) + tags := cleanTags(tt.ptIn) + if !reflect.DeepEqual(tags, tt.outTags) { + t.Errorf("\nexpected %+v\ngot %+v\n", tt.outTags, tags) + } + } +} + +func TestBuildTagsTelnet(t *testing.T) { + var tagtests = []struct { + ptIn TagSet + outTags string + }{ + { + TagSet{"one": "two", "three": "four"}, + "one=two three=four", + }, + { + TagSet{"aaa": "bbb"}, + "aaa=bbb", + }, + { + TagSet{"one": "two", "aaa": "bbb"}, + "aaa=bbb one=two", + }, + { + TagSet{}, + "", + }, + } + for _, tt := range tagtests { + tags := tt.ptIn.ToLineFormat() if !reflect.DeepEqual(tags, tt.outTags) { t.Errorf("\nexpected %+v\ngot %+v\n", tt.outTags, tags) }