~jan0sch/smederee

Showing details for patch 292a0f576e06362f72b8333a5c99540f32b4d398.
2024-06-10 (Mon), 10:30 AM - Jens Grassel - 292a0f576e06362f72b8333a5c99540f32b4d398

hub: Adjust error logging to include request id.

Summary of changes
1 files modified with 21 lines added and 9 lines removed
  • modules/hub/src/main/scala/de/smederee/hub/HubServer.scala with 21 added and 9 removed lines
diff -rN -u old-smederee/modules/hub/src/main/scala/de/smederee/hub/HubServer.scala new-smederee/modules/hub/src/main/scala/de/smederee/hub/HubServer.scala
--- old-smederee/modules/hub/src/main/scala/de/smederee/hub/HubServer.scala	2025-01-11 17:32:20.145962234 +0000
+++ new-smederee/modules/hub/src/main/scala/de/smederee/hub/HubServer.scala	2025-01-11 17:32:20.145962234 +0000
@@ -47,6 +47,7 @@
 import org.http4s.server.middleware.RequestId
 import org.http4s.server.staticcontent.resourceServiceBuilder
 import org.slf4j.LoggerFactory
+import org.typelevel.ci.CIStringSyntax
 import pureconfig.*
 import scodec.bits.ByteVector
 
@@ -130,14 +131,25 @@
         }
     }
 
-    /** An error handler that simply logs error messages but nothing more.
+    /** Log message failures and other errors.
       *
-      * @param exception
-      *   An exception thrown by underlying code.
-      * @param message
-      *   An error message.
+      * @param request
+      *   The request during which the error occured.
+      * @param error
+      *   The error (exception) thrown during the request.
       */
-    private def handleError(exception: Throwable, message: => String) = IO(log.error(message, exception))
+    private def logErrors(request: Request[IO], error: Throwable): IO[Unit] =
+        IO(request.headers.get(ci"X-Request-ID").fold("null")(_.head.value)).map { reqId =>
+            val message = (request, error) match {
+                case (req, _: MessageFailure) =>
+                    s"""Message failure handling request $reqId: ${request.method} ${request.pathInfo} from ${request.remoteAddr
+                            .getOrElse("<unknown>")}"""
+                case (req, _) =>
+                    s"""Error servicing request $reqId: ${request.method} ${request.pathInfo} from ${request.remoteAddr
+                            .getOrElse("<unknown>")}"""
+            }
+            log.error(message, error)
+        }
 
     /** Try to load the CSRF key from the given path. If it doesn't exist or fails then a new key is generated and
       * stored in the file.
@@ -557,11 +569,11 @@
                                 )
                         )
                     )
+                    // Currently we simply log errors and recover.
                     serviceWithErrorHandling = ErrorHandling.Recover.total(
-                        ErrorAction.log(
+                        ErrorAction(
                             hubWebService,
-                            messageFailureLogAction = handleError,
-                            serviceErrorLogAction = handleError
+                            logErrors
                         )
                     )
                     // Create our webserver fiber.