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

Commit

Permalink
Fix namespace for metrics invocations
Browse files Browse the repository at this point in the history
The metrics for OpenFaaS now have a namespace suffix so
that it is needed when looking up Prometheus metrics.

This change also fixes the proxy issue with create-react-app
now deprecating proxy settings in the package.json

Fixes: #700

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
alexellis committed Dec 1, 2020
1 parent 40adc94 commit ff1aa45
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ const FunctionDetailSummary = ({
const fnLogsButton = (
<Button outline color="secondary" size="xs" tag={Link} to={toFnLogs}>
<FontAwesomeIcon icon="folder-open" className="mr-2" />
<span>Invocation Logs</span>
<span>Logs</span>
</Button>
);

Expand Down Expand Up @@ -178,7 +178,7 @@ const FunctionDetailSummary = ({
</div>
<div className="col-lg-4">
<FunctionOverviewPanel
headerText="Invocations"
headerText="Runtime"
headerIcon={invocationsIcon}
button={fnLogsButton}
>
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/openfaas/openfaas-cloud

go 1.13

require github.com/openfaas/openfaas-cloud/metrics v0.0.0-20201201105924-2f2413a8b8ab // indirect
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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/openfaas/openfaas-cloud/metrics v0.0.0-20201201105924-2f2413a8b8ab h1:xDX+6thocabwas9PrC9qcs59xzLgBcnhjLFhowP3Mp8=
github.com/openfaas/openfaas-cloud/metrics v0.0.0-20201201105924-2f2413a8b8ab/go.mod h1:U+JISKIa+CMKs8ykMHarPuz+k/uVz5wTNWCUywpK944=
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=
38 changes: 25 additions & 13 deletions metrics/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/url"
"os"
"strconv"
"strings"

"github.com/openfaas/faas/gateway/metrics"
)
Expand All @@ -19,11 +20,14 @@ type Metrics struct {

// Handle exposes the OpenFaaS instance metrics
func Handle(req []byte) string {
fnName, err := parseFunctionName()

fnName, ns, err := parseFunctionName()
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"
}

host := os.Getenv("prometheus_host")
envPort := os.Getenv("prometheus_port")
Expand All @@ -32,18 +36,19 @@ func Handle(req []byte) string {
log.Fatalf("Could not convert env-var prometheus_port to int. Env-var value: %s. Error: %t", envPort, err)
}

metricsQuery := metrics.NewPrometheusQuery(host, port, &http.Client{})
metricsQuery := metrics.NewPrometheusQuery(host, port, http.DefaultClient)
metricsWindow := parseMetricsWindow()

fnMetrics, err := getMetrics(fnName, metricsQuery, metricsWindow)
key := fnName + "." + ns
fnMetrics, err := getMetrics(key, metricsQuery, metricsWindow)
if err != nil {
log.Fatalf("Couldn't get metrics from Prometheus for function %s, %t", fnName, err)
log.Fatalf("Couldn't get metrics from Prometheus for function %s, %t", key, err)
}

res, err := json.Marshal(fnMetrics)
if err != nil {
log.Fatalf("Couldn't marshal json %t", err)
}

return string(res)
}

Expand All @@ -67,20 +72,27 @@ func parseMetricsWindow() string {
return metricsWindow
}

func parseFunctionName() (functionName string, error error) {
func parseFunctionName() (name, namespace string, error error) {
if query, exists := os.LookupEnv("Http_Query"); exists {
vals, _ := url.ParseQuery(query)
val, err := url.ParseQuery(query)
if err != nil {
return "", "", err
}

functionNameQuery := vals.Get("function")
if functionName := val.Get("function"); len(functionName) > 0 {
name = functionName
if index := strings.Index(functionName, "."); index > -1 {
name = functionName[:index]
namespace = functionName[index:]
}

if len(functionNameQuery) > 0 {
return functionNameQuery, nil
return name, namespace, nil
}

return "", fmt.Errorf("there is no `function` inside env var Http_Query")
return "", "", fmt.Errorf("there is no `function` inside env var Http_Query")
}

return "", fmt.Errorf("unable to parse Http_Query")
return "", "", fmt.Errorf("unable to parse Http_Query")
}

func getMetrics(fnName string, metricsQuery metrics.PrometheusQueryFetcher, metricsWindow string) (*Metrics, error) {
Expand Down

0 comments on commit ff1aa45

Please sign in to comment.