Be able to tag requests with labels as easier way to provide business metrics

This commit is contained in:
Daniel García Aubert 2020-06-08 16:29:22 +02:00
parent 07b9decb03
commit b115bca07e
13 changed files with 39 additions and 0 deletions

View File

@ -1,6 +1,7 @@
'use strict'; 'use strict';
const PSQL = require('cartodb-psql'); const PSQL = require('cartodb-psql');
const tag = require('../middlewares/tag');
const cleanUpQueryParams = require('../middlewares/clean-up-query-params'); const cleanUpQueryParams = require('../middlewares/clean-up-query-params');
const credentials = require('../middlewares/credentials'); const credentials = require('../middlewares/credentials');
const authorize = require('../middlewares/authorize'); const authorize = require('../middlewares/authorize');
@ -23,6 +24,7 @@ module.exports = class AnalysesController {
middlewares () { middlewares () {
return [ return [
tag({ tags: ['analysis', 'catalog'] }),
credentials(), credentials(),
authorize(this.authBackend), authorize(this.authBackend),
dbConnSetup(this.pgConnection), dbConnSetup(this.pgConnection),

View File

@ -1,5 +1,6 @@
'use strict'; 'use strict';
const tag = require('../middlewares/tag');
const layergroupToken = require('../middlewares/layergroup-token'); const layergroupToken = require('../middlewares/layergroup-token');
const cleanUpQueryParams = require('../middlewares/clean-up-query-params'); const cleanUpQueryParams = require('../middlewares/clean-up-query-params');
const credentials = require('../middlewares/credentials'); const credentials = require('../middlewares/credentials');
@ -23,6 +24,7 @@ module.exports = class AnalysisLayergroupController {
middlewares () { middlewares () {
return [ return [
tag({ tags: ['analysis', 'node'] }),
layergroupToken(), layergroupToken(),
credentials(), credentials(),
authorize(this.authBackend), authorize(this.authBackend),

View File

@ -3,6 +3,7 @@
const windshaft = require('windshaft'); const windshaft = require('windshaft');
const MapConfig = windshaft.model.MapConfig; const MapConfig = windshaft.model.MapConfig;
const Datasource = windshaft.model.Datasource; const Datasource = windshaft.model.Datasource;
const tag = require('../middlewares/tag');
const cleanUpQueryParams = require('../middlewares/clean-up-query-params'); const cleanUpQueryParams = require('../middlewares/clean-up-query-params');
const credentials = require('../middlewares/credentials'); const credentials = require('../middlewares/credentials');
const dbConnSetup = require('../middlewares/db-conn-setup'); const dbConnSetup = require('../middlewares/db-conn-setup');
@ -90,6 +91,7 @@ module.exports = class AnonymousMapController {
}; };
return [ return [
tag({ tags: ['map', 'anonymous'] }),
metrics({ metrics({
enabled: this.config.pubSubMetrics.enabled, enabled: this.config.pubSubMetrics.enabled,
metricsBackend: this.metricsBackend, metricsBackend: this.metricsBackend,

View File

@ -1,5 +1,6 @@
'use strict'; 'use strict';
const tag = require('../middlewares/tag');
const layergroupToken = require('../middlewares/layergroup-token'); const layergroupToken = require('../middlewares/layergroup-token');
const cleanUpQueryParams = require('../middlewares/clean-up-query-params'); const cleanUpQueryParams = require('../middlewares/clean-up-query-params');
const credentials = require('../middlewares/credentials'); const credentials = require('../middlewares/credentials');
@ -38,6 +39,7 @@ module.exports = class AttributesLayergroupController {
middlewares () { middlewares () {
return [ return [
tag({ tags: ['attributes'] }),
layergroupToken(), layergroupToken(),
credentials(), credentials(),
authorize(this.authBackend), authorize(this.authBackend),

View File

@ -1,5 +1,6 @@
'use strict'; 'use strict';
const tag = require('../middlewares/tag');
const layergroupToken = require('../middlewares/layergroup-token'); const layergroupToken = require('../middlewares/layergroup-token');
const cleanUpQueryParams = require('../middlewares/clean-up-query-params'); const cleanUpQueryParams = require('../middlewares/clean-up-query-params');
const credentials = require('../middlewares/credentials'); const credentials = require('../middlewares/credentials');
@ -38,6 +39,7 @@ module.exports = class AggregatedFeaturesLayergroupController {
middlewares () { middlewares () {
return [ return [
tag({ tags: ['cluster'] }),
layergroupToken(), layergroupToken(),
credentials(), credentials(),
authorize(this.authBackend), authorize(this.authBackend),

View File

@ -1,5 +1,6 @@
'use strict'; 'use strict';
const tag = require('../middlewares/tag');
const layergroupToken = require('../middlewares/layergroup-token'); const layergroupToken = require('../middlewares/layergroup-token');
const cleanUpQueryParams = require('../middlewares/clean-up-query-params'); const cleanUpQueryParams = require('../middlewares/clean-up-query-params');
const credentials = require('../middlewares/credentials'); const credentials = require('../middlewares/credentials');
@ -76,6 +77,7 @@ module.exports = class DataviewLayergroupController {
middlewares ({ action, rateLimitGroup }) { middlewares ({ action, rateLimitGroup }) {
return [ return [
tag({ tags: ['dataview', action] }),
layergroupToken(), layergroupToken(),
credentials(), credentials(),
authorize(this.authBackend), authorize(this.authBackend),

View File

@ -1,5 +1,6 @@
'use strict'; 'use strict';
const tag = require('../middlewares/tag');
const layergroupToken = require('../middlewares/layergroup-token'); const layergroupToken = require('../middlewares/layergroup-token');
const coordinates = require('../middlewares/coordinates'); const coordinates = require('../middlewares/coordinates');
const cleanUpQueryParams = require('../middlewares/clean-up-query-params'); const cleanUpQueryParams = require('../middlewares/clean-up-query-params');
@ -61,6 +62,7 @@ module.exports = class PreviewLayergroupController {
} }
return [ return [
tag({ tags: ['static', 'tile'] }),
layergroupToken(), layergroupToken(),
validateZoom ? coordinates({ z: true, x: false, y: false }) : noop(), validateZoom ? coordinates({ z: true, x: false, y: false }) : noop(),
credentials(), credentials(),

View File

@ -1,5 +1,6 @@
'use strict'; 'use strict';
const tag = require('../middlewares/tag');
const cleanUpQueryParams = require('../middlewares/clean-up-query-params'); const cleanUpQueryParams = require('../middlewares/clean-up-query-params');
const credentials = require('../middlewares/credentials'); const credentials = require('../middlewares/credentials');
const dbConnSetup = require('../middlewares/db-conn-setup'); const dbConnSetup = require('../middlewares/db-conn-setup');
@ -67,6 +68,7 @@ module.exports = class PreviewTemplateController {
}; };
return [ return [
tag({ tags: ['named', 'static', 'tile'] }),
metrics({ metrics({
enabled: this.config.pubSubMetrics.enabled, enabled: this.config.pubSubMetrics.enabled,
metricsBackend: this.metricsBackend, metricsBackend: this.metricsBackend,

View File

@ -1,5 +1,6 @@
'use strict'; 'use strict';
const tag = require('../middlewares/tag');
const layergroupToken = require('../middlewares/layergroup-token'); const layergroupToken = require('../middlewares/layergroup-token');
const coordinates = require('../middlewares/coordinates'); const coordinates = require('../middlewares/coordinates');
const cleanUpQueryParams = require('../middlewares/clean-up-query-params'); const cleanUpQueryParams = require('../middlewares/clean-up-query-params');
@ -59,6 +60,7 @@ module.exports = class TileLayergroupController {
middlewares () { middlewares () {
return [ return [
tag({ tags: ['tile'] }),
layergroupToken(), layergroupToken(),
coordinates(), coordinates(),
credentials(), credentials(),

View File

@ -0,0 +1,15 @@
'use strict';
module.exports = function tag ({ tags }) {
if (!Array.isArray(tags) || !tags.every((tag) => typeof tag === 'string')) {
throw new Error('Required "tags" option must be a valid Array: [string, string, ...]');
}
return function tagMiddleware (req, res, next) {
const { logger } = res.locals;
res.locals.tags = tags;
res.on('finish', () => logger.info({ tags: res.locals.tags }));
next();
};
};

View File

@ -1,6 +1,7 @@
'use strict'; 'use strict';
const { templateName } = require('../../backends/template-maps'); const { templateName } = require('../../backends/template-maps');
const tag = require('../middlewares/tag');
const credentials = require('../middlewares/credentials'); const credentials = require('../middlewares/credentials');
const rateLimit = require('../middlewares/rate-limit'); const rateLimit = require('../middlewares/rate-limit');
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimit; const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimit;
@ -76,6 +77,7 @@ module.exports = class AdminTemplateController {
} }
return [ return [
tag({ tags: ['named', 'admin', action] }),
credentials(), credentials(),
authorizedByAPIKey({ authBackend: this.authBackend, action, label }), authorizedByAPIKey({ authBackend: this.authBackend, action, label }),
rateLimit(this.userLimitsBackend, rateLimitGroup), rateLimit(this.userLimitsBackend, rateLimitGroup),

View File

@ -1,5 +1,6 @@
'use strict'; 'use strict';
const tag = require('../middlewares/tag');
const cleanUpQueryParams = require('../middlewares/clean-up-query-params'); const cleanUpQueryParams = require('../middlewares/clean-up-query-params');
const credentials = require('../middlewares/credentials'); const credentials = require('../middlewares/credentials');
const dbConnSetup = require('../middlewares/db-conn-setup'); const dbConnSetup = require('../middlewares/db-conn-setup');
@ -88,6 +89,7 @@ module.exports = class NamedMapController {
}; };
return [ return [
tag({ tags: ['map', 'named'] }),
metrics({ metrics({
enabled: this.config.pubSubMetrics.enabled, enabled: this.config.pubSubMetrics.enabled,
metricsBackend: this.metricsBackend, metricsBackend: this.metricsBackend,

View File

@ -1,5 +1,6 @@
'use strict'; 'use strict';
const tag = require('../middlewares/tag');
const coordinates = require('../middlewares/coordinates'); const coordinates = require('../middlewares/coordinates');
const cleanUpQueryParams = require('../middlewares/clean-up-query-params'); const cleanUpQueryParams = require('../middlewares/clean-up-query-params');
const credentials = require('../middlewares/credentials'); const credentials = require('../middlewares/credentials');
@ -37,6 +38,7 @@ module.exports = class TileTemplateController {
middlewares () { middlewares () {
return [ return [
tag({ tags: ['tile', 'named'] }),
coordinates(), coordinates(),
credentials(), credentials(),
authorize(this.authBackend), authorize(this.authBackend),