diff --git a/internal/default.go b/internal/default.go index 79f9810..61bf5d9 100644 --- a/internal/default.go +++ b/internal/default.go @@ -30,8 +30,8 @@ func Run(ctx context.Context, database *sqlx.DB, migrationsPrefix string, env fu ctx, cancel := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM) defer cancel() - otelEnabled := core.IsOtelEnabled(env) - if otelEnabled { + isOtelEnabled := core.IsOtelEnabled(env) + if isOtelEnabled { // use context.Background(), otherwise the shutdown can't be called, as the context is already cancelled otelShutdown, err := setupOTelSDK(context.Background()) if err != nil { @@ -67,7 +67,7 @@ func Run(ctx context.Context, database *sqlx.DB, migrationsPrefix string, env fu // init server httpServer := &http.Server{ Addr: ":" + serverSettings.Port, - Handler: createHandlerWithServices(ctx, database, serverSettings), + Handler: createHandlerWithServices(ctx, database, serverSettings, isOtelEnabled), ReadHeaderTimeout: 2 * time.Second, } go startServer(ctx, httpServer) @@ -106,7 +106,7 @@ func shutdownServer(ctx context.Context, s *http.Server, wg *sync.WaitGroup) { } } -func createHandlerWithServices(ctx context.Context, d *sqlx.DB, serverSettings *core.Settings) http.Handler { +func createHandlerWithServices(ctx context.Context, d *sqlx.DB, serverSettings *core.Settings, isOtelEnabled bool) http.Handler { var router = http.NewServeMux() authDb := authentication.NewDbSqlite(d) @@ -155,7 +155,7 @@ func createHandlerWithServices(ctx context.Context, d *sqlx.DB, serverSettings * middleware.CrossSiteRequestForgery(authService), middleware.Authenticate(authService), middleware.Gzip, - middleware.Log, + middleware.Log(isOtelEnabled), ) wrapper = otelhttp.NewHandler(wrapper, "http.request") diff --git a/internal/handler/middleware/logger.go b/internal/handler/middleware/logger.go index bb9d54f..bb0507d 100644 --- a/internal/handler/middleware/logger.go +++ b/internal/handler/middleware/logger.go @@ -17,21 +17,28 @@ func (w *WrappedWriter) WriteHeader(code int) { w.ResponseWriter.WriteHeader(code) } -func Log(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - start := time.Now() +func Log(enabled bool) func(http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if !enabled { + next.ServeHTTP(w, r) + return + } - wrapped := &WrappedWriter{ - ResponseWriter: w, - StatusCode: http.StatusOK, - } - next.ServeHTTP(wrapped, r) + start := time.Now() - slog.InfoContext(r.Context(), "request", - "remoteAddr", r.RemoteAddr, - "status", wrapped.StatusCode, - "method", r.Method, - "path", r.URL.Path, - "duration", time.Since(start).String()) - }) + wrapped := &WrappedWriter{ + ResponseWriter: w, + StatusCode: http.StatusOK, + } + next.ServeHTTP(wrapped, r) + + slog.InfoContext(r.Context(), "request", + "remoteAddr", r.RemoteAddr, + "status", wrapped.StatusCode, + "method", r.Method, + "path", r.URL.Path, + "duration", time.Since(start).String()) + }) + } }