Create middlewares to initialize and finish profiling

This commit is contained in:
Daniel García Aubert 2018-02-19 14:42:52 +01:00
parent 633d06bf2d
commit 85fbd7c6b2

View File

@ -19,13 +19,14 @@ JobController.prototype.route = function (app) {
app.post( app.post(
`${base_url}/sql/job`, `${base_url}/sql/job`,
initializeProfiler('job'),
checkBodyPayloadSize(), checkBodyPayloadSize(),
userMiddleware(), userMiddleware(),
credentialsMiddleware(), credentialsMiddleware(),
authenticatedMiddleware(this.userDatabaseService, forceToBeAuthenticated), authenticatedMiddleware(this.userDatabaseService, forceToBeAuthenticated),
createJob(this.jobService), createJob(this.jobService),
setServedByDBHostHeader(), setServedByDBHostHeader(),
profile(), finishProfiler(),
log('create'), log('create'),
incrementSuccessMetrics(this.statsdClient), incrementSuccessMetrics(this.statsdClient),
incrementErrorMetrics(this.statsdClient), incrementErrorMetrics(this.statsdClient),
@ -35,22 +36,18 @@ JobController.prototype.route = function (app) {
app.get( app.get(
`${base_url}/jobs-wip`, `${base_url}/jobs-wip`,
listWorkInProgressJobs(this.jobService), listWorkInProgressJobs(this.jobService),
setServedByDBHostHeader(),
profile(),
log('list'),
incrementSuccessMetrics(this.statsdClient),
incrementErrorMetrics(this.statsdClient),
errorMiddleware() errorMiddleware()
); );
app.get( app.get(
`${base_url}/sql/job/:job_id`, `${base_url}/sql/job/:job_id`,
initializeProfiler('job'),
userMiddleware(), userMiddleware(),
credentialsMiddleware(), credentialsMiddleware(),
authenticatedMiddleware(this.userDatabaseService, forceToBeAuthenticated), authenticatedMiddleware(this.userDatabaseService, forceToBeAuthenticated),
getJob(this.jobService), getJob(this.jobService),
setServedByDBHostHeader(), setServedByDBHostHeader(),
profile(), finishProfiler(),
log('retrieve'), log('retrieve'),
incrementSuccessMetrics(this.statsdClient), incrementSuccessMetrics(this.statsdClient),
incrementErrorMetrics(this.statsdClient), incrementErrorMetrics(this.statsdClient),
@ -59,12 +56,13 @@ JobController.prototype.route = function (app) {
app.delete( app.delete(
`${base_url}/sql/job/:job_id`, `${base_url}/sql/job/:job_id`,
initializeProfiler('job'),
userMiddleware(), userMiddleware(),
credentialsMiddleware(), credentialsMiddleware(),
authenticatedMiddleware(this.userDatabaseService, forceToBeAuthenticated), authenticatedMiddleware(this.userDatabaseService, forceToBeAuthenticated),
cancelJob(this.jobService), cancelJob(this.jobService),
setServedByDBHostHeader(), setServedByDBHostHeader(),
profile(), finishProfiler(),
log('cancel'), log('cancel'),
incrementSuccessMetrics(this.statsdClient), incrementSuccessMetrics(this.statsdClient),
incrementErrorMetrics(this.statsdClient), incrementErrorMetrics(this.statsdClient),
@ -77,6 +75,10 @@ function cancelJob (jobService) {
const { job_id } = req.params; const { job_id } = req.params;
jobService.cancel(job_id, (err, job) => { jobService.cancel(job_id, (err, job) => {
if (req.profiler) {
req.profiler.done('cancelJob');
}
if (err) { if (err) {
return next(err); return next(err);
} }
@ -93,6 +95,10 @@ function getJob (jobService) {
const { job_id } = req.params; const { job_id } = req.params;
jobService.get(job_id, (err, job) => { jobService.get(job_id, (err, job) => {
if (req.profiler) {
req.profiler.done('getJob');
}
if (err) { if (err) {
return next(err); return next(err);
} }
@ -119,6 +125,10 @@ function createJob (jobService) {
}; };
jobService.create(data, (err, job) => { jobService.create(data, (err, job) => {
if (req.profiler) {
req.profiler.done('createJob');
}
if (err) { if (err) {
return next(err); return next(err);
} }
@ -146,6 +156,16 @@ function listWorkInProgressJobs (jobService) {
}; };
} }
function initializeProfiler (label) {
return function initializeProfilerMiddleware (req, res, next) {
if (req.profiler) {
req.profiler.start(`sqlapi.${label}`);
}
next();
};
}
function checkBodyPayloadSize () { function checkBodyPayloadSize () {
return function checkBodyPayloadSizeMiddleware(req, res, next) { return function checkBodyPayloadSizeMiddleware(req, res, next) {
const payload = JSON.stringify(req.body); const payload = JSON.stringify(req.body);
@ -189,8 +209,8 @@ function setServedByDBHostHeader () {
}; };
} }
function profile () { function finishProfiler () {
return function profileMiddleware (req, res, next) { return function finishProfilerMiddleware (req, res, next) {
if (req.profiler) { if (req.profiler) {
req.profiler.done(); req.profiler.done();
req.profiler.end(); req.profiler.end();