Do not use global logger in middlewares, use the one initialized in res.locals instead

This commit is contained in:
Daniel García Aubert 2020-06-02 09:00:45 +02:00
parent e644201756
commit 62d66f2dbc
11 changed files with 20 additions and 26 deletions

View File

@ -86,8 +86,7 @@ module.exports = class ApiRouter {
const { rendererCache, tileBackend, attributesBackend, previewBackend, mapBackend, mapStore } = windshaftFactory({ const { rendererCache, tileBackend, attributesBackend, previewBackend, mapBackend, mapStore } = windshaftFactory({
rendererOptions: serverOptions, rendererOptions: serverOptions,
redisPool, redisPool,
onTileErrorStrategy: getOnTileErrorStrategy({ enabled: environmentOptions.enabledFeatures.onTileErrorStrategy }), onTileErrorStrategy: getOnTileErrorStrategy({ enabled: environmentOptions.enabledFeatures.onTileErrorStrategy })
logger: global.logger
}); });
const rendererStatsReporter = new RendererStatsReporter(rendererCache, serverOptions.renderCache.statsInterval); const rendererStatsReporter = new RendererStatsReporter(rendererCache, serverOptions.renderCache.statsInterval);

View File

@ -95,7 +95,6 @@ module.exports = class AnonymousMapController {
metrics({ metrics({
enabled: this.config.pubSubMetrics.enabled, enabled: this.config.pubSubMetrics.enabled,
metricsBackend: this.metricsBackend, metricsBackend: this.metricsBackend,
logger: global.logger,
tags: metricsTags tags: metricsTags
}), }),
credentials(), credentials(),

View File

@ -70,7 +70,6 @@ module.exports = class PreviewTemplateController {
metrics({ metrics({
enabled: this.config.pubSubMetrics.enabled, enabled: this.config.pubSubMetrics.enabled,
metricsBackend: this.metricsBackend, metricsBackend: this.metricsBackend,
logger: global.logger,
tags: metricsTags tags: metricsTags
}), }),
credentials(), credentials(),
@ -337,17 +336,13 @@ function setContentTypeHeader () {
}; };
} }
function incrementMapViewsError (ctx) {
return `ERROR: failed to increment mapview count for user '${ctx.user}': ${ctx.err}`;
}
function incrementMapViews ({ metadataBackend }) { function incrementMapViews ({ metadataBackend }) {
return function incrementMapViewsMiddleware (req, res, next) { return function incrementMapViewsMiddleware (req, res, next) {
const { user, mapConfigProvider } = res.locals; const { user, mapConfigProvider, logger } = res.locals;
mapConfigProvider.getMapConfig((err, mapConfig) => { mapConfigProvider.getMapConfig((err, mapConfig) => {
if (err) { if (err) {
global.logger.info(incrementMapViewsError({ user, err })); logger.warn(err, `ERROR: failed to increment mapview count for user '${user}'`);
return next(); return next();
} }
@ -359,7 +354,7 @@ function incrementMapViews ({ metadataBackend }) {
metadataBackend.incMapviewCount(user, statTag, (err) => { metadataBackend.incMapviewCount(user, statTag, (err) => {
if (err) { if (err) {
global.logger.info(incrementMapViewsError({ user, err })); logger.warn(err, `ERROR: failed to increment mapview count for user '${user}'`);
} }
next(); next();

View File

@ -6,11 +6,11 @@ module.exports = function setCacheChannelHeader () {
return next(); return next();
} }
const { mapConfigProvider } = res.locals; const { mapConfigProvider, logger } = res.locals;
mapConfigProvider.getAffectedTables((err, affectedTables) => { mapConfigProvider.getAffectedTables((err, affectedTables) => {
if (err) { if (err) {
global.logger.warn(err, 'ERROR generating Cache Channel Header'); logger.warn(err, 'ERROR generating Cache Channel Header');
return next(); return next();
} }

View File

@ -40,11 +40,11 @@ module.exports = function setCacheControlHeader ({
return next(); return next();
} }
const { mapConfigProvider = { getAffectedTables: callback => callback() } } = res.locals; const { mapConfigProvider = { getAffectedTables: callback => callback() }, logger } = res.locals;
mapConfigProvider.getAffectedTables((err, affectedTables) => { mapConfigProvider.getAffectedTables((err, affectedTables) => {
if (err) { if (err) {
global.logger.warn(err, 'ERROR generating Cache Control Header'); logger.warn(err, 'ERROR generating Cache Control Header');
return next(); return next();
} }

View File

@ -2,7 +2,7 @@
module.exports = function incrementMapViewCount (metadataBackend) { module.exports = function incrementMapViewCount (metadataBackend) {
return function incrementMapViewCountMiddleware (req, res, next) { return function incrementMapViewCountMiddleware (req, res, next) {
const { mapConfig, user } = res.locals; const { mapConfig, user, logger } = res.locals;
const statTag = mapConfig.obj().stat_tag; const statTag = mapConfig.obj().stat_tag;
if (statTag) { if (statTag) {
@ -14,7 +14,7 @@ module.exports = function incrementMapViewCount (metadataBackend) {
req.profiler.done('incMapviewCount'); req.profiler.done('incMapviewCount');
if (err) { if (err) {
global.logger.warn(err, `ERROR: failed to increment mapview count for user '${user}'`); logger.warn(err, `ERROR: failed to increment mapview count for user '${user}'`);
} }
next(); next();

View File

@ -6,7 +6,7 @@ module.exports = function setLastModifiedHeader () {
return next(); return next();
} }
const { mapConfigProvider, cache_buster: cacheBuster } = res.locals; const { mapConfigProvider, cache_buster: cacheBuster, logger } = res.locals;
if (cacheBuster) { if (cacheBuster) {
const cacheBusterTimestamp = parseInt(cacheBuster, 10); const cacheBusterTimestamp = parseInt(cacheBuster, 10);
@ -21,7 +21,7 @@ module.exports = function setLastModifiedHeader () {
mapConfigProvider.getAffectedTables((err, affectedTables) => { mapConfigProvider.getAffectedTables((err, affectedTables) => {
if (err) { if (err) {
global.logger.warn(err, 'ERROR generating Last Modified Header'); logger.warn(err, 'ERROR generating Last Modified Header');
return next(); return next();
} }

View File

@ -5,10 +5,10 @@ const uuid = require('uuid');
module.exports = function logger () { module.exports = function logger () {
return function loggerMiddleware (req, res, next) { return function loggerMiddleware (req, res, next) {
const id = req.get('X-Request-Id') || uuid.v4(); const id = req.get('X-Request-Id') || uuid.v4();
res.locals.logger = global.logger.child({ id }); const logger = res.locals.logger = global.logger.child({ id });
res.locals.logger.info(req); logger.info(req);
res.on('finish', () => res.locals.logger.info(res)); res.on('finish', () => logger.info(res));
next(); next();
}; };

View File

@ -3,7 +3,7 @@
const EVENT_VERSION = '1'; const EVENT_VERSION = '1';
const MAX_LENGTH = 100; const MAX_LENGTH = 100;
module.exports = function metrics ({ enabled, tags, metricsBackend, logger }) { module.exports = function metrics ({ enabled, tags, metricsBackend }) {
if (!enabled) { if (!enabled) {
return function metricsDisabledMiddleware (req, res, next) { return function metricsDisabledMiddleware (req, res, next) {
next(); next();
@ -15,6 +15,8 @@ module.exports = function metrics ({ enabled, tags, metricsBackend, logger }) {
} }
return function metricsMiddleware (req, res, next) { return function metricsMiddleware (req, res, next) {
const { logger } = res.locals;
res.on('finish', () => { res.on('finish', () => {
const { event, attributes } = getEventData(req, res, tags); const { event, attributes } = getEventData(req, res, tags);

View File

@ -5,7 +5,7 @@ const NamedMapMapConfigProvider = require('../../models/mapconfig/provider/named
module.exports = function setSurrogateKeyHeader ({ surrogateKeysCache }) { module.exports = function setSurrogateKeyHeader ({ surrogateKeysCache }) {
return function setSurrogateKeyHeaderMiddleware (req, res, next) { return function setSurrogateKeyHeaderMiddleware (req, res, next) {
const { user, mapConfigProvider } = res.locals; const { user, mapConfigProvider, logger } = res.locals;
if (mapConfigProvider instanceof NamedMapMapConfigProvider) { if (mapConfigProvider instanceof NamedMapMapConfigProvider) {
surrogateKeysCache.tag(res, new NamedMapsCacheEntry(user, mapConfigProvider.getTemplateName())); surrogateKeysCache.tag(res, new NamedMapsCacheEntry(user, mapConfigProvider.getTemplateName()));
@ -17,7 +17,7 @@ module.exports = function setSurrogateKeyHeader ({ surrogateKeysCache }) {
mapConfigProvider.getAffectedTables((err, affectedTables) => { mapConfigProvider.getAffectedTables((err, affectedTables) => {
if (err) { if (err) {
global.logger.warn(err, 'ERROR generating Surrogate Key Header'); logger.warn(err, 'ERROR generating Surrogate Key Header');
return next(); return next();
} }

View File

@ -93,7 +93,6 @@ module.exports = class NamedMapController {
metrics({ metrics({
enabled: this.config.pubSubMetrics.enabled, enabled: this.config.pubSubMetrics.enabled,
metricsBackend: this.metricsBackend, metricsBackend: this.metricsBackend,
logger: global.logger,
tags: metricsTags tags: metricsTags
}), }),
credentials(), credentials(),