From 54aeac231e2ba98a3f78aff460ea03134ac24cc5 Mon Sep 17 00:00:00 2001 From: Zachary Carter <zack.carter@gmail.com> Date: Wed, 18 Jul 2012 11:06:07 -0700 Subject: [PATCH] move metrics to router: avoids headers not being forwarded and caching - issue #2040 --- bin/router | 5 ++ lib/metrics.js | 3 ++ lib/static/views.js | 1 - tests/metrics-header-test.js | 93 ++++++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100755 tests/metrics-header-test.js diff --git a/bin/router b/bin/router index 1d333c310..6b27d7ead 100755 --- a/bin/router +++ b/bin/router @@ -13,6 +13,7 @@ urlparse = require('urlparse'), express = require('express'); const +metrics = require('../lib/metrics.js'), wsapi = require('../lib/wsapi.js'), config = require('../lib/configuration.js'), heartbeat = require('../lib/heartbeat.js'), @@ -146,6 +147,10 @@ wsapi.routeSetup(app, { //catch-all app.use(function(req, res, next) { + + // log metrics + if (req.url === '/sign_in') metrics.userEntry(req); + forward( static_url+req.url, req, res, function(err) { diff --git a/lib/metrics.js b/lib/metrics.js index c5e2cd2b5..eeabd14d8 100644 --- a/lib/metrics.js +++ b/lib/metrics.js @@ -56,6 +56,9 @@ function setupLogger() { mkdir_p(log_path); var filename = path.join(log_path, configuration.get('process_type') + "-metrics.json"); + if (process.env.METRICS_LOG_FILE) { + filename = process.env.METRICS_LOG_FILE; + } LOGGER = new (winston.Logger)({ transports: [new (winston.transports.File)({filename: filename})], diff --git a/lib/static/views.js b/lib/static/views.js index 2f4660c60..62695b442 100644 --- a/lib/static/views.js +++ b/lib/static/views.js @@ -91,7 +91,6 @@ exports.setup = function(app) { // this should probably be an internal redirect // as soon as relative paths are figured out. app.get('/sign_in', function(req, res, next ) { - metrics.userEntry(req); renderCachableView(req, res, 'dialog.ejs', { title: _('A Better Way to Sign In'), layout: 'dialog_layout.ejs', diff --git a/tests/metrics-header-test.js b/tests/metrics-header-test.js new file mode 100755 index 000000000..9ff4fcfef --- /dev/null +++ b/tests/metrics-header-test.js @@ -0,0 +1,93 @@ +#!/usr/bin/env node + +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +require('./lib/test_env.js'); + +const assert = require('assert'), +fs = require('fs'), +path = require('path'), +http = require('http'), +vows = require('vows'), +start_stop = require('./lib/start-stop.js'), +wsapi = require('./lib/wsapi.js'), +urlparse = require('urlparse'); + +var suite = vows.describe('metrics header test'); +suite.options.error = false; + +// allow this unit test to be targeted +var SERVER_URL = process.env['SERVER_URL'] || 'http://127.0.0.1:10002/'; + +process.env.METRICS_LOG_FILE = path.resolve(path.join(__dirname, 'data', 'metrics.json')); + +if (!process.env['SERVER_URL']) { + // start up a pristine server if we're locally testing + start_stop.addStartupBatches(suite); +} + +// now parse out host, port and scheme +var purl = urlparse(SERVER_URL); +const method = (purl.scheme === 'https') ? require('https') : require('http'); + +function doRequest(path, headers, cb) { + var req = method.request({ + port: purl.port, + host: purl.host, + path: path, + headers: headers, + agent: false + }, function(res) { + req.abort(); + cb(null, res); + }); + req.on('error', function(e) { + cb(e); + }); + req.end(); +} + +suite.addBatch({ + '/sign_in': { + topic: function() { + doRequest('/sign_in', {'user-agent': 'Test Runner', 'x-real-ip': '123.0.0.1', 'referer': 'https://persona.org'}, this.callback); + }, + "metrics log exists": { + topic: function (err, r) { + if (fs.existsSync(process.env.METRICS_LOG_FILE)) { + this.callback(); + } else { + fs.watchFile(process.env.METRICS_LOG_FILE, null, this.callback); + } + }, + "metric fields are logged properly": function (event, filename) { + var metrics = JSON.parse(fs.readFileSync(process.env.METRICS_LOG_FILE, "utf8").trim()); + var message = JSON.parse(metrics.message); + assert.equal(message.ip, "123.0.0.1"); + assert.equal(message.rp, "https://persona.org"); + assert.equal(message.browser, "Test Runner"); + fs.unwatchFile(process.env.METRICS_LOG_FILE); + } + } + } +}); + + +suite.addBatch({ + 'clean up': function () { + fs.unlink(process.env.METRICS_LOG_FILE); + delete process.env.METRICS_LOG_FILE; + } +}); + +// shut the server down and cleanup +if (!process.env['SERVER_URL']) { + start_stop.addShutdownBatches(suite); +} + + +// run or export the suite. +if (process.argv[1] === __filename) suite.run(); +else suite.export(module); -- GitLab