reduce memory capture in http logger

only capture the properties of 'req' that we need, to avoid leaking
the whole req object for responses that never call res.end()
This commit is contained in:
Brian Gough 2015-05-05 10:50:59 +01:00
parent 1816676e5c
commit 1e0a991fcd

View file

@ -4,23 +4,35 @@ module.exports.monitor = (logger) ->
return (req, res, next) -> return (req, res, next) ->
Metrics = require("./metrics") Metrics = require("./metrics")
startTime = new Date() startTime = new Date()
# only capture the properties of 'req' that we need, to avoid
# leaking the whole req object for responses that never call
# res.end()
url = req.originalUrl || req.url
method = req.method
referrer = req.headers['referer'] || req.headers['referrer']
remoteAddr = req.ip || req.socket?.socket?.remoteAddress || req.socket?.remoteAddress
userAgent = req.headers["user-agent"]
contentLength = req.headers["content-length"]
path = req.route?.path
end = res.end end = res.end
res.end = () -> res.end = () ->
end.apply(this, arguments) end.apply(this, arguments)
responseTime = new Date() - startTime responseTime = new Date() - startTime
if req.route?.path? if path?
routePath = req.route.path.toString().replace(/\//g, '_').replace(/\:/g, '').slice(1) routePath = path.toString().replace(/\//g, '_').replace(/\:/g, '').slice(1)
key = "http-requests.#{routePath}.#{req.method}.#{res.statusCode}" key = "http-requests.#{routePath}.#{method}.#{res.statusCode}"
Metrics.timing(key, responseTime) Metrics.timing(key, responseTime)
logger.log logger.log
req: req:
url: req.originalUrl || req.url url: url
method: req.method method: method
referrer: req.headers['referer'] || req.headers['referrer'] referrer: referrer
"remote-addr": req.ip || req.socket?.socket?.remoteAddress || req.socket?.remoteAddress "remote-addr": remoteAddr
"user-agent": req.headers["user-agent"] "user-agent": userAgent
"content-length": req.headers["content-length"] "content-length": contentLength
res: res:
"content-length": res._headers?["content-length"] "content-length": res._headers?["content-length"]
statusCode: res.statusCode statusCode: res.statusCode