Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add function to eat dots #34

Merged
merged 3 commits into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,65 @@ func SanitizeNameAsTagValue(name string) string {
return ""
}

// EatDots removes multiple consecutive, leading, and trailing dots
// from name. If the provided name is only dots, it will return an
// empty string
// The vast majority of names will not need to be modified,
// so we optimize for that case. This function only requires
// allocations if the name does need to be modified.
func EatDots(name string) string {
if len(name) == 0 {
return ""
}

dotsToRemove := 0
if name[0] == '.' {
dotsToRemove++
}
for i := 1; i < len(name); i++ {
if name[i] == '.' {
if name[i-1] == '.' {
dotsToRemove++
}
if i == len(name)-1 {
dotsToRemove++
}
}
}

// the majority of cases will return here
if dotsToRemove == 0 {
return name
}

if dotsToRemove >= len(name) {
return ""
}

newName := make([]byte, len(name)-dotsToRemove)
j := 0
sawDot := false
for i := 0; i < len(name); i++ {
if name[i] == '.' {
if j > 0 {
sawDot = true
}
continue
}

if sawDot {
newName[j] = '.'
sawDot = false
j++
}

newName[j] = name[i]
j++
}

return string(newName)
}

// ValidateTags returns whether all tags are in a valid format.
// a valid format is anything that looks like key=value,
// the length of key and value must be >0 and both cannot contain
Expand Down
53 changes: 53 additions & 0 deletions metricpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@ import (
"testing"
)

var testMetrics = []string{
"some.id.of.a.metric",
".some.id.of.a.metric.",
"..some.id.of.a.metric",
"some.id....of.a.metric",
"some.id.of....a..metric",
"some...id.of.a.metric..",
".",
"...",
"..a.",
"...a",
"a...",
}

var expectedResults = []string{
"some.id.of.a.metric",
"some.id.of.a.metric",
"some.id.of.a.metric",
"some.id.of.a.metric",
"some.id.of.a.metric",
"some.id.of.a.metric",
"",
"",
"a",
"a",
"a",
}

func TestMetricPointMarshal(t *testing.T) {
tests := []MetricPoint{
{
Expand Down Expand Up @@ -169,3 +197,28 @@ func TestMetricPointMarshalMultiple(t *testing.T) {
}
}
}

func TestEatDots(t *testing.T) {
results := make([]string, 0, len(testMetrics))

for _, s := range testMetrics {
results = append(results, EatDots(s))
}

for i := range results {
if results[i] != expectedResults[i] {
t.Errorf("Result '%s' does not match expected '%s'", results[i], expectedResults[i])
}
}
}

var globalString string

func BenchmarkEatDots(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, s := range testMetrics {
globalString = EatDots(s)
}
}
}