Middlewarify metrics increment whether success or error

This commit is contained in:
Daniel García Aubert 2018-03-07 11:56:57 +01:00
parent a66c19c6c7
commit b786164e8a

View File

@ -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