From b786164e8aa1943b0aa449c91b6cee323c17e8ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Wed, 7 Mar 2018 11:56:57 +0100 Subject: [PATCH] Middlewarify metrics increment whether success or error --- lib/cartodb/controllers/layergroup.js | 58 ++++++++++++++++----------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/lib/cartodb/controllers/layergroup.js b/lib/cartodb/controllers/layergroup.js index 8cf3f064..08722815 100644 --- a/lib/cartodb/controllers/layergroup.js +++ b/lib/cartodb/controllers/layergroup.js @@ -50,6 +50,8 @@ LayergroupController.prototype.register = function(app) { this.prepareContext, this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), this.tile(this.tileBackend), + this.incrementSuccessMetrics(global.statsClient), + this.incrementErrorMetrics(global.statsClient), this.setCacheControlHeader(), this.setLastModifiedHeader(), this.getAffectedTables(this.layergroupAffectedTables, this.pgConnection), @@ -67,6 +69,8 @@ LayergroupController.prototype.register = function(app) { this.prepareContext, this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), this.tile(this.tileBackend), + this.incrementSuccessMetrics(global.statsClient), + this.incrementErrorMetrics(global.statsClient), this.setCacheControlHeader(), this.setLastModifiedHeader(), this.getAffectedTables(this.layergroupAffectedTables, this.pgConnection), @@ -85,6 +89,8 @@ LayergroupController.prototype.register = function(app) { this.prepareContext, this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), this.layer(this.tileBackend), + this.incrementSuccessMetrics(global.statsClient), + this.incrementErrorMetrics(global.statsClient), this.setCacheControlHeader(), this.setLastModifiedHeader(), this.getAffectedTables(this.layergroupAffectedTables, this.pgConnection), @@ -375,7 +381,7 @@ const supportedFormats = { mvt: true }; -function parseFormat (format = null) { +function parseFormat (format = '') { const prettyFormat = format.replace('.', '_'); let formatStat = 'invalid'; @@ -396,28 +402,20 @@ LayergroupController.prototype.tile = function (tileBackend) { tileBackend.getTile(mapConfigProvider, params, (err, tile, headers, stats) => { req.profiler.add(stats); - const formatStat = parseFormat(req.params.format); - if (err) { - next(err); - - global.statsClient.increment('windshaft.tiles.error'); - global.statsClient.increment('windshaft.tiles.' + formatStat + '.error'); - - return; + return next(err); } if (headers) { res.set(headers); } + const formatStat = parseFormat(req.params.format); + res.statusCode = getStatusCode(tile, formatStat); res.body = tile; next(); - - global.statsClient.increment('windshaft.tiles.success'); - global.statsClient.increment('windshaft.tiles.' + formatStat + '.success'); }); }; }; @@ -432,32 +430,46 @@ LayergroupController.prototype.layer = function (tileBackend) { tileBackend.getTile(mapConfigProvider, params, (err, tile, headers, stats) => { req.profiler.add(stats); - const formatStat = parseFormat(req.params.format); - if (err) { - next(err); - - global.statsClient.increment('windshaft.tiles.error'); - global.statsClient.increment('windshaft.tiles.' + formatStat + '.error'); - - return; + return next(err); } if (headers) { res.set(headers); } + const formatStat = parseFormat(req.params.format); + res.statusCode = getStatusCode(tile, formatStat); res.body = tile; next(); - - global.statsClient.increment('windshaft.tiles.success'); - global.statsClient.increment('windshaft.tiles.' + formatStat + '.success'); }); }; }; +LayergroupController.prototype.incrementSuccessMetrics = function (statsClient) { + return function incrementSuccessMetricsMiddleware (req, res, next) { + const formatStat = parseFormat(req.params.format); + + statsClient.increment('windshaft.tiles.success'); + statsClient.increment(`windshaft.tiles.${formatStat}.success`); + + next(); + }; +}; + +LayergroupController.prototype.incrementErrorMetrics = function (statsClient) { + return function incrementErrorMetricsMiddleware (err, req, res, next) { + const formatStat = parseFormat(req.params.format); + + statsClient.increment('windshaft.tiles.error'); + statsClient.increment(`windshaft.tiles.${formatStat}.error`); + + next(err); + }; +}; + LayergroupController.prototype.tileError = function () { return function tileErrorMiddleware (err, req, res, next) { // See https://github.com/Vizzuality/Windshaft-cartodb/issues/68