-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: bakins <[email protected]>
- Loading branch information
Showing
4 changed files
with
107 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package mapper | ||
|
||
import ( | ||
"strings" | ||
"unicode/utf8" | ||
) | ||
|
||
// EscapeMetricName replaces invalid characters in the metric name with "_" | ||
// Valid characters are a-z, A-Z, 0-9, and _ | ||
func EscapeMetricName(metricName string) string { | ||
metricLen := len(metricName) | ||
if metricLen == 0 { | ||
return "" | ||
} | ||
|
||
escaped := false | ||
var sb strings.Builder | ||
// If a metric starts with a digit, allocate the memory and prepend an | ||
// underscore. | ||
if metricName[0] >= '0' && metricName[0] <= '9' { | ||
escaped = true | ||
sb.Grow(metricLen + 1) | ||
sb.WriteByte('_') | ||
} | ||
|
||
// This is an character replacement method optimized for this limited | ||
// use case. It is much faster than using a regex. | ||
offset := 0 | ||
for i, c := range metricName { | ||
// Seek forward, skipping valid characters until we find one that needs | ||
// to be replaced, then add all the characters we've seen so far to the | ||
// string.Builder. | ||
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || | ||
(c >= '0' && c <= '9') || (c == '_') { | ||
// Character is valid, so skip over it without doing anything. | ||
} else { | ||
if !escaped { | ||
// Up until now we've been lazy and avoided actually allocating | ||
// memory. Unfortunately we've now determined this string needs | ||
// escaping, so allocate the buffer for the whole string. | ||
escaped = true | ||
sb.Grow(metricLen) | ||
} | ||
sb.WriteString(metricName[offset:i]) | ||
offset = i + utf8.RuneLen(c) | ||
sb.WriteByte('_') | ||
} | ||
} | ||
|
||
if !escaped { | ||
// This is the happy path where nothing had to be escaped, so we can | ||
// avoid doing anything. | ||
return metricName | ||
} | ||
|
||
if offset < metricLen { | ||
sb.WriteString(metricName[offset:]) | ||
} | ||
|
||
return sb.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package mapper | ||
|
||
import "testing" | ||
|
||
func TestEscapeMetricName(t *testing.T) { | ||
scenarios := map[string]string{ | ||
"clean": "clean", | ||
"0starts_with_digit": "_0starts_with_digit", | ||
"with_underscore": "with_underscore", | ||
"with.dot": "with_dot", | ||
"with😱emoji": "with_emoji", | ||
"with.*.multiple": "with___multiple", | ||
"test.web-server.foo.bar": "test_web_server_foo_bar", | ||
"": "", | ||
} | ||
|
||
for in, want := range scenarios { | ||
if got := EscapeMetricName(in); want != got { | ||
t.Errorf("expected `%s` to be escaped to `%s`, got `%s`", in, want, got) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkEscapeMetricName(b *testing.B) { | ||
scenarios := []string{ | ||
"clean", | ||
"0starts_with_digit", | ||
"with_underscore", | ||
"with.dot", | ||
"with😱emoji", | ||
"with.*.multiple", | ||
"test.web-server.foo.bar", | ||
"", | ||
} | ||
|
||
for _, s := range scenarios { | ||
b.Run(s, func(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
EscapeMetricName(s) | ||
} | ||
}) | ||
} | ||
} |