internal: fix web requests not having a logger set

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-09-04 13:52:47 +02:00
parent 7771c0b905
commit 75476217a0
2 changed files with 20 additions and 17 deletions

View File

@ -9,19 +9,21 @@ import (
"goauthentik.io/internal/utils/web" "goauthentik.io/internal/utils/web"
) )
func loggingMiddleware(next http.Handler) http.Handler { func loggingMiddleware(l *log.Entry) func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return func(next http.Handler) http.Handler {
span := sentry.StartSpan(r.Context(), "authentik.go.request") return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
before := time.Now() span := sentry.StartSpan(r.Context(), "authentik.go.request")
// Call the next handler, which can be another middleware in the chain, or the final handler. before := time.Now()
next.ServeHTTP(w, r) // Call the next handler, which can be another middleware in the chain, or the final handler.
after := time.Now() next.ServeHTTP(w, r)
log.WithFields(log.Fields{ after := time.Now()
"remote": r.RemoteAddr, l.WithFields(log.Fields{
"method": r.Method, "remote": r.RemoteAddr,
"took": after.Sub(before), "method": r.Method,
"host": web.GetHost(r), "took": after.Sub(before),
}).Info(r.RequestURI) "host": web.GetHost(r),
span.Finish() }).Info(r.RequestURI)
}) span.Finish()
})
}
} }

View File

@ -30,6 +30,7 @@ type WebServer struct {
} }
func NewWebServer() *WebServer { func NewWebServer() *WebServer {
l := log.WithField("logger", "authentik.g.web")
mainHandler := mux.NewRouter() mainHandler := mux.NewRouter()
if config.G.ErrorReporting.Enabled { if config.G.ErrorReporting.Enabled {
mainHandler.Use(recoveryMiddleware()) mainHandler.Use(recoveryMiddleware())
@ -37,14 +38,14 @@ func NewWebServer() *WebServer {
mainHandler.Use(handlers.ProxyHeaders) mainHandler.Use(handlers.ProxyHeaders)
mainHandler.Use(handlers.CompressHandler) mainHandler.Use(handlers.CompressHandler)
logginRouter := mainHandler.NewRoute().Subrouter() logginRouter := mainHandler.NewRoute().Subrouter()
logginRouter.Use(loggingMiddleware) logginRouter.Use(loggingMiddleware(l))
ws := &WebServer{ ws := &WebServer{
LegacyProxy: true, LegacyProxy: true,
m: mainHandler, m: mainHandler,
lh: logginRouter, lh: logginRouter,
log: log.WithField("logger", "authentik.g.web"), log: l,
} }
ws.configureStatic() ws.configureStatic()
ws.configureProxy() ws.configureProxy()