2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-04 00:24:39 +08:00
|
|
|
const Profiler = require('../../stats/profiler-proxy');
|
2019-10-01 20:31:17 +08:00
|
|
|
|
|
|
|
module.exports = function profiler ({ statsClient }) {
|
|
|
|
return function profilerMiddleware (req, res, next) {
|
|
|
|
req.profiler = new Profiler({
|
|
|
|
profile: global.settings.useProfiler,
|
|
|
|
statsd_client: statsClient
|
|
|
|
});
|
|
|
|
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-02-19 22:13:36 +08:00
|
|
|
module.exports.initializeProfilerMiddleware = function initializeProfiler (label) {
|
|
|
|
return function initializeProfilerMiddleware (req, res, next) {
|
|
|
|
if (req.profiler) {
|
|
|
|
req.profiler.start(`sqlapi.${label}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.finishProfilerMiddleware = function finishProfiler () {
|
|
|
|
return function finishProfilerMiddleware (req, res, next) {
|
|
|
|
if (req.profiler) {
|
|
|
|
req.profiler.end();
|
|
|
|
req.profiler.sendStats();
|
|
|
|
|
|
|
|
res.header('X-SQLAPI-Profiler', req.profiler.toJSONString());
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
};
|