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,14 +9,15 @@ 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 func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
span := sentry.StartSpan(r.Context(), "authentik.go.request") span := sentry.StartSpan(r.Context(), "authentik.go.request")
before := time.Now() before := time.Now()
// Call the next handler, which can be another middleware in the chain, or the final handler. // Call the next handler, which can be another middleware in the chain, or the final handler.
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
after := time.Now() after := time.Now()
log.WithFields(log.Fields{ l.WithFields(log.Fields{
"remote": r.RemoteAddr, "remote": r.RemoteAddr,
"method": r.Method, "method": r.Method,
"took": after.Sub(before), "took": after.Sub(before),
@ -24,4 +25,5 @@ func loggingMiddleware(next http.Handler) http.Handler {
}).Info(r.RequestURI) }).Info(r.RequestURI)
span.Finish() 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()