Browse Source

Add prematurelyClosed metadata to request logs

Leszek Wiesner 3 years ago
parent
commit
50ca49dd4c
1 changed files with 7 additions and 1 deletions
  1. 7 1
      distributor-node/src/services/httpApi/HttpApiService.ts

+ 7 - 1
distributor-node/src/services/httpApi/HttpApiService.ts

@@ -26,7 +26,10 @@ export class HttpApiService {
   ) {
     return async (req: express.Request<T>, res: express.Response, next: express.NextFunction) => {
       // Fix for express-winston in order to also log prematurely closed requests
-      res.on('close', () => res.end())
+      res.on('close', () => {
+        res.locals.prematurelyClosed = !res.writableFinished
+        res.end()
+      })
       try {
         await handler(req, res, next)
       } catch (err) {
@@ -56,6 +59,9 @@ export class HttpApiService {
       expressWinston.logger({
         winstonInstance: this.logger,
         level: 'http',
+        dynamicMeta: (req, res) => {
+          return { prematurelyClosed: res.locals.prematurelyClosed ?? false }
+        },
       })
     )