CartoDB-SQL-API/lib/api/middlewares/profiler.js

33 lines
870 B
JavaScript
Raw Normal View History

2018-10-24 21:42:33 +08:00
'use strict';
const Profiler = require('../../stats/profiler-proxy');
const { name: prefix } = require('../../../package.json');
module.exports = function profiler ({ statsClient }) {
return function profilerMiddleware (req, res, next) {
const start = new Date();
const { logger } = res.locals;
req.profiler = new Profiler({
profile: global.settings.useProfiler,
statsd_client: statsClient
});
req.profiler.start(prefix);
res.on('finish', () => {
req.profiler.add({ response: new Date() - start });
req.profiler.end();
logger.info({ stats: req.profiler.toJSON() });
try {
req.profiler.sendStats();
} catch (err) {
logger.warn({ error: err });
}
});
next();
};
};