disable printing invocation timing in stderr

Signed-off-by: Nitishkumar Singh <nitishkumarsingh71@gmail.com>
This commit is contained in:
Nitishkumar Singh 2023-10-22 16:31:40 +05:30 committed by Alex Ellis
parent 5356fca4c5
commit 078043b168
6 changed files with 38 additions and 21 deletions

View File

@ -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),

2
go.mod
View File

@ -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

4
go.sum
View File

@ -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=

View File

@ -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)

View File

@ -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

2
vendor/modules.txt generated vendored
View File

@ -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