ca-apm-probe
Version:
CA APM Node.js Agent monitors real-time health and performance of Node.js applications
105 lines (96 loc) • 3.69 kB
JavaScript
/**
* Copyright (c) 2015 CA. All rights reserved.
*
* This software and all information contained therein is confidential and proprietary and
* shall not be duplicated, used, disclosed or disseminated in any way except as authorized
* by the applicable license agreement, without the express written permission of CA. All
* authorized reproductions must be marked with this language.
*
* EXCEPT AS SET FORTH IN THE APPLICABLE LICENSE AGREEMENT, TO THE EXTENT
* PERMITTED BY APPLICABLE LAW, CA PROVIDES THIS SOFTWARE WITHOUT WARRANTY
* OF ANY KIND, INCLUDING WITHOUT LIMITATION, ANY IMPLIED WARRANTIES OF
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL CA BE
* LIABLE TO THE END USER OR ANY THIRD PARTY FOR ANY LOSS OR DAMAGE, DIRECT OR
* INDIRECT, FROM THE USE OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, LOST
* PROFITS, BUSINESS INTERRUPTION, GOODWILL, OR LOST DATA, EVEN IF CA IS
* EXPRESSLY ADVISED OF SUCH LOSS OR DAMAGE.
*/
var agent = require('../agent');
var proxy = require('../proxy');
var extend = require('util')._extend;
var inspect = require('util').inspect;
var logger = require('../logger');
var commands = {
create: 'create',
findOrCreate: 'find_or_create',
exists: 'exists',
find: 'find',
findById: 'find_by_id',
remove: 'remove',
removeById: 'remove_by_id',
count: 'count'
};
var instanceCommands = {
save: 'save',
remove: 'remove',
updateAttribute: 'update_attribute',
updateAttributes: 'update_attributes',
reload: 'reload'
};
module.exports = function(juggler) {
var dao = juggler.Schema.DataAccessObject;
function createPatcher(obj) {
return function patch(command) {
var _old = dao[command];
proxy.before(obj, command, function(obj, args, storage) {
var cmd = commands[command];
if (!cmd) {
cmd = instanceCommands[command];
}
var query = '';
if (typeof args[0] === 'string') {
query = args[0];
}
var loopBackObject = obj.definition.name;
var eventNameFormatted = 'loopbackDAO.' + loopBackObject + '_' + cmd;
//console.log(eventNameFormatted + ':' + query);
var ctx = storage.get('ctx');
var debug = logger.isDebug();
if (debug) {
if (ctx != null) {
logger.debug('loopbackDAO.%s[%d %d %d]: %s', loopBackObject, ctx.txid, ctx.lane, ctx.evtid, cmd);
}
else {
logger.debug('loopbackDAO.%s - no context: %s', loopBackObject,cmd);
logger.debug((new Error('No context')).stack);
}
}
ctx = agent.asynchEventStart(ctx, eventNameFormatted, null);
storage.set('ctx', ctx);
proxy.callback(args, -1, function(obj, args, storage) {
var errorObject = agent.checkAndSetErrorObject(args, 'MySQLError');
// CA code start
if (ctx != null) {
if (debug) {
logger.debug('loopbackDAO.%s[%d %d %d]: callback', loopBackObject, ctx.txid, ctx.lane, ctx.evtid);
}
ctx = agent.asynchEventDone(ctx, eventNameFormatted, null, errorObject);
storage.set('ctx', ctx);
}
else {
if (debug) {
logger.debug('loopbackDAO.%s - no context: callback', loopBackObject);
logger.debug((new Error('No context')).stack);
}
}
},
function(obj, args) {
if (ctx != null) agent.asynchEventFinish(ctx);
}); // CA code end
});
extend(dao[command], _old);
};
}
Object.keys(commands).forEach(createPatcher(dao));
Object.keys(instanceCommands).forEach(createPatcher(dao.prototype));
};