Skip to content
This repository has been archived by the owner on Mar 10, 2023. It is now read-only.

Commit

Permalink
Fix issue in namespace parsing for metrics function
Browse files Browse the repository at this point in the history
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) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Dec 15, 2020
1 parent b4ad89b commit ffbb847
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
18 changes: 18 additions & 0 deletions metrics/go.sum
Original file line number Diff line number Diff line change
@@ -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=
11 changes: 9 additions & 2 deletions metrics/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -101,6 +107,7 @@ func getMetrics(fnName string, metricsQuery metrics.PrometheusQueryFetcher, metr
fnName,
metricsWindow,
)

expr := url.QueryEscape(queryValue)

response, err := metricsQuery.Fetch(expr)
Expand Down
32 changes: 32 additions & 0 deletions metrics/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}

Expand Down

0 comments on commit ffbb847

Please sign in to comment.