From 078043b168d8e18fa2671de9cc1f9aca27d4e9ba Mon Sep 17 00:00:00 2001 From: Nitishkumar Singh Date: Sun, 22 Oct 2023 16:31:40 +0530 Subject: [PATCH] disable printing invocation timing in stderr Signed-off-by: Nitishkumar Singh --- cmd/provider.go | 2 +- go.mod | 2 +- go.sum | 4 +-- .../openfaas/faas-provider/proxy/proxy.go | 27 ++++++++++++++----- .../openfaas/faas-provider/types/queue.go | 22 ++++++++------- vendor/modules.txt | 2 +- 6 files changed, 38 insertions(+), 21 deletions(-) diff --git a/cmd/provider.go b/cmd/provider.go index d7e724f..a59ccc8 100644 --- a/cmd/provider.go +++ b/cmd/provider.go @@ -93,7 +93,7 @@ func makeProviderCmd() *cobra.Command { } bootstrapHandlers := types.FaaSHandlers{ - FunctionProxy: proxy.NewHandlerFunc(*config, invokeResolver), + FunctionProxy: proxy.NewHandlerFunc(*config, invokeResolver, false), DeleteFunction: handlers.MakeDeleteHandler(client, cni), DeployFunction: handlers.MakeDeployHandler(client, cni, baseUserSecretsPath, alwaysPull), FunctionLister: handlers.MakeReadHandler(client), diff --git a/go.mod b/go.mod index 998e89e..baf45a6 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/gorilla/mux v1.8.0 github.com/morikuni/aec v1.0.0 github.com/opencontainers/runtime-spec v1.1.0-rc.3 - github.com/openfaas/faas-provider v0.24.0 + github.com/openfaas/faas-provider v0.24.4 github.com/pkg/errors v0.9.1 github.com/sethvargo/go-password v0.2.0 github.com/spf13/cobra v1.7.0 diff --git a/go.sum b/go.sum index a95946d..85f3128 100644 --- a/go.sum +++ b/go.sum @@ -188,8 +188,8 @@ github.com/opencontainers/runtime-spec v1.1.0-rc.3/go.mod h1:jwyrGlmzljRJv/Fgzds github.com/opencontainers/selinux v1.10.0/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI= github.com/opencontainers/selinux v1.11.0 h1:+5Zbo97w3Lbmb3PeqQtpmTkMwsW5nRI3YaLpt7tQ7oU= github.com/opencontainers/selinux v1.11.0/go.mod h1:E5dMC3VPuVvVHDYmi78qvhJp8+M586T4DlDRYpFkyec= -github.com/openfaas/faas-provider v0.24.0 h1:5ToqdkqZ3pM9SdFKBMUmhU8IjXMh6+qd7gEDBeFhp1M= -github.com/openfaas/faas-provider v0.24.0/go.mod h1:NsETIfEndZn4mn/w/XnBTcDTwKqULCziphLp7KgeRcA= +github.com/openfaas/faas-provider v0.24.4 h1:Zzbkabgd0PoQmnRjy53NbMXjhLaIyoIiwP3qaLkm9rE= +github.com/openfaas/faas-provider v0.24.4/go.mod h1:NsETIfEndZn4mn/w/XnBTcDTwKqULCziphLp7KgeRcA= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= diff --git a/vendor/github.com/openfaas/faas-provider/proxy/proxy.go b/vendor/github.com/openfaas/faas-provider/proxy/proxy.go index fdd5559..be0980f 100644 --- a/vendor/github.com/openfaas/faas-provider/proxy/proxy.go +++ b/vendor/github.com/openfaas/faas-provider/proxy/proxy.go @@ -49,6 +49,8 @@ type BaseURLResolver interface { } // NewHandlerFunc creates a standard http.HandlerFunc to proxy function requests. +// When verbose is set to true, the timing of each invocation will be printed out to +// stderr. // The returned http.HandlerFunc will ensure: // // - proper proxy request timeouts @@ -58,7 +60,7 @@ type BaseURLResolver interface { // - logging errors and proxy request timing to stdout // // Note that this will panic if `resolver` is nil. -func NewHandlerFunc(config types.FaaSConfig, resolver BaseURLResolver) http.HandlerFunc { +func NewHandlerFunc(config types.FaaSConfig, resolver BaseURLResolver, verbose bool) http.HandlerFunc { if resolver == nil { panic("NewHandlerFunc: empty proxy handler resolver, cannot be nil") } @@ -78,7 +80,7 @@ func NewHandlerFunc(config types.FaaSConfig, resolver BaseURLResolver) http.Hand http.MethodGet, http.MethodOptions, http.MethodHead: - proxyRequest(w, r, proxyClient, resolver) + proxyRequest(w, r, proxyClient, resolver, verbose) default: w.WriteHeader(http.StatusMethodNotAllowed) @@ -131,26 +133,33 @@ func NewProxyClient(timeout time.Duration, maxIdleConns int, maxIdleConnsPerHost } // proxyRequest handles the actual resolution of and then request to the function service. -func proxyRequest(w http.ResponseWriter, originalReq *http.Request, proxyClient *http.Client, resolver BaseURLResolver) { +func proxyRequest(w http.ResponseWriter, originalReq *http.Request, proxyClient *http.Client, resolver BaseURLResolver, verbose bool) { ctx := originalReq.Context() pathVars := mux.Vars(originalReq) functionName := pathVars["name"] if functionName == "" { + w.Header().Add("X-OpenFaaS-Internal", "proxy") + httputil.Errorf(w, http.StatusBadRequest, "Provide function name in the request path") return } - functionAddr, resolveErr := resolver.Resolve(functionName) - if resolveErr != nil { + functionAddr, err := resolver.Resolve(functionName) + if err != nil { + w.Header().Add("X-OpenFaaS-Internal", "proxy") + // TODO: Should record the 404/not found error in Prometheus. - log.Printf("resolver error: no endpoints for %s: %s\n", functionName, resolveErr.Error()) + log.Printf("resolver error: no endpoints for %s: %s\n", functionName, err.Error()) httputil.Errorf(w, http.StatusServiceUnavailable, "No endpoints available for: %s.", functionName) return } proxyReq, err := buildProxyRequest(originalReq, functionAddr, pathVars["params"]) if err != nil { + + w.Header().Add("X-OpenFaaS-Internal", "proxy") + httputil.Errorf(w, http.StatusInternalServerError, "Failed to resolve service: %s.", functionName) return } @@ -166,6 +175,8 @@ func proxyRequest(w http.ResponseWriter, originalReq *http.Request, proxyClient if err != nil { log.Printf("error with proxy request to: %s, %s\n", proxyReq.URL.String(), err.Error()) + w.Header().Add("X-OpenFaaS-Internal", "proxy") + httputil.Errorf(w, http.StatusInternalServerError, "Can't reach service for: %s.", functionName) return } @@ -174,7 +185,9 @@ func proxyRequest(w http.ResponseWriter, originalReq *http.Request, proxyClient defer response.Body.Close() } - log.Printf("%s took %f seconds\n", functionName, seconds.Seconds()) + if verbose { + log.Printf("%s took %f seconds\n", functionName, seconds.Seconds()) + } clientHeader := w.Header() copyHeaders(clientHeader, &response.Header) diff --git a/vendor/github.com/openfaas/faas-provider/types/queue.go b/vendor/github.com/openfaas/faas-provider/types/queue.go index 0713887..f8e7c66 100644 --- a/vendor/github.com/openfaas/faas-provider/types/queue.go +++ b/vendor/github.com/openfaas/faas-provider/types/queue.go @@ -8,32 +8,36 @@ import ( // Request for asynchronous processing type QueueRequest struct { // Header from HTTP request - Header http.Header + Header http.Header `json:"Header,omitempty"` // Host from HTTP request - Host string + Host string `json:"Host,omitempty"` // Body from HTTP request to use for invocation - Body []byte + Body []byte `json:"Body,omitempty"` // Method from HTTP request - Method string + Method string `json:"Method"` // Path from HTTP request - Path string + Path string `json:"Path,omitempty"` // QueryString from HTTP request - QueryString string + QueryString string `json:"QueryString,omitempty"` // Function name to invoke - Function string + Function string `json:"Function"` // QueueName to publish the request to, leave blank // for default. - QueueName string + QueueName string `json:"QueueName,omitempty"` + + // Annotations defines a collection of meta-data that can be used by + // the queue worker when processing the queued request. + Annotations map[string]string `json:"Annotations,omitempty"` // Used by queue worker to submit a result - CallbackURL *url.URL `json:"CallbackUrl"` + CallbackURL *url.URL `json:"CallbackUrl,omitempty"` } // RequestQueuer can public a request to be executed asynchronously diff --git a/vendor/modules.txt b/vendor/modules.txt index 4a1b597..b9f2753 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -301,7 +301,7 @@ github.com/opencontainers/runtime-spec/specs-go github.com/opencontainers/selinux/go-selinux github.com/opencontainers/selinux/go-selinux/label github.com/opencontainers/selinux/pkg/pwalkdir -# github.com/openfaas/faas-provider v0.24.0 +# github.com/openfaas/faas-provider v0.24.4 ## explicit; go 1.20 github.com/openfaas/faas-provider github.com/openfaas/faas-provider/auth