2014-05-07 09:58:52 +00:00
|
|
|
os = require("os")
|
|
|
|
|
|
|
|
module.exports.monitor = (logger) ->
|
|
|
|
return (req, res, next) ->
|
|
|
|
Metrics = require("./metrics")
|
|
|
|
startTime = new Date()
|
|
|
|
end = res.end
|
|
|
|
res.end = () ->
|
|
|
|
end.apply(this, arguments)
|
|
|
|
responseTime = new Date() - startTime
|
2014-05-12 14:28:09 +00:00
|
|
|
if req.route?.path?
|
|
|
|
routePath = req.route.path.toString().replace(/\//g, '_').replace(/\:/g, '').slice(1)
|
|
|
|
key = "http-requests.#{routePath}.#{req.method}.#{res.statusCode}"
|
2014-05-07 09:58:52 +00:00
|
|
|
|
2014-05-12 14:28:09 +00:00
|
|
|
Metrics.timing(key, responseTime)
|
|
|
|
logger.log
|
|
|
|
req:
|
|
|
|
url: req.originalUrl || req.url
|
|
|
|
method: req.method
|
|
|
|
referrer: req.headers['referer'] || req.headers['referrer']
|
|
|
|
"remote-addr": req.ip || req.socket?.socket?.remoteAddress || req.socket?.remoteAddress
|
|
|
|
"user-agent": req.headers["user-agent"]
|
|
|
|
"content-length": req.headers["content-length"]
|
|
|
|
res:
|
|
|
|
"content-length": res._headers?["content-length"]
|
|
|
|
statusCode: res.statusCode
|
|
|
|
"response-time": responseTime
|
|
|
|
"http request"
|
|
|
|
|
2014-05-07 09:58:52 +00:00
|
|
|
next()
|
|
|
|
|