From 6857a892f7335b7fc4b32d5a2cccb4474b07acb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Thu, 23 Jul 2020 12:14:39 +0200 Subject: [PATCH] Log request with the username bound --- lib/api/middlewares/logger.js | 4 +--- lib/api/sql/copy-controller.js | 5 ++++- lib/api/sql/job-controller.js | 4 ++++ lib/api/sql/query-controller.js | 2 ++ 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/api/middlewares/logger.js b/lib/api/middlewares/logger.js index 86264678..9be06b0b 100644 --- a/lib/api/middlewares/logger.js +++ b/lib/api/middlewares/logger.js @@ -2,12 +2,10 @@ const uuid = require('uuid'); -module.exports = function initLogger ({ logger, logOnEvent = 'finish' }) { +module.exports = function initLogger ({ logger }) { return function initLoggerMiddleware (req, res, next) { const requestId = req.get('X-Request-Id') || uuid.v4(); res.locals.logger = logger.child({ request_id: requestId }); - res.locals.logger.info({ client_request: req }, 'Incoming request'); - res.on(logOnEvent, () => res.locals.logger.info({ server_response: res, status: res.statusCode }, 'Response sent')); next(); }; }; diff --git a/lib/api/sql/copy-controller.js b/lib/api/sql/copy-controller.js index 00374e24..572dc86b 100644 --- a/lib/api/sql/copy-controller.js +++ b/lib/api/sql/copy-controller.js @@ -1,6 +1,7 @@ 'use strict'; const initLogger = require('../middlewares/logger'); +const logReqRes = require('../middlewares/log-req-res'); const profiler = require('../middlewares/profiler'); const user = require('../middlewares/user'); const authorization = require('../middlewares/authorization'); @@ -32,6 +33,7 @@ module.exports = class CopyController { return [ initLogger({ logger: this.logger }), user(this.metadataBackend), + logReqRes(), profiler({ statsClient: this.statsdClient }), tag({ tags: ['ingestion', 'copyfrom'] }), rateLimits(this.userLimitsService, endpointGroup), @@ -46,8 +48,9 @@ module.exports = class CopyController { const copyToMiddlewares = endpointGroup => { return [ - initLogger({ logger: this.logger, logOnEvent: 'log' }), + initLogger({ logger: this.logger }), user(this.metadataBackend), + logReqRes({ logOnEvent: 'log' }), profiler({ statsClient: this.statsdClient, logOnEvent: 'log' }), tag({ tags: ['ingestion', 'copyto'], logOnEvent: 'log' }), bodyParser(), diff --git a/lib/api/sql/job-controller.js b/lib/api/sql/job-controller.js index ae7f36bf..662b0303 100644 --- a/lib/api/sql/job-controller.js +++ b/lib/api/sql/job-controller.js @@ -3,6 +3,7 @@ const util = require('util'); const initLogger = require('../middlewares/logger'); +const logReqRes = require('../middlewares/log-req-res'); const profiler = require('../middlewares/profiler'); const bodyParser = require('../middlewares/body-parser'); const user = require('../middlewares/user'); @@ -35,6 +36,7 @@ module.exports = class JobController { sqlRouter.post('/job', [ initLogger({ logger: this.logger }), user(this.metadataBackend), + logReqRes(), profiler({ statsClient: this.statsdClient }), tag({ tags: ['job', 'create'] }), bodyParser(), @@ -46,6 +48,7 @@ module.exports = class JobController { sqlRouter.get('/job/:job_id', [ initLogger({ logger: this.logger }), user(this.metadataBackend), + logReqRes(), profiler({ statsClient: this.statsdClient }), tag({ tags: ['job', 'retrieve'] }), bodyParser(), @@ -55,6 +58,7 @@ module.exports = class JobController { sqlRouter.delete('/job/:job_id', [ initLogger({ logger: this.logger }), user(this.metadataBackend), + logReqRes(), profiler({ statsClient: this.statsdClient }), tag({ tags: ['job', 'cancel'] }), bodyParser(), diff --git a/lib/api/sql/query-controller.js b/lib/api/sql/query-controller.js index c7dbbafb..f8be5e15 100644 --- a/lib/api/sql/query-controller.js +++ b/lib/api/sql/query-controller.js @@ -1,6 +1,7 @@ 'use strict'; const initLogger = require('../middlewares/logger'); +const logReqRes = require('../middlewares/log-req-res'); const profiler = require('../middlewares/profiler'); const bodyParser = require('../middlewares/body-parser'); const user = require('../middlewares/user'); @@ -41,6 +42,7 @@ module.exports = class QueryController { return [ initLogger({ logger: this.logger }), user(this.metadataBackend), + logReqRes(), profiler({ statsClient: this.statsdClient }), tag({ tags: ['query'] }), bodyParser(),