Skip to content
Snippets Groups Projects
Commit c716c548 authored by Alex Ellis (VMware)'s avatar Alex Ellis (VMware) Committed by Alex Ellis
Browse files

Break out logging / metrics for functions in forwarding_proxy

parent c484eeec
No related branches found
No related tags found
No related merge requests found
FROM golang:1.9.4 as build FROM golang:1.9.4 as build
WORKDIR /go/src/github.com/openfaas/faas/gateway WORKDIR /go/src/github.com/openfaas/faas/gateway
RUN curl -sL https://github.com/alexellis/license-check/releases/download/0.1/license-check > /usr/bin/license-check && chmod +x /usr/bin/license-check RUN curl -sL https://github.com/alexellis/license-check/releases/download/0.2.2/license-check \
> /usr/bin/license-check \
&& chmod +x /usr/bin/license-check
COPY vendor vendor COPY vendor vendor
...@@ -16,7 +18,7 @@ COPY plugin plugin ...@@ -16,7 +18,7 @@ COPY plugin plugin
COPY server.go . COPY server.go .
# Run a gofmt and exclude all vendored code. # Run a gofmt and exclude all vendored code.
RUN license-check -path ./ --verbose=false \ RUN license-check -path ./ --verbose=false "Alex Ellis" "OpenFaaS Project" \
&& test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*"))" \ && test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*"))" \
&& go test $(go list ./... | grep -v integration | grep -v /vendor/ | grep -v /template/) -cover \ && go test $(go list ./... | grep -v integration | grep -v /vendor/ | grep -v /template/) -cover \
&& CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o gateway . && CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o gateway .
......
...@@ -14,8 +14,12 @@ import ( ...@@ -14,8 +14,12 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
type HTTPNotifier interface {
Notify(method string, URL string, statusCode int, duration time.Duration)
}
// MakeForwardingProxyHandler create a handler which forwards HTTP requests // MakeForwardingProxyHandler create a handler which forwards HTTP requests
func MakeForwardingProxyHandler(proxy *types.HTTPClientReverseProxy, metrics *metrics.MetricOptions) http.HandlerFunc { func MakeForwardingProxyHandler(proxy *types.HTTPClientReverseProxy, notifiers []HTTPNotifier) http.HandlerFunc {
baseURL := proxy.BaseURL.String() baseURL := proxy.BaseURL.String()
if strings.HasSuffix(baseURL, "/") { if strings.HasSuffix(baseURL, "/") {
baseURL = baseURL[0 : len(baseURL)-1] baseURL = baseURL[0 : len(baseURL)-1]
...@@ -24,34 +28,18 @@ func MakeForwardingProxyHandler(proxy *types.HTTPClientReverseProxy, metrics *me ...@@ -24,34 +28,18 @@ func MakeForwardingProxyHandler(proxy *types.HTTPClientReverseProxy, metrics *me
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
requestURL := r.URL.String() requestURL := r.URL.String()
serviceName := getServiceName(requestURL)
log.Printf("> Forwarding [%s] to %s", r.Method, requestURL)
start := time.Now() start := time.Now()
statusCode, err := forwardRequest(w, r, proxy.Client, baseURL, requestURL, proxy.Timeout) statusCode, err := forwardRequest(w, r, proxy.Client, baseURL, requestURL, proxy.Timeout)
seconds := time.Since(start)
if err != nil { if err != nil {
log.Printf("error with upstream request to: %s, %s\n", requestURL, err.Error()) log.Printf("error with upstream request to: %s, %s\n", requestURL, err.Error())
} }
for _, notifier := range notifiers {
seconds := time.Since(start).Seconds() notifier.Notify(r.Method, requestURL, statusCode, seconds)
log.Printf("< [%s] - %d took %f seconds\n", r.URL.String(),
statusCode, seconds)
if len(serviceName) > 0 {
metrics.GatewayFunctionsHistogram.
WithLabelValues(serviceName).
Observe(seconds)
code := strconv.Itoa(statusCode)
metrics.GatewayFunctionInvocation.
With(prometheus.Labels{"function_name": serviceName, "code": code}).
Inc()
} }
} }
} }
...@@ -102,6 +90,25 @@ func copyHeaders(destination http.Header, source *http.Header) { ...@@ -102,6 +90,25 @@ func copyHeaders(destination http.Header, source *http.Header) {
} }
} }
type PrometheusFunctionNotifier struct {
Metrics *metrics.MetricOptions
}
func (p PrometheusFunctionNotifier) Notify(method string, URL string, statusCode int, duration time.Duration) {
seconds := duration.Seconds()
serviceName := getServiceName(URL)
p.Metrics.GatewayFunctionsHistogram.
WithLabelValues(serviceName).
Observe(seconds)
code := strconv.Itoa(statusCode)
p.Metrics.GatewayFunctionInvocation.
With(prometheus.Labels{"function_name": serviceName, "code": code}).
Inc()
}
func getServiceName(urlValue string) string { func getServiceName(urlValue string) string {
var serviceName string var serviceName string
forward := "/function/" forward := "/function/"
...@@ -114,3 +121,10 @@ func getServiceName(urlValue string) string { ...@@ -114,3 +121,10 @@ func getServiceName(urlValue string) string {
func startsWith(value, token string) bool { func startsWith(value, token string) bool {
return len(value) > len(token) && strings.Index(value, token) == 0 return len(value) > len(token) && strings.Index(value, token) == 0
} }
type LoggingNotifier struct {
}
func (LoggingNotifier) Notify(method string, URL string, statusCode int, duration time.Duration) {
log.Printf("Forwarded [%s] to %s - [%d] - %f seconds", method, URL, statusCode, duration.Seconds())
}
...@@ -10,8 +10,8 @@ import ( ...@@ -10,8 +10,8 @@ import (
"time" "time"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/openfaas/faas/gateway/handlers"
internalHandlers "github.com/openfaas/faas/gateway/handlers"
"github.com/openfaas/faas/gateway/metrics" "github.com/openfaas/faas/gateway/metrics"
"github.com/openfaas/faas/gateway/plugin" "github.com/openfaas/faas/gateway/plugin"
"github.com/openfaas/faas/gateway/types" "github.com/openfaas/faas/gateway/types"
...@@ -42,17 +42,24 @@ func main() { ...@@ -42,17 +42,24 @@ func main() {
reverseProxy := types.NewHTTPClientReverseProxy(config.FunctionsProviderURL, config.UpstreamTimeout) reverseProxy := types.NewHTTPClientReverseProxy(config.FunctionsProviderURL, config.UpstreamTimeout)
faasHandlers.Proxy = internalHandlers.MakeForwardingProxyHandler(reverseProxy, &metricsOptions) loggingNotifier := handlers.LoggingNotifier{}
faasHandlers.RoutelessProxy = internalHandlers.MakeForwardingProxyHandler(reverseProxy, &metricsOptions) prometheusNotifier := handlers.PrometheusFunctionNotifier{
faasHandlers.ListFunctions = internalHandlers.MakeForwardingProxyHandler(reverseProxy, &metricsOptions) Metrics: &metricsOptions,
faasHandlers.DeployFunction = internalHandlers.MakeForwardingProxyHandler(reverseProxy, &metricsOptions) }
faasHandlers.DeleteFunction = internalHandlers.MakeForwardingProxyHandler(reverseProxy, &metricsOptions) functionNotifiers := []handlers.HTTPNotifier{loggingNotifier, prometheusNotifier}
faasHandlers.UpdateFunction = internalHandlers.MakeForwardingProxyHandler(reverseProxy, &metricsOptions) forwardingNotifiers := []handlers.HTTPNotifier{loggingNotifier, prometheusNotifier}
faasHandlers.Proxy = handlers.MakeForwardingProxyHandler(reverseProxy, functionNotifiers)
faasHandlers.RoutelessProxy = handlers.MakeForwardingProxyHandler(reverseProxy, forwardingNotifiers)
faasHandlers.ListFunctions = handlers.MakeForwardingProxyHandler(reverseProxy, forwardingNotifiers)
faasHandlers.DeployFunction = handlers.MakeForwardingProxyHandler(reverseProxy, forwardingNotifiers)
faasHandlers.DeleteFunction = handlers.MakeForwardingProxyHandler(reverseProxy, forwardingNotifiers)
faasHandlers.UpdateFunction = handlers.MakeForwardingProxyHandler(reverseProxy, forwardingNotifiers)
queryFunction := internalHandlers.MakeForwardingProxyHandler(reverseProxy, &metricsOptions) queryFunction := handlers.MakeForwardingProxyHandler(reverseProxy, forwardingNotifiers)
alertHandler := plugin.NewExternalServiceQuery(*config.FunctionsProviderURL) alertHandler := plugin.NewExternalServiceQuery(*config.FunctionsProviderURL)
faasHandlers.Alert = internalHandlers.MakeAlertHandler(alertHandler) faasHandlers.Alert = handlers.MakeAlertHandler(alertHandler)
metrics.AttachExternalWatcher(*config.FunctionsProviderURL, metricsOptions, "func", servicePollInterval) metrics.AttachExternalWatcher(*config.FunctionsProviderURL, metricsOptions, "func", servicePollInterval)
...@@ -63,13 +70,13 @@ func main() { ...@@ -63,13 +70,13 @@ func main() {
log.Fatalln(queueErr) log.Fatalln(queueErr)
} }
faasHandlers.QueuedProxy = internalHandlers.MakeQueuedProxy(metricsOptions, true, natsQueue) faasHandlers.QueuedProxy = handlers.MakeQueuedProxy(metricsOptions, true, natsQueue)
faasHandlers.AsyncReport = internalHandlers.MakeAsyncReport(metricsOptions) faasHandlers.AsyncReport = handlers.MakeAsyncReport(metricsOptions)
} }
prometheusQuery := metrics.NewPrometheusQuery(config.PrometheusHost, config.PrometheusPort, &http.Client{}) prometheusQuery := metrics.NewPrometheusQuery(config.PrometheusHost, config.PrometheusPort, &http.Client{})
listFunctions := metrics.AddMetricsHandler(faasHandlers.ListFunctions, prometheusQuery) listFunctions := metrics.AddMetricsHandler(faasHandlers.ListFunctions, prometheusQuery)
faasHandlers.Proxy = internalHandlers.MakeCallIDMiddleware(faasHandlers.Proxy) faasHandlers.Proxy = handlers.MakeCallIDMiddleware(faasHandlers.Proxy)
r := mux.NewRouter() r := mux.NewRouter()
// r.StrictSlash(false) // This didn't work, so register routes twice. // r.StrictSlash(false) // This didn't work, so register routes twice.
...@@ -95,7 +102,7 @@ func main() { ...@@ -95,7 +102,7 @@ func main() {
// This URL allows access from the UI to the OpenFaaS store // This URL allows access from the UI to the OpenFaaS store
allowedCORSHost := "raw.githubusercontent.com" allowedCORSHost := "raw.githubusercontent.com"
fsCORS := internalHandlers.DecorateWithCORS(fs, allowedCORSHost) fsCORS := handlers.DecorateWithCORS(fs, allowedCORSHost)
r.PathPrefix("/ui/").Handler(http.StripPrefix("/ui", fsCORS)).Methods("GET") r.PathPrefix("/ui/").Handler(http.StripPrefix("/ui", fsCORS)).Methods("GET")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment