diff --git a/lib/cartodb/controllers/base.js b/lib/cartodb/controllers/base.js index 7462f6ba..46158eee 100644 --- a/lib/cartodb/controllers/base.js +++ b/lib/cartodb/controllers/base.js @@ -1,18 +1,13 @@ -var debug = require('debug')('windshaft:cartodb'); - function BaseController() { } module.exports = BaseController; -// jshint maxcomplexity:9 BaseController.prototype.send = function(req, res, body, status, headers) { if (req.params.dbhost) { res.set('X-Served-By-DB-Host', req.params.dbhost); } - res.set('X-Tiler-Profiler', req.profiler.toJSONString()); - if (headers) { res.set(headers); } @@ -28,13 +23,4 @@ BaseController.prototype.send = function(req, res, body, status, headers) { } else { res.send(body); } - - try { - // May throw due to dns, see - // See http://github.com/CartoDB/Windshaft/issues/166 - req.profiler.sendStats(); - } catch (err) { - debug("error sending profiling stats: " + err); - } }; -// jshint maxcomplexity:6 diff --git a/lib/cartodb/middleware/error-middleware.js b/lib/cartodb/middleware/error-middleware.js index 71f6c411..acf8d99d 100644 --- a/lib/cartodb/middleware/error-middleware.js +++ b/lib/cartodb/middleware/error-middleware.js @@ -35,8 +35,6 @@ module.exports = function errorMiddleware (/* options */) { res.set('X-Served-By-DB-Host', res.locals.dbhost); } - res.set('X-Tiler-Profiler', req.profiler.toJSONString()); - res.status(statusCode); if (req.query && req.query.callback) { @@ -44,14 +42,6 @@ module.exports = function errorMiddleware (/* options */) { } else { res.json(errorResponseBody); } - - try { - // May throw due to dns, see - // See http://github.com/CartoDB/Windshaft/issues/166 - req.profiler.sendStats(); - } catch (err) { - debug("error sending profiling stats: " + err); - } }; }; diff --git a/lib/cartodb/middleware/stats.js b/lib/cartodb/middleware/stats.js new file mode 100644 index 00000000..98dde45b --- /dev/null +++ b/lib/cartodb/middleware/stats.js @@ -0,0 +1,33 @@ +const Profiler = require('../stats/profiler_proxy'); +const debug = require('debug')('windshaft:cartodb:stats'); +const methods = ['send', 'json', 'jsonp']; + +module.exports = function statsMiddleware(options) { + const { enabled = true, statsClient } = options; + + return function stats(req, res, next) { + req.profiler = new Profiler({ + statsd_client: statsClient, + profile: enabled + }); + + methods.forEach((method) => { + const originalFn = res[method]; + + res[method] = function (body) { + res.set('X-Tiler-Profiler', req.profiler.toJSONString()); + + originalFn.call(this, body); + + try { + // May throw due to dns, see: http://github.com/CartoDB/Windshaft/issues/166 + req.profiler.sendStats(); + } catch (err) { + debug("error sending profiling stats: " + err); + } + }; + }); + + next(); + }; +}; \ No newline at end of file diff --git a/lib/cartodb/server.js b/lib/cartodb/server.js index 15c7190e..377fff69 100644 --- a/lib/cartodb/server.js +++ b/lib/cartodb/server.js @@ -12,7 +12,8 @@ var VarnishHttpCacheBackend = require('./cache/backend/varnish_http'); var FastlyCacheBackend = require('./cache/backend/fastly'); var StatsClient = require('./stats/client'); -var Profiler = require('./stats/profiler_proxy'); +const stats = require('./middleware/stats'); + var RendererStatsReporter = require('./stats/reporter/renderer'); var windshaft = require('windshaft'); @@ -362,10 +363,6 @@ function bootstrap(opts) { app.use(function bootstrap$prepareRequestResponse(req, res, next) { req.context = req.context || {}; - req.profiler = new Profiler({ - statsd_client: global.statsClient, - profile: opts.useProfiler - }); if (global.environment && global.environment.api_hostname) { res.set('X-Served-By-Host', global.environment.api_hostname); @@ -374,6 +371,11 @@ function bootstrap(opts) { next(); }); + app.use(stats({ + enabled: opts.useProfiler, + statsClient: global.statsClient + })); + app.use(lzmaMiddleware); // temporary measure until we upgrade to newer version expressjs so we can check err.status