mirror of
https://github.com/openfaas/faas.git
synced 2025-06-08 16:26:47 +00:00
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>
This commit is contained in:
parent
67c9a71686
commit
a26d350376
@ -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, "/")
|
||||
}
|
||||
return result
|
||||
if path == "" {
|
||||
path = "/"
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
// PrometheusFunctionNotifier records metrics to Prometheus
|
||||
|
23
gateway/handlers/notifiers_test.go
Normal file
23
gateway/handlers/notifiers_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user