2014-05-07 09:58:52 +00:00
|
|
|
os = require("os")
|
2019-10-25 02:02:55 +00:00
|
|
|
yn = require("yn")
|
|
|
|
|
|
|
|
STACKDRIVER_LOGGING = yn(process.env['STACKDRIVER_LOGGING'])
|
2014-05-07 09:58:52 +00:00
|
|
|
|
|
|
|
module.exports.monitor = (logger) ->
|
|
|
|
return (req, res, next) ->
|
|
|
|
Metrics = require("./metrics")
|
2019-10-25 02:02:55 +00:00
|
|
|
startTime = process.hrtime()
|
2014-05-07 09:58:52 +00:00
|
|
|
end = res.end
|
|
|
|
res.end = () ->
|
|
|
|
end.apply(this, arguments)
|
2019-10-25 02:02:55 +00:00
|
|
|
responseTime = process.hrtime(startTime)
|
|
|
|
responseTimeMs = Math.round(responseTime[0] * 1000 + responseTime[1] / 1000)
|
2015-05-14 15:14:24 +00:00
|
|
|
if req.route?.path?
|
|
|
|
routePath = req.route.path.toString().replace(/\//g, '_').replace(/\:/g, '').slice(1)
|
2019-10-25 02:02:55 +00:00
|
|
|
Metrics.timing("http_request", responseTimeMs, null, {method:req.method, status_code: res.statusCode, path:routePath})
|
|
|
|
remoteIp = req.ip || req.socket?.socket?.remoteAddress || req.socket?.remoteAddress
|
|
|
|
reqUrl = req.originalUrl || req.url
|
|
|
|
referrer = req.headers['referer'] || req.headers['referrer']
|
|
|
|
if STACKDRIVER_LOGGING
|
|
|
|
info =
|
|
|
|
httpRequest:
|
|
|
|
requestMethod: req.method
|
|
|
|
requestUrl: reqUrl
|
|
|
|
requestSize: req.headers["content-length"]
|
|
|
|
status: res.statusCode
|
|
|
|
responseSize: res._headers?["content-length"]
|
|
|
|
userAgent: req.headers["user-agent"]
|
|
|
|
remoteIp: remoteIp
|
|
|
|
referer: referrer
|
|
|
|
latency:
|
|
|
|
seconds: responseTime[0]
|
|
|
|
nanos: responseTime[1]
|
|
|
|
protocol: req.protocol
|
|
|
|
else
|
|
|
|
info =
|
|
|
|
req:
|
|
|
|
url: reqUrl
|
|
|
|
method: req.method
|
|
|
|
referrer: referrer
|
|
|
|
"remote-addr": remoteIp
|
|
|
|
"user-agent": req.headers["user-agent"]
|
|
|
|
"content-length": req.headers["content-length"]
|
|
|
|
res:
|
|
|
|
"content-length": res._headers?["content-length"]
|
|
|
|
statusCode: res.statusCode
|
|
|
|
"response-time": responseTimeMs
|
|
|
|
if res.statusCode >= 500
|
|
|
|
logger.error(info, "%s %s", req.method, reqUrl)
|
|
|
|
else if res.statusCode >= 400 and res.statusCode < 500
|
|
|
|
logger.warn(info, "%s %s", req.method, reqUrl)
|
|
|
|
else
|
|
|
|
logger.info(info, "%s %s", req.method, reqUrl)
|
2014-05-07 09:58:52 +00:00
|
|
|
next()
|