Support sending performance information to stats.

This commit is contained in:
Rob 2015-11-17 14:57:12 -08:00
parent 7a86f754df
commit 32861b025a
4 changed files with 53 additions and 0 deletions

View file

@ -72,6 +72,7 @@
"mongoose-cache": "~0.1.4",
"node-force-domain": "~0.1.0",
"node-gyp": "~0.13.0",
"node-statsd": "^0.1.1",
"passport": "0.1.x",
"passport-local": "0.1.x",
"redis": "",

View file

@ -0,0 +1,43 @@
fs = require 'fs'
path = require 'path'
config = require '../../server_config'
StatsD = require 'node-statsd'
if config.statsd
realClient = new StatsD(config.statsd)
else
mock = new StatsD(mock: true)
exports.client = realClient or mock
exports.middleware = (req, res, next) ->
req.statsd = exports.client
if realClient
time = process.hrtime();
cleanup = ->
res.removeListener 'finish', recordMetrics
res.removeListener 'error', cleanup
res.removeListener 'close', cleanup
recordMetrics = ->
diff = process.hrtime(time);
ms = (diff[0] * 1000 + diff[1] / 1e6);
path = req.route?.path or '/*'
stat = req.method + "." + path.replace /[^A-Za-z0-9]+/g, '_'
realClient.timing stat, ms
res.once 'finish', recordMetrics
res.once 'error', cleanup
res.once 'close', cleanup
else
req.statsd = mock
next() unless not next
exports.trace = (name, callback) ->
return callback unless realClient
time = process.hrtime()
(args...) ->
realClient.timing name, ms
return callback.apply(this, args)

View file

@ -84,5 +84,10 @@ if not config.unittest and not config.isProduction
# change artificially slow down non-static requests for testing
config.slow_down = false
if process.env.COCO_STATSD_HOST
config.statsd =
host: process.env.COCO_STATSD_HOST
port: process.env.COCO_STATSD_PORT or 8125
prefix: process.env.COCO_STATSD_PREFIX or ''
module.exports = config

View file

@ -8,6 +8,7 @@ compressible = require 'compressible'
geoip = require 'geoip-lite'
database = require './server/commons/database'
perfmon = require './server/commons/perfmon'
baseRoute = require './server/routes/base'
user = require './server/users/user_handler'
logging = require './server/commons/logging'
@ -127,8 +128,11 @@ setupRedirectMiddleware = (app) ->
nameOrID = req.path.split('/')[3]
res.redirect 301, "/user/#{nameOrID}/profile"
setupPerfMonMiddleware = (app) ->
app.use perfmon.middleware
exports.setupMiddleware = (app) ->
setupPerfMonMiddleware app
setupCountryRedirectMiddleware app, "china", "CN", "zh", "tokyo"
setupCountryRedirectMiddleware app, "brazil", "BR", "pt-BR", "saoPaulo"
setupMiddlewareToSendOldBrowserWarningWhenPlayersViewLevelDirectly app