diff --git a/lib/monitor.js b/lib/monitor.js index c84f78d..e9cc8c9 100755 --- a/lib/monitor.js +++ b/lib/monitor.js @@ -11,6 +11,7 @@ var Package = require('../package.json'); var ProcessMonitor = require('./process'); var Schema = require('./schema'); var System = require('./system'); +var Utils = require('./utils'); // Declare internals @@ -49,6 +50,13 @@ module.exports = internals.Monitor = function (server, options) { this._state = {}; this._server = server; + // Options used to create Great Response + this._responseOptions = { + logRequestHeaders: this.settings.logRequestHeaders, + logRequestPayload: this.settings.logRequestPayload, + logResponsePayload: this.settings.logResponsePayload + }; + // Validate settings Schema.assert('monitorOptions', this.settings); @@ -182,109 +190,23 @@ internals.Monitor.prototype.stop = function () { internals.Monitor.prototype._logHandler = function (event) { - event = { - event: 'log', - timestamp: event.timestamp, - tags: event.tags, - data: event.data, - pid: process.pid - }; - - this.emit('report', 'log', event); + this.emit('report', 'log', Utils.GreatLog(event)); }; internals.Monitor.prototype._errorHandler = function (request, error) { - var event = { - event: 'error', - timestamp: request.info.received, - url: request.url, - method: request.method, - pid: process.pid, - error: error - }; - - event.toJSON = function () { - - var errObject = this.error; - this.error = { - message: errObject.message, - stack: errObject.stack - }; - return this; - }; - - this.emit('report', 'error', event); + this.emit('report', 'error', Utils.GreatError(request, error)); }; internals.Monitor.prototype._responseHandler = function (request) { - var req = request.raw.req; - var res = request.raw.res; - - var event = { - event: 'response', - timestamp: request.info.received, - id: request.id, - instance: request.connection.info.uri, - labels: request.connection.settings.labels, - method: request.method, - path: request.path, - query: request.query, - responseTime: Date.now() - request.info.received, - statusCode: res.statusCode, - pid: process.pid, - source: { - remoteAddress: request.info.remoteAddress, - userAgent: req.headers['user-agent'], - referer: req.headers.referer - } - }; - - event.log = request.getLog(); - - if (this.settings.logRequestHeaders) { - event.headers = req.headers; - } - - if (this.settings.logRequestPayload) { - event.requestPayload = request.payload; - } - - if (this.settings.logResponsePayload) { - event.responsePayload = request.response.source; - } - - this.emit('report', 'response', event); + this.emit('report', 'response', Utils.GreatResponse(request, this._responseOptions)); }; internals.Monitor.prototype._opsHandler = function (results) { - var event = { - event: 'ops', - timestamp: Date.now(), - host: results.host, - pid: process.pid, - os: { - load: results.osload, - mem: results.osmem, - uptime: results.osup - }, - proc: { - uptime: results.psup, - mem: results.psmem, - delay: results.psdelay - }, - load: { - requests: results.requests, - concurrents: results.concurrents, - responseTimes: results.responseTimes, - sockets: results.sockets - } - }; - - this.emit('report', 'ops', event); + this.emit('report', 'ops', Utils.GreatOps(results)); }; diff --git a/lib/utils.js b/lib/utils.js index 41f74d2..d20f87b 100755 --- a/lib/utils.js +++ b/lib/utils.js @@ -16,3 +16,119 @@ exports.makeContinuation = function (predicate) { }); }; }; + + +exports.GreatLog = function (event) { + + if (this.constructor === exports.GreatLog) { + this.event = 'log'; + this.timestamp = event.timestamp; + this.tags = event.tags; + this.data = event.data; + this.pid = process.pid; + } + else { + return new exports.GreatLog(event); + } +}; + + +exports.GreatError = function (request, error) { + + if (this.constructor === exports.GreatError) { + this.event = 'error'; + this.timestamp = request.info.received; + this.url = request.url; + this.method = request.method; + this.pid = process.pid; + this.error = error; + } + else { + return new exports.GreatError(request, error); + } +}; + + +exports.GreatError.prototype.toJSON = function () { + + var errObject = this.error; + this.error = { + message: errObject.message, + stack: errObject.stack + }; + return this; +}; + + +exports.GreatResponse = function (request, options) { + + if (this.constructor === exports.GreatResponse) { + + var req = request.raw.req; + var res = request.raw.res; + + this.event = 'response'; + this.timestamp = request.info.received; + this.id = request.id; + this.instance = request.connection.info.uri; + this.labels = request.connection.settings.labels; + this.method = request.method; + this.path = request.path; + this.query = request.query; + this.responseTime = Date.now() - request.info.received; + this.statusCode = res.statusCode; + this.pid = process.pid; + this.source = { + remoteAddress: request.info.remoteAddress, + userAgent: req.headers['user-agent'], + referer: req.headers.referer + }; + + this.log = request.getLog(); + + if (options.logRequestHeaders) { + this.headers = req.headers; + } + + if (options.logRequestPayload) { + this.requestPayload = request.payload; + } + + if (options.logResponsePayload) { + this.responsePayload = request.response.source; + } + } + else { + return new exports.GreatResponse(request, options); + } +}; + + +exports.GreatOps = function (ops) { + + if (this.constructor === exports.GreatOps) { + this.event = 'ops'; + this.timestamp = Date.now(); + this.host = ops.host; + this.pid = process.pid; + this.os = { + load: ops.osload, + mem: ops.osmem, + uptime: ops.osup + }; + this.proc = { + uptime: ops.psup, + mem: ops.psmem, + delay: ops.psdelay + }; + this.load = { + requests: ops.requests, + concurrents: ops.concurrents, + responseTimes: ops.responseTimes, + sockets: ops.sockets + }; + } + else { + return new exports.GreatOps(ops); + } +};