From a26d350376701836e532b04217658e2671d778e9 Mon Sep 17 00:00:00 2001 From: "Alex Ellis (VMware)" <alexellis2@gmail.com> Date: Thu, 24 Jan 2019 09:06:18 +0000 Subject: [PATCH] Allow unicode in service paths - according to discussion in #1013 all unicode characters are valid label values - this commit allows the original path to be retained. Signed-off-by: Alex Ellis (VMware) <alexellis2@gmail.com> --- gateway/handlers/notifiers.go | 15 ++++++--------- gateway/handlers/notifiers_test.go | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 gateway/handlers/notifiers_test.go diff --git a/gateway/handlers/notifiers.go b/gateway/handlers/notifiers.go index ed11c2a7..ff57fbf7 100644 --- a/gateway/handlers/notifiers.go +++ b/gateway/handlers/notifiers.go @@ -3,7 +3,6 @@ package handlers import ( "fmt" "log" - "regexp" "strconv" "strings" "time" @@ -30,16 +29,14 @@ func (psn PrometheusServiceNotifier) Notify(method string, URL string, originalU psn.ServiceMetrics.Histogram.WithLabelValues(method, path, code).Observe(duration.Seconds()) } -var invalidChars = regexp.MustCompile(`[^a-zA-Z0-9]+`) - -// converts a URL path to a string compatible with Prometheus label value. func urlToLabel(path string) string { - result := invalidChars.ReplaceAllString(path, "_") - result = strings.ToLower(strings.Trim(result, "_")) - if result == "" { - result = "root" + if len(path) > 0 { + path = strings.TrimRight(path, "/") + } + if path == "" { + path = "/" } - return result + return path } // PrometheusFunctionNotifier records metrics to Prometheus diff --git a/gateway/handlers/notifiers_test.go b/gateway/handlers/notifiers_test.go new file mode 100644 index 00000000..753e5ccb --- /dev/null +++ b/gateway/handlers/notifiers_test.go @@ -0,0 +1,23 @@ +package handlers + +import "testing" + +func Test_urlToLabel_normalizeTrailing(t *testing.T) { + have := "/system/functions/" + want := "/system/functions" + got := urlToLabel(have) + + if got != want { + t.Errorf("want %s, got %s", want, got) + } +} + +func Test_urlToLabel_retainRoot(t *testing.T) { + have := "/" + want := have + got := urlToLabel(have) + + if got != want { + t.Errorf("want %s, got %s", want, got) + } +} -- GitLab