diff --git a/common.go b/common.go index f51bf2b6..474a008a 100644 --- a/common.go +++ b/common.go @@ -19,10 +19,14 @@ package fission import ( "fmt" "net" + "net/http" "os" "os/signal" "runtime/debug" + "strings" "syscall" + + "github.com/gorilla/handlers" ) func UrlForFunction(name string) string { @@ -52,3 +56,13 @@ func IsNetworkError(err error) bool { func GetFunctionIstioServiceName(fnName, fnNamespace string) string { return fmt.Sprintf("istio-%v-%v", fnName, fnNamespace) } + +func LoggingMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requestURI := r.RequestURI + if !strings.Contains(requestURI, "healthz") { + // Call the next handler, which can be another middleware in the chain, or the final handler. + handlers.LoggingHandler(os.Stdout, next).ServeHTTP(w, r) + } + }) +} diff --git a/controller/api.go b/controller/api.go index 2f0e8a4f..960f29f8 100644 --- a/controller/api.go +++ b/controller/api.go @@ -24,7 +24,6 @@ import ( "strconv" "strings" - "github.com/gorilla/handlers" "github.com/gorilla/mux" log "github.com/sirupsen/logrus" kerrors "k8s.io/apimachinery/pkg/api/errors" @@ -209,5 +208,6 @@ func (api *API) Serve(port int) { address := fmt.Sprintf(":%v", port) log.WithFields(log.Fields{"port": port}).Info("Server started") - log.Fatal(http.ListenAndServe(address, handlers.LoggingHandler(os.Stdout, r))) + r.Use(fission.LoggingMiddleware) + log.Fatal(http.ListenAndServe(address, r)) } diff --git a/executor/api.go b/executor/api.go index c43a50d4..cd8276db 100644 --- a/executor/api.go +++ b/executor/api.go @@ -23,10 +23,8 @@ import ( "io/ioutil" "log" "net/http" - "os" "strings" - "github.com/gorilla/handlers" "github.com/gorilla/mux" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -113,5 +111,6 @@ func (executor *Executor) Serve(port int) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() executor.ndm.Run(ctx) - log.Fatal(http.ListenAndServe(address, handlers.LoggingHandler(os.Stdout, r))) + r.Use(fission.LoggingMiddleware) + log.Fatal(http.ListenAndServe(address, r)) } diff --git a/router/router.go b/router/router.go index d004a978..4497b7ae 100644 --- a/router/router.go +++ b/router/router.go @@ -44,10 +44,8 @@ import ( "fmt" "log" "net/http" - "os" "time" - "github.com/gorilla/handlers" "github.com/gorilla/mux" "github.com/fission/fission" @@ -62,6 +60,7 @@ import ( func router(ctx context.Context, httpTriggerSet *HTTPTriggerSet, resolver *functionReferenceResolver) *mutableRouter { muxRouter := mux.NewRouter() mr := NewMutableRouter(muxRouter) + muxRouter.Use(fission.LoggingMiddleware) httpTriggerSet.subscribeRouter(ctx, mr, resolver) return mr } @@ -69,7 +68,7 @@ func router(ctx context.Context, httpTriggerSet *HTTPTriggerSet, resolver *funct func serve(ctx context.Context, port int, httpTriggerSet *HTTPTriggerSet, resolver *functionReferenceResolver) { mr := router(ctx, httpTriggerSet, resolver) url := fmt.Sprintf(":%v", port) - http.ListenAndServe(url, handlers.LoggingHandler(os.Stdout, mr)) + http.ListenAndServe(url, mr) } func Start(port int, executorUrl string) { diff --git a/storagesvc/storagesvc.go b/storagesvc/storagesvc.go index f7f2a1f8..6d166f9f 100644 --- a/storagesvc/storagesvc.go +++ b/storagesvc/storagesvc.go @@ -26,7 +26,6 @@ import ( "time" "github.com/fission/fission" - "github.com/gorilla/handlers" "github.com/gorilla/mux" _ "github.com/graymeta/stow/local" log "github.com/sirupsen/logrus" @@ -169,7 +168,9 @@ func (ss *StorageService) Start(port int) { r.HandleFunc("/healthz", ss.healthHandler).Methods("GET") address := fmt.Sprintf(":%v", port) - log.Fatal(http.ListenAndServe(address, handlers.LoggingHandler(os.Stdout, r))) + + r.Use(fission.LoggingMiddleware) + log.Fatal(http.ListenAndServe(address, r)) } func RunStorageService(storageType StorageType, storagePath string, containerName string, port int, enablePruner bool) *StorageService { diff --git a/test/tests/test_logging/test_function_logs.sh b/test/tests/test_logging/test_function_logs.sh index af7034c9..62eba182 100755 --- a/test/tests/test_logging/test_function_logs.sh +++ b/test/tests/test_logging/test_function_logs.sh @@ -42,7 +42,7 @@ done log "Grabbing logs, should have 4 calls in logs" -sleep 15 +sleep 60 fission function logs --name $fn --detail > /tmp/logfile @@ -61,6 +61,7 @@ log $num logs found if [ $num -ne 4 ] then log "Test Failed: expected 4, found $num logs" + exit 1 fi log "All done."