From 32861b025a96c6acfe9714a1460aba5ad45a97b0 Mon Sep 17 00:00:00 2001 From: Rob Date: Tue, 17 Nov 2015 14:57:12 -0800 Subject: [PATCH] Support sending performance information to stats. --- package.json | 1 + server/commons/perfmon.coffee | 43 +++++++++++++++++++++++++++++++++++ server_config.coffee | 5 ++++ server_setup.coffee | 4 ++++ 4 files changed, 53 insertions(+) create mode 100644 server/commons/perfmon.coffee diff --git a/package.json b/package.json index 36a5dc034..0dfd4bc87 100644 --- a/package.json +++ b/package.json @@ -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": "", diff --git a/server/commons/perfmon.coffee b/server/commons/perfmon.coffee new file mode 100644 index 000000000..e03b6145f --- /dev/null +++ b/server/commons/perfmon.coffee @@ -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) \ No newline at end of file diff --git a/server_config.coffee b/server_config.coffee index 4507a8d7e..b8468f4c9 100644 --- a/server_config.coffee +++ b/server_config.coffee @@ -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 diff --git a/server_setup.coffee b/server_setup.coffee index ee327a830..230f7c56b 100644 --- a/server_setup.coffee +++ b/server_setup.coffee @@ -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