Add in http monitoring

This commit is contained in:
James Allen 2014-05-07 10:58:52 +01:00
parent 4da7fa43fa
commit f4895fb04f
2 changed files with 32 additions and 1 deletions

View file

@ -0,0 +1,29 @@
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
routePath = req.route.path.toString().replace(/\//g, '_').replace(/\:/g, '').slice(1)
key = "#{routePath}.#{req.method}.#{res.statusCode}"
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"
next()

View file

@ -2,8 +2,9 @@ StatsD = require('lynx')
statsd = new StatsD('localhost', 8125, {on_error:->})
name = "unknown"
hostname = require('os').hostname()
buildKey = (key)-> "#{name}.#{process.env.NODE_ENV or "development"}.#{key}"
buildKey = (key)-> "#{name}.#{hostname}.#{key}"
module.exports =
initialize: (_name) ->
@ -31,4 +32,5 @@ module.exports =
statsd.gauge key, value, sampleRate
mongodb: require "./mongodb"
http: require "./http"