mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-23 07:38:20 -05:00
Support sending performance information to stats.
This commit is contained in:
parent
7a86f754df
commit
32861b025a
4 changed files with 53 additions and 0 deletions
|
@ -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": "",
|
||||
|
|
43
server/commons/perfmon.coffee
Normal file
43
server/commons/perfmon.coffee
Normal 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)
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue