From ffbb84724081c19ca8b4714e4121e6ead36fb1ef Mon Sep 17 00:00:00 2001 From: "Alex Ellis (OpenFaaS Ltd)" Date: Tue, 15 Dec 2020 12:09:43 +0000 Subject: [PATCH] Fix issue in namespace parsing for metrics function Fixes an issue where the . was being returned in the namespace by adding a unit test to verify the behaviour. Tested e2e with local openfaas-cloud. Signed-off-by: Alex Ellis (OpenFaaS Ltd) --- metrics/go.sum | 18 ++++++++++++++++++ metrics/handler.go | 11 +++++++++-- metrics/handler_test.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 metrics/go.sum diff --git a/metrics/go.sum b/metrics/go.sum new file mode 100644 index 000000000..51932e2b9 --- /dev/null +++ b/metrics/go.sum @@ -0,0 +1,18 @@ +github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0= +github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= +github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= +github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/openfaas/faas v0.0.0-20191227175319-80b6976c1063 h1:9dxY2GyAGl6j3+g4RN0gHsDIk3dEHuKmqlB2mkGlJ7c= +github.com/openfaas/faas v0.0.0-20191227175319-80b6976c1063/go.mod h1:E0m2rLup0Vvxg53BKxGgaYAGcZa3Xl+vvL7vSi5yQ14= +github.com/openfaas/faas-provider v0.0.0-20191011092439-98c25c3919da h1:IgAWwOVcLrPNc495areghkleecv3JjciOkEHpavm7go= +github.com/openfaas/faas-provider v0.0.0-20191011092439-98c25c3919da/go.mod h1:W4OIp33RUOpR7wW+omJB/7GhIydRmYXvKf/VqUKI4yM= +github.com/prometheus/client_golang v0.8.0 h1:1921Yw9Gc3iSc4VQh3PIoOqgPCZS7G/4xQNVUp8Mda8= +github.com/prometheus/client_golang v0.8.0/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= +github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 h1:idejC8f05m9MGOsuEi1ATq9shN03HrxNkD/luQvxCv8= +github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= +github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e h1:n/3MEhJQjQxrOUCzh1Y3Re6aJUUWRp2M9+Oc3eVn/54= +github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= +github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273 h1:agujYaXJSxSo18YNX3jzl+4G6Bstwt+kqv47GS12uL0= +github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= diff --git a/metrics/handler.go b/metrics/handler.go index a91b7be10..594fa23d5 100644 --- a/metrics/handler.go +++ b/metrics/handler.go @@ -24,6 +24,7 @@ func Handle(req []byte) string { if err != nil { log.Fatalf("couldn't parse function name from query: %t", err) } + if ns == "" { // TODO: read from env-var for environments where this is overridden ns = "openfaas-fn" @@ -39,6 +40,7 @@ func Handle(req []byte) string { metricsQuery := metrics.NewPrometheusQuery(host, port, http.DefaultClient) metricsWindow := parseMetricsWindow() key := fnName + "." + ns + // log.Printf("key: %q", key) fnMetrics, err := getMetrics(key, metricsQuery, metricsWindow) if err != nil { log.Fatalf("Couldn't get metrics from Prometheus for function %s, %t", key, err) @@ -72,7 +74,11 @@ func parseMetricsWindow() string { return metricsWindow } -func parseFunctionName() (name, namespace string, error error) { +func parseFunctionName() (string, string, error) { + var ( + name, namespace string + ) + if query, exists := os.LookupEnv("Http_Query"); exists { val, err := url.ParseQuery(query) if err != nil { @@ -83,7 +89,7 @@ func parseFunctionName() (name, namespace string, error error) { name = functionName if index := strings.Index(functionName, "."); index > -1 { name = functionName[:index] - namespace = functionName[index:] + namespace = functionName[index+1:] } return name, namespace, nil @@ -101,6 +107,7 @@ func getMetrics(fnName string, metricsQuery metrics.PrometheusQueryFetcher, metr fnName, metricsWindow, ) + expr := url.QueryEscape(queryValue) response, err := metricsQuery.Fetch(expr) diff --git a/metrics/handler_test.go b/metrics/handler_test.go index d76991d6f..9d7921731 100644 --- a/metrics/handler_test.go +++ b/metrics/handler_test.go @@ -9,6 +9,38 @@ import ( "github.com/openfaas/faas/gateway/metrics" ) +func Test_parseFunctionName_NoSuffix(t *testing.T) { + os.Setenv("Http_Query", "function=alexellis-hooks&metrics_window=60m&user=alexellis") + n, ns, err := parseFunctionName() + + if err != nil { + t.Fatal(err) + } + if n != "alexellis-hooks" { + t.Fatalf("want %s, got %s", "alexellis-hooks", n) + } + + if ns != "" { + t.Fatalf("want %s, got %s", "", ns) + } +} + +func Test_parseFunctionName_WithSuffix(t *testing.T) { + os.Setenv("Http_Query", "function=alexellis-hooks.dev&metrics_window=60m&user=alexellis") + n, ns, err := parseFunctionName() + + if err != nil { + t.Fatal(err) + } + if n != "alexellis-hooks" { + t.Fatalf("want %s, got %s", "alexellis-hooks", n) + } + + if ns != "dev" { + t.Fatalf("want %s, got %s", "dev", ns) + } +} + type FakePrometheusQueryFetcher struct { }