Skip to content

Commit

Permalink
replace dots in metric keys, default a value delimiter in columns is …
Browse files Browse the repository at this point in the history
…"" (#3)
  • Loading branch information
xjewer authored Sep 29, 2017
1 parent ca069c2 commit 1a83d65
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 10 deletions.
4 changes: 4 additions & 0 deletions export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,7 @@ func GetElementString(l *Line, i int, sep string, last bool) (string, error) {
func GetLastMatch(s string, sep string) string {
return getLastMatch(s, sep)
}

func SubstituteDots(s string) string {
return substituteDots(s)
}
16 changes: 14 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func makeKeyFromPaths(l *Line, m *metric) (string, error) {
if err != nil {
return "", err
}
buffer.WriteString(m)
buffer.WriteString(substituteDots(m))
} else {
buffer.WriteString(k.val)
}
Expand All @@ -99,7 +99,7 @@ func getElementString(l *Line, i int, sep string, last bool) (string, error) {
return "", err
}

if last {
if last && sep != "" {
return getLastMatch(c, sep), nil
}

Expand Down Expand Up @@ -132,6 +132,13 @@ func getAmount(s string, sep string) (float32, error) {
if s == "-" {
return result, ErrEmptyString
}
if sep == "" {
f, err := strconv.ParseFloat(s, 32)
if err != nil {
return result, err
}
return float32(f), nil
}

columns := strings.Split(s, sep)
for _, c := range columns {
Expand All @@ -149,3 +156,8 @@ func getAmount(s string, sep string) (float32, error) {
}
return result, nil
}

// substituteDots replaces dots in string
func substituteDots(s string) string {
return strings.Replace(s, ".", "_", -1)
}
63 changes: 55 additions & 8 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,6 @@ func Test_GetElementAmount(t *testing.T) {
}

func Test_HandleLine(t *testing.T) {
type testCase struct {
str string
err error
}

a := assert.New(t)
cfg := config.Source{
Delimiter: " ",
Keys: []config.Key{
Expand All @@ -135,7 +129,31 @@ func Test_HandleLine(t *testing.T) {
},
},
}
runHandleLineTestcases(cfg, t)
}

func Test_HandleLine2(t *testing.T) {
cfg := config.Source{
Delimiter: " ",
Keys: []config.Key{
{
Key: "$3.$6",
Count: true,
Timing: "$4",
},
},
}

runHandleLineTestcases(cfg, t)
}

func runHandleLineTestcases(cfg config.Source, t *testing.T) {
type testCase struct {
str string
err error
}

a := assert.New(t)
p, err := snitch.NewParser(snitch.NewNoopReader(nil), statsd.NoopClient{}, cfg)
a.Nil(err)
h, ok := p.(*snitch.Handler)
Expand All @@ -160,9 +178,9 @@ func Test_HandleLine(t *testing.T) {
a := assert.New(t)
err := h.HandleLine(snitch.NewLine(tc.str, nil))
if tc.err != nil {
a.EqualError(tc.err, err.Error())
a.EqualError(tc.err, err.Error(), "Should be equal")
} else {
a.Nil(err)
a.Nil(err, "Should be nil error")
}
})
}
Expand Down Expand Up @@ -210,3 +228,32 @@ func Test_HandleLineError(t *testing.T) {
})
}
}

func TestSubstitute(t *testing.T) {
type testCase struct {
str string
expectation string
}
cases := []testCase{
{
"10.1.12.13",
"10_1_12_13",
},
{
"test",
"test",
},
{
"1.2.3.4.5.6.7.8.9",
"1_2_3_4_5_6_7_8_9",
},
}

for i, tc := range cases {
t.Run(fmt.Sprint(i), func(t *testing.T) {
a := assert.New(t)
result := snitch.SubstituteDots(tc.str)
a.EqualValues(tc.expectation, result)
})
}
}

0 comments on commit 1a83d65

Please sign in to comment.