mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-29 16:11:36 +00:00
4b308553be
[misc] move the linting setup to the root of the monorepo GitOrigin-RevId: 1633e2a58598add0b727738cd3bfba0ab7bae781
45 lines
1.2 KiB
JavaScript
45 lines
1.2 KiB
JavaScript
const Metrics = require('./index')
|
|
|
|
module.exports.monitor = logger =>
|
|
function (req, res, next) {
|
|
const startTime = Date.now()
|
|
const { end } = res
|
|
res.end = function (...args) {
|
|
end.apply(this, args)
|
|
const responseTimeMs = Date.now() - startTime
|
|
const requestSize = parseInt(req.headers['content-length'], 10)
|
|
const routePath = getRoutePath(req)
|
|
const reqUrl = req.originalUrl || req.url
|
|
|
|
if (routePath != null) {
|
|
Metrics.timing('http_request', responseTimeMs, null, {
|
|
method: req.method,
|
|
status_code: res.statusCode,
|
|
path: routePath,
|
|
})
|
|
if (requestSize) {
|
|
Metrics.summary('http_request_size_bytes', requestSize, {
|
|
method: req.method,
|
|
status_code: res.statusCode,
|
|
path: routePath,
|
|
})
|
|
}
|
|
}
|
|
logger.info({ req, res, responseTimeMs }, '%s %s', req.method, reqUrl)
|
|
}
|
|
next()
|
|
}
|
|
|
|
function getRoutePath(req) {
|
|
if (req.route && req.route.path != null) {
|
|
return req.route.path
|
|
.toString()
|
|
.replace(/\//g, '_')
|
|
.replace(/:/g, '')
|
|
.slice(1)
|
|
}
|
|
if (req.swagger && req.swagger.apiPath != null) {
|
|
return req.swagger.apiPath
|
|
}
|
|
return null
|
|
}
|