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

30 lines
865 B
JavaScript
Raw Normal View History

'use strict';
const Profiler = require('../../stats/profiler-proxy');
const debug = require('debug')('windshaft:cartodb:stats');
const onHeaders = require('on-headers');
2018-03-02 01:12:07 +08:00
module.exports = function stats (options) {
const { enabled = true, statsClient } = options;
2018-03-02 01:12:07 +08:00
return function statsMiddleware (req, res, next) {
req.profiler = new Profiler({
statsd_client: statsClient,
profile: enabled
});
onHeaders(res, () => res.set('X-Tiler-Profiler', req.profiler.toJSONString()));
res.on('finish', () => {
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
};