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

View File

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