Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debug interface, log interface #94

Merged
merged 1 commit into from
Aug 22, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions lib/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2012 Walmart. All rights reserved. Copyrights licensed under the New BSD License.
* See LICENSE file included with this code project for license terms.
*/

// Load modules

var Err = require('./error');


// Declare internals

var internals = {

events: {} // Map: debug session -> events array
};


exports.report = function (session, event) {

internals.events[session] = internals.events[session] || [];
internals.events[session].push(event);
};


exports.session = function (request) {

if (internals.events[request.params.id]) {

request.reply(internals.events[request.params.id]);
}
else {

request.reply(Err.notFound());
}
};

15 changes: 14 additions & 1 deletion lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ exports.server = {

// Caching (see exports.cache for expected content)

cache: null
cache: null,

// Debugging interface (see exports.debug for expected content)

debug: null
};


Expand Down Expand Up @@ -144,3 +148,12 @@ exports.cache = {
}
};


// Debug interface

exports.debug = {

debugEndpoint: '/debug/session',
queryKey: 'debug'
};

71 changes: 65 additions & 6 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var NodeUtil = require('util');
var Events = require('events');
var Url = require('url');
var Utils = require('./utils');
var Log = require('./log');
var Debug = require('./debug');


// Declare internals
Expand Down Expand Up @@ -54,17 +54,40 @@ exports = module.exports = Request = function (server, req, res) {
res: res
};

this._response = {};
this._log = [];
this._response = {}; // { result, error, options }
this._analytics = {

startTime: now
};

// Defined elsewhere:
//
// _response: { result, error, options }
// Extract session debugging information

if (this.server.settings.debug) {

if (this.query[this.server.settings.debug.queryKey]) {

this._debug = this.query[this.server.settings.debug.queryKey];

delete this.url.search;
delete this.query[this.server.settings.debug.queryKey];

this._raw.req.url = Url.format(this.url);
this.setUrl(this._raw.req.url);
}
}

// Log request

var about = {

method: this.method,
url: this.url.href,
agent: this._raw.req.headers['user-agent']
};

this.log('http', 'received', '', about, now)

Log.info('Received', this);
return this;
};

Expand All @@ -84,3 +107,39 @@ Request.prototype.setMethod = function (method) {
this.method = method.toLowerCase();
};


Request.prototype.log = function (category, event, message, data, timestamp) {

// Prepare log item

var now = (timestamp ? (timestamp instanceof Date ? timestamp.getTime() : timestamp) : Utils.getTimestamp());
var item = {

timestamp: now,
category: category,
event: event
};

if (message) {

item.message = message;
}

if (data) {

item.data = data;
}

// Add to array

this._log.push(item);

// Pass to debug

if (this._debug) {

Debug.report(this._debug, item);
}
};


22 changes: 21 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var Session = require('./session');
var Cache = require('./cache');
var Request = require('./request');
var Route = require('./route');
var Debug = require('./debug');


// Declare internals
Expand Down Expand Up @@ -129,7 +130,7 @@ exports.Server = Server = function (host, port, options, routes) {
this.listener = Http.createServer(this.dispatch());
}

// Initialize Monitoring if set
// Initialize monitoring if set

this.monitor = new Monitor(this, this.settings, Log);

Expand All @@ -152,6 +153,24 @@ exports.Server = Server = function (host, port, options, routes) {
});
}

// Setup debug endpoint

if (this.settings.debug) {

this.settings.debug = Utils.applyToDefaults(Defaults.debug, (typeof this.settings.debug === 'boolean' ? {} : this.settings.debug));

this.addRoute({

method: 'GET',
path: this.settings.debug.debugEndpoint + '/:id',
config: {

handler: Debug.session,
authentication: 'none'
}
});
}

// Add routes

if (routes) {
Expand All @@ -174,6 +193,7 @@ Server.prototype.dispatch = function () {
// Create request object

var request = new Request(self, req, res);
Log.info('Received', request);

// Execute onRequest extensions (can change request method, url, etc.)

Expand Down