Windshaft-cartodb/lib/api/middlewares/profiler.js

37 lines
1.1 KiB
JavaScript
Raw Normal View History

'use strict';
const Profiler = require('../../stats/profiler-proxy');
const debug = require('debug')('windshaft:cartodb:stats');
const { name: prefix } = require('../../../package.json');
module.exports = function profiler (options) {
const { enabled = true, statsClient } = options;
return function profilerMiddleware (req, res, next) {
const { logger } = res.locals;
2020-06-04 18:10:15 +08:00
// TODO: stop using profiler and log stats instead of adding them to the profiler
req.profiler = new Profiler({
statsd_client: statsClient,
profile: enabled
});
req.profiler.start(prefix);
res.on('finish', () => {
req.profiler.done('response');
2020-06-11 16:12:27 +08:00
req.profiler.end();
logger.info({ stats: req.profiler.toJSON() });
try {
// May throw due to dns, see: http://github.com/CartoDB/Windshaft/issues/166
req.profiler.sendStats();
} catch (err) {
2019-10-22 01:07:24 +08:00
debug('error sending profiling stats: ' + err);
}
});
next();
};
2017-10-08 01:16:15 +08:00
};