Skip to content

Commit

Permalink
add response time procesing header
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroVega committed Sep 23, 2024
1 parent 5a08b6b commit 7f6ddd1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
12 changes: 11 additions & 1 deletion lib/bindings/HTTPBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ function sendConfigurationToDevice(apiKey, group, deviceId, results, callback) {

function handleConfigurationRequest(req, res, next) {
function replyToDevice(error) {
res.set(constants.X_PROCESSING_TIME, Date.now() - req.startTime + ' ms ');
if (error) {
res.status(error.code).json(error);
} else {
Expand All @@ -476,6 +477,11 @@ function handleConfigurationRequest(req, res, next) {
});
}

function reqTiming(req, res, next) {
req.startTime = Date.now();
next();
}

function handleError(error, req, res, next) {
let code = 500;

Expand All @@ -484,7 +490,7 @@ function handleError(error, req, res, next) {
if (error.code && String(error.code).match(/^[2345]\d\d$/)) {
code = error.code;
}

res.set(constants.X_PROCESSING_TIME, Date.now() - req.startTime + ' ms ');
res.status(code).json({
name: error.name,
message: error.message
Expand Down Expand Up @@ -623,6 +629,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) {
if (req.accepts('json')) {
res.status(200).send({});
Expand All @@ -639,8 +646,10 @@ function returnCommands(req, res, next) {
}
});
} else if (req.accepts('json')) {
res.set(constants.X_PROCESSING_TIME, Date.now() - req.startTime + ' ms ');
res.status(200).send({});
} else {
res.set(constants.X_PROCESSING_TIME, Date.now() - req.startTime + ' ms ');
res.status(200).send('');
}
}
Expand All @@ -664,6 +673,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,
Expand Down
4 changes: 3 additions & 1 deletion lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,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'
};

0 comments on commit 7f6ddd1

Please sign in to comment.