ca-apm-probe
Version:
CA APM Node.js Agent monitors real-time health and performance of Node.js applications
114 lines (83 loc) • 3.25 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 path = require('path');
var agent = require('ca-apm-probe/lib/agent');
var proxy = require('ca-apm-probe/lib/proxy');
var util = require('util');
var methods = require('methods');
var logger = require("ca-apm-probe/lib/logger");
var targetModule = new Object;
module.exports = function (KoaRoute) {
targetModule.router = KoaRoute;
targetModule.methodMap = getMethodsWithProbes();
// instrument each route defined by application
methods.forEach(function wrap(method) {
proxy.before(KoaRoute, method, instrumentRoutes)
});
};
// instrument a given route by registering a call back
function instrumentRoutes(obj, args, storage) {
proxy.callback(args, 1, routeHook, null);
}
// route hook call back for each registered route request
function routeHook(obj, args, storage) {
logger.debug('koa-route hook');
var res = args[0].res;
var req = args[0].req;
var eventNameFormatted = 'koa.route';
var ctx = storage.get('ctx') || req.__CA_ctx;
if (logger.isDebug()) {
if (ctx) {
logger.debug('%s[%d %d %d]: path - %s', eventNameFormatted, ctx.txid, ctx.lane, ctx.evtid, req.url);
}
else {
logger.debug('%s - no context', eventNameFormatted);
logger.debug((new Error('No context')).stack);
}
}
ctx = agent.asynchEventStart(ctx, eventNameFormatted, {
route: args[0].routePath,
http_method: req.method
});
storage.set('ctx', ctx);
var errorObject = new Object();
var end_save = res.end;
res.end = function () {
if (res.finished === false && ctx) {
ctx = agent.asynchEventDone(ctx, eventNameFormatted, null,
errorObject);
if (ctx) {
agent.asynchEventFinish(ctx);
}
}
end_save.apply(res, arguments);
};
}
function getMethodsWithProbes() {
if (!targetModule.methodMap) {
var mt = new Object;
mt[0] = 'koa#route';
targetModule.methodMap = mt;
}
return targetModule.methodMap;
}
function instrument(methodMap) {
targetModule.methodMap = methodMap;
}
module.exports.getMethodsWithProbes = getMethodsWithProbes;
module.exports.instrument = instrument.bind(module);