From 2ab2c442cf707a9b2fcc0ba5628881b8d5dc8dc2 Mon Sep 17 00:00:00 2001 From: akartasov Date: Sat, 23 Oct 2021 23:45:19 +0700 Subject: [PATCH 1/2] feat: handle double dashes in unmapped metrics (#389) Signed-off-by: akartasov --- pkg/mapper/escape.go | 10 ++++++++++ pkg/mapper/escape_test.go | 6 +++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/mapper/escape.go b/pkg/mapper/escape.go index fc8d194..cbc01a6 100644 --- a/pkg/mapper/escape.go +++ b/pkg/mapper/escape.go @@ -39,6 +39,7 @@ func EscapeMetricName(metricName string) string { // This is an character replacement method optimized for this limited // use case. It is much faster than using a regex. offset := 0 + var prevChar rune 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 @@ -47,6 +48,13 @@ func EscapeMetricName(metricName string) string { (c >= '0' && c <= '9') || (c == '_') { // Character is valid, so skip over it without doing anything. } else { + // Double-dashes are allowed if there is a corresponding mapping. + // For consistency, double-dashes should also be allowed in the default case. + if c == '-' && prevChar == '-' { + offset = i + utf8.RuneLen(c) + continue + } + if !escaped { // Up until now we've been lazy and avoided actually allocating // memory. Unfortunately we've now determined this string needs @@ -58,6 +66,8 @@ func EscapeMetricName(metricName string) string { offset = i + utf8.RuneLen(c) sb.WriteByte('_') } + + prevChar = c } if !escaped { diff --git a/pkg/mapper/escape_test.go b/pkg/mapper/escape_test.go index 336692d..2d2e1ca 100644 --- a/pkg/mapper/escape_test.go +++ b/pkg/mapper/escape_test.go @@ -20,8 +20,10 @@ func TestEscapeMetricName(t *testing.T) { "clean": "clean", "0starts_with_digit": "_0starts_with_digit", "with_underscore": "with_underscore", + "with--doubledash": "with_doubledash", + "with---multiple-dashes": "with_multiple_dashes", "with.dot": "with_dot", - "with😱emoji": "with_emoji", + "with😱emoji": "with_emoji", "with.*.multiple": "with___multiple", "test.web-server.foo.bar": "test_web_server_foo_bar", "": "", @@ -39,6 +41,8 @@ func BenchmarkEscapeMetricName(b *testing.B) { "clean", "0starts_with_digit", "with_underscore", + "with--doubledash", + "with---multiple-dashes", "with.dot", "with😱emoji", "with.*.multiple", From b6fc5ded9f2eabb2fe9af3bd6d54f812e9f736b9 Mon Sep 17 00:00:00 2001 From: akartasov Date: Sun, 24 Oct 2021 00:12:41 +0700 Subject: [PATCH 2/2] fix linter warnings: goimports, wsl Signed-off-by: akartasov --- pkg/mapper/escape.go | 2 ++ pkg/mapper/escape_test.go | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/mapper/escape.go b/pkg/mapper/escape.go index cbc01a6..f73b16d 100644 --- a/pkg/mapper/escape.go +++ b/pkg/mapper/escape.go @@ -39,7 +39,9 @@ func EscapeMetricName(metricName string) string { // This is an character replacement method optimized for this limited // use case. It is much faster than using a regex. offset := 0 + var prevChar rune + 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 diff --git a/pkg/mapper/escape_test.go b/pkg/mapper/escape_test.go index 2d2e1ca..bafb22f 100644 --- a/pkg/mapper/escape_test.go +++ b/pkg/mapper/escape_test.go @@ -23,7 +23,7 @@ func TestEscapeMetricName(t *testing.T) { "with--doubledash": "with_doubledash", "with---multiple-dashes": "with_multiple_dashes", "with.dot": "with_dot", - "with😱emoji": "with_emoji", + "with😱emoji": "with_emoji", "with.*.multiple": "with___multiple", "test.web-server.foo.bar": "test_web_server_foo_bar", "": "",