Extract profiler setup to its middleware

This commit is contained in:
Daniel García Aubert 2019-10-01 14:31:17 +02:00
parent 1a9081135d
commit 9c1a1611d5
3 changed files with 17 additions and 7 deletions

View File

@ -20,9 +20,12 @@ const JobController = require('./job_controller');
const cors = require('../middlewares/cors');
const servedByHostHeader = require('../middlewares/served-by-host-header');
const logger = require('../middlewares/logger');
const profiler = require('../middlewares/profiler');
module.exports = class ApiRouter {
constructor ({ redisPool, metadataBackend, statsClient, dataIngestionLogger }) {
this.statsClient = statsClient;
const userLimitsServiceOptions = {
limits: {
rateLimitsEnabled: global.settings.ratelimits.rateLimitsEnabled
@ -66,6 +69,7 @@ module.exports = class ApiRouter {
const apiRouter = router({ mergeParams: true });
apiRouter.use(logger());
apiRouter.use(profiler({ statsClient: this.statsClient }));
apiRouter.use(cors());
apiRouter.use(servedByHostHeader());

View File

@ -1,5 +1,18 @@
'use strict';
const Profiler = require('../stats/profiler-proxy');
module.exports = function profiler ({ statsClient }) {
return function profilerMiddleware (req, res, next) {
req.profiler = new Profiler({
profile: global.settings.useProfiler,
statsd_client: statsClient
});
next();
};
};
module.exports.initializeProfilerMiddleware = function initializeProfiler (label) {
return function initializeProfilerMiddleware (req, res, next) {
if (req.profiler) {

View File

@ -17,7 +17,6 @@
//
var express = require('express');
var Profiler = require('./stats/profiler-proxy');
var fs = require('fs');
var mkdirp = require('mkdirp');
@ -75,15 +74,9 @@ function App(statsClient) {
mkdirp.sync(global.settings.tmpDir);
}
// Use step-profiler
app.use(function bootstrap$prepareRequestResponse(req, res, next) {
res.locals = res.locals || {};
var profile = global.settings.useProfiler;
req.profiler = new Profiler({
profile: profile,
statsd_client: statsClient
});
next();
});