Centralize common headers, this will help up to move biz metrics out of the process

This commit is contained in:
Daniel García Aubert 2020-06-04 17:45:15 +02:00
parent b2da00900f
commit adeffd2018
6 changed files with 162 additions and 34 deletions

View File

@ -346,11 +346,9 @@ function incrementMapViews ({ metadataBackend }) {
return next();
}
const statTag = mapConfig.obj().stat_tag;
res.locals.mapConfig = mapConfig;
if (statTag) {
res.set('Carto-Stat-Tag', `${statTag}`);
}
const statTag = mapConfig.obj().stat_tag;
metadataBackend.incMapviewCount(user, statTag, (err) => {
if (err) {

View File

@ -1,5 +1,7 @@
'use strict';
const setCommonHeaders = require('../../utils/common-headers');
module.exports = function errorMiddleware (/* options */) {
return function error (err, req, res, next) {
const { logger } = res.locals;
@ -7,21 +9,23 @@ module.exports = function errorMiddleware (/* options */) {
logger.error({ error: errors });
const errorResponseBody = {
errors: errors.map(errorMessage),
errors_with_context: errors.map(errorMessageWithContext)
};
setCommonHeaders(req, res, () => {
const errorResponseBody = {
errors: errors.map(errorMessage),
errors_with_context: errors.map(errorMessageWithContext)
};
// If a callback was requested, force status to 200
res.status(req.query.callback ? 200 : findStatusCode(errors[0]));
// If a callback was requested, force status to 200
res.status(req.query.callback ? 200 : findStatusCode(errors[0]));
if (req.query && req.query.callback) {
res.jsonp(errorResponseBody);
} else {
res.json(errorResponseBody);
}
if (req.query && req.query.callback) {
res.jsonp(errorResponseBody);
} else {
res.json(errorResponseBody);
}
return next();
return next();
});
};
};

View File

@ -5,11 +5,6 @@ module.exports = function incrementMapViewCount (metadataBackend) {
const { mapConfig, user, logger } = res.locals;
const statTag = mapConfig.obj().stat_tag;
if (statTag) {
res.set('Carto-Stat-Tag', `${statTag}`);
}
// Error won't blow up, just be logged.
metadataBackend.incMapviewCount(user, statTag, (err) => {
if (err) {
err.message = `Failed to increment mapview count for user '${user}'. ${err.message}`;

View File

@ -1,20 +1,24 @@
'use strict';
const setCommonHeaders = require('../../utils/common-headers');
module.exports = function sendResponse () {
return function sendResponseMiddleware (req, res, next) {
res.status(res.statusCode);
setCommonHeaders(req, res, () => {
res.status(res.statusCode);
if (Buffer.isBuffer(res.body)) {
res.send(res.body);
if (Buffer.isBuffer(res.body)) {
res.send(res.body);
return next();
}
if (req.query.callback) {
res.jsonp(res.body);
return next();
}
res.json(res.body);
return next();
}
if (req.query.callback) {
res.jsonp(res.body);
return next();
}
res.json(res.body);
return next();
});
};
};

View File

@ -18,7 +18,7 @@ module.exports = function user (metadataBackend) {
}
res.locals.userId = userId;
res.set('Carto-User-Id', `${userId}`);
return next();
});
};

127
lib/utils/common-headers.js Normal file
View File

@ -0,0 +1,127 @@
'use strict';
module.exports = function setCommonHeaders (req, res, callback) {
const { logger } = res.locals;
res.set('X-Request-Id', logger.bindings.id);
// TODO: x-layergroupid header??
const user = getUser({ res });
if (user) {
res.set('Carto-User', user);
}
const userId = getUserId({ res });
if (userId) {
res.set('Carto-User-Id', `${userId}`);
}
const mapId = getMapId({ res });
if (mapId) {
res.set('Carto-Map-Id', mapId);
}
const cacheBuster = getCacheBuster({ res });
if (cacheBuster) {
res.set('Carto-Cache-Buster', cacheBuster);
}
const templateHash = getTemplateHash({ res });
if (templateHash) {
res.set('Carto-Template-Hash', templateHash);
}
getStatTag({ res }, (err, statTag) => {
if (err) {
err.message = `Error generating Stat Tag header: ${err.message}`;
logger.warn({ error: err });
}
if (statTag) {
res.set('Carto-Stat-Tag', statTag);
}
callback();
});
};
function getUser ({ res }) {
if (res.locals.user) {
return res.locals.user;
}
}
function getUserId ({ res }) {
if (res.locals.userId) {
return res.locals.userId;
}
}
function getMapId ({ res }) {
if (res.locals.token) {
return res.locals.token;
}
if (res.locals.mapConfig) {
return res.locals.mapConfig.id();
}
if (res.locals.mapConfigProvider && res.locals.mapConfigProvider.mapConfig) {
return res.locals.mapConfigProvider.mapConfig.id();
}
}
function getCacheBuster ({ res }) {
if (res.locals.cache_buster !== undefined) {
return `${res.locals.cache_buster}`;
}
if (res.locals.mapConfigProvider) {
return `${res.locals.mapConfigProvider.getCacheBuster()}`;
}
}
function getTemplateHash ({ res }) {
const { logger } = res.locals;
if (res.locals.templateHash) {
return res.locals.templateHash;
}
if (res.locals.mapConfigProvider && typeof res.locals.mapConfigProvider.getTemplateHash === 'function') {
let templateHash;
try {
templateHash = res.locals.mapConfigProvider.getTemplateHash().substring(0, 8);
} catch (err) {
err.message = `Error generating Stat Tag header: ${err.message}`;
logger.warn({ error: err });
}
return templateHash;
}
}
function getStatTag ({ res }, callback) {
if (res.locals.mapConfig) {
return callback(null, res.locals.mapConfig.obj().stat_tag);
}
if (!res.locals.mapConfigProvider) {
return callback();
}
res.locals.mapConfigProvider.getMapConfig((err, mapConfig) => {
if (err) {
return callback(err);
}
return callback(null, mapConfig.obj().stat_tag);
});
}