mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-24 08:08:15 -05:00
Tabs -> Spaces
This commit is contained in:
parent
32861b025a
commit
7a377350e8
1 changed files with 27 additions and 27 deletions
|
@ -4,40 +4,40 @@ config = require '../../server_config'
|
||||||
StatsD = require 'node-statsd'
|
StatsD = require 'node-statsd'
|
||||||
|
|
||||||
if config.statsd
|
if config.statsd
|
||||||
realClient = new StatsD(config.statsd)
|
realClient = new StatsD(config.statsd)
|
||||||
else
|
else
|
||||||
mock = new StatsD(mock: true)
|
mock = new StatsD(mock: true)
|
||||||
|
|
||||||
exports.client = realClient or mock
|
exports.client = realClient or mock
|
||||||
exports.middleware = (req, res, next) ->
|
exports.middleware = (req, res, next) ->
|
||||||
|
|
||||||
req.statsd = exports.client
|
req.statsd = exports.client
|
||||||
if realClient
|
if realClient
|
||||||
time = process.hrtime();
|
time = process.hrtime();
|
||||||
cleanup = ->
|
cleanup = ->
|
||||||
res.removeListener 'finish', recordMetrics
|
res.removeListener 'finish', recordMetrics
|
||||||
res.removeListener 'error', cleanup
|
res.removeListener 'error', cleanup
|
||||||
res.removeListener 'close', cleanup
|
res.removeListener 'close', cleanup
|
||||||
|
|
||||||
recordMetrics = ->
|
recordMetrics = ->
|
||||||
diff = process.hrtime(time);
|
diff = process.hrtime(time);
|
||||||
ms = (diff[0] * 1000 + diff[1] / 1e6);
|
ms = (diff[0] * 1000 + diff[1] / 1e6);
|
||||||
path = req.route?.path or '/*'
|
path = req.route?.path or '/*'
|
||||||
stat = req.method + "." + path.replace /[^A-Za-z0-9]+/g, '_'
|
stat = req.method + "." + path.replace /[^A-Za-z0-9]+/g, '_'
|
||||||
realClient.timing stat, ms
|
realClient.timing stat, ms
|
||||||
|
|
||||||
|
|
||||||
res.once 'finish', recordMetrics
|
res.once 'finish', recordMetrics
|
||||||
res.once 'error', cleanup
|
res.once 'error', cleanup
|
||||||
res.once 'close', cleanup
|
res.once 'close', cleanup
|
||||||
else
|
else
|
||||||
req.statsd = mock
|
req.statsd = mock
|
||||||
|
|
||||||
next() unless not next
|
next() unless not next
|
||||||
|
|
||||||
exports.trace = (name, callback) ->
|
exports.trace = (name, callback) ->
|
||||||
return callback unless realClient
|
return callback unless realClient
|
||||||
time = process.hrtime()
|
time = process.hrtime()
|
||||||
(args...) ->
|
(args...) ->
|
||||||
realClient.timing name, ms
|
realClient.timing name, ms
|
||||||
return callback.apply(this, args)
|
return callback.apply(this, args)
|
Loading…
Reference in a new issue