From a557e278bd4dc1aaca8b052bb3575df536f0a774 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Mon, 23 Sep 2024 15:14:07 +0200 Subject: [PATCH] add_response_header_procesing_time --- CHANGES_NEXT_RELEASE | 2 +- Changelog | 2 ++ lib/bindings/HTTPBindings.js | 8 ++++++++ lib/constants.js | 4 +++- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index d3f5a12f..3a7b7bcb 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1 +1 @@ - +- ADD: reponse header about procesing time (iotagent-node-lib#1650) diff --git a/Changelog b/Changelog index 905a4720..9584183c 100644 --- a/Changelog +++ b/Changelog @@ -1,3 +1,5 @@ +- ADD: reponse header about procesing time (iotagent-node-lib#1650) + 3.6.0 (September 18th, 2024) - Upgrade express from 4.19.2 to 4.20.0 due to a vulnerability diff --git a/lib/bindings/HTTPBindings.js b/lib/bindings/HTTPBindings.js index 44f1f11b..654f264f 100644 --- a/lib/bindings/HTTPBindings.js +++ b/lib/bindings/HTTPBindings.js @@ -46,6 +46,11 @@ let context = { op: 'IOTAUL.HTTP.Binding' }; +function reqTiming(req, res, next) { + req.startTime = Date.now(); + next(); +} + /* eslint-disable-next-line no-unused-vars */ function handleError(error, req, res, next) { let code = 500; @@ -198,6 +203,7 @@ function returnCommands(req, res, next) { if (req.query && req.query.getCmd === '1') { iotAgentLib.commandQueue(req.device.service, req.device.subservice, req.deviceId, function (error, list) { + res.set(constants.X_PROCESSING_TIME, Date.now() - req.startTime + ' ms '); if (error || !list || list.count === 0) { res.set('Content-Type', 'text/plain'); res.status(200).send(''); @@ -209,6 +215,7 @@ function returnCommands(req, res, next) { }); } else { res.set('Content-Type', 'text/plain'); + res.set(constants.X_PROCESSING_TIME, Date.now() - req.startTime + ' ms '); res.status(200).send(''); } } @@ -477,6 +484,7 @@ function start(callback) { httpBindingServer.app.set('port', config.getConfig().http.port); httpBindingServer.app.set('host', config.getConfig().http.host || '0.0.0.0'); + httpBindingServer.app.use(reqTiming); httpBindingServer.router.get( config.getConfig().iota.defaultResource || constants.HTTP_MEASURE_PATH, diff --git a/lib/constants.js b/lib/constants.js index d299b870..dee1d4f0 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -53,5 +53,7 @@ module.exports = { AMQP_DEFAULT_QUEUE: 'iotaqueue', AMQP_DEFAULT_DURABLE: true, AMQP_DEFAULT_RETRIES: 5, - AMQP_DEFAULT_RETRY_TIME: 5 + AMQP_DEFAULT_RETRY_TIME: 5, + + X_PROCESSING_TIME: 'X-Processing-Time' };