ca-apm-probe
Version:
CA APM Node.js Agent monitors real-time health and performance of Node.js applications
125 lines (110 loc) • 3.8 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 proxy = require('../proxy');
var config = require('../configdata').getConfigData();
var targetModule = new Object;
module.exports = function (apollo) {
targetModule.apolloserver = apollo;
targetModule.runHttpQueryJs = findHttpQuery(targetModule.apolloserver, require, 'runHttpQuery.js');
targetModule.ApolloServerJs = findHttpQuery(targetModule.apolloserver, require, 'ApolloServer.js');
targetModule.nodeHttpToRequest = findNodeHttpToRequest(targetModule.apolloserver, require);
proxy.before(targetModule.apolloserver, 'runHttpQuery', queryHook);
proxy.before(targetModule.runHttpQueryJs, 'runHttpQuery', queryHook);
// if(config.graphql.apolloServer.enabled){
// proxy.before(targetModule.ApolloServerJs.ApolloServerBase.prototype, 'ensurePluginInstantiation', addPlugin);
// }
proxy.after(targetModule.nodeHttpToRequest, 'convertNodeHttpToRequest', updateRequest);
};
function updateRequest(obj, args, request, storage) {
request.__CA_ctx = args[0].__CA_ctx;
request.gqlQuery = args[0].body.query;
}
function queryHook(obj, args, storage) {
var ctx = args[1].request.__CA_ctx;
if(!ctx){
ctx = args[0][0].__CA_ctx;
} else{
storage.set('gqlQuery', args[1].request.gqlQuery);
}
storage.set('ctx', ctx)
}
function addPlugin(obj, args, storage) {
var plugin = require('../plugin/apollo-server-plugin');
if(args && args.length > 0){
var plugins = args[0];
if(!plugins){
args[0] = [plugin];
}else{
args[0][plugins.length] = plugin;
}
}
storage.get('ctx');
}
function findHttpQuery(parent, require) {
var cache = require.cache;
for (var key in cache) {
var candidate = cache[key];
if (candidate.exports !== parent) {
continue;
}
var dirname = path.dirname(candidate.filename);
var filename = path.join(dirname, 'runHttpQuery.js');
try {
return require(filename);
} catch (e) {
return null;
}
}
return null;
}
function findHttpQuery(parent, require, fileName) {
var cache = require.cache;
for (var key in cache) {
var candidate = cache[key];
if (candidate.exports !== parent) {
continue;
}
var dirname = path.dirname(candidate.filename);
var filename = path.join(dirname, fileName);
try {
return require(filename);
} catch (e) {
return null;
}
}
return null;
}
function findNodeHttpToRequest(parent, require) {
var cache = require.cache;
for (var key in cache) {
var candidate = cache[key];
if (candidate.exports !== parent) {
continue;
}
var dirname = path.dirname(candidate.filename);
var filename = path.join(dirname, 'nodeHttpToRequest.js');
try {
return require(filename);
} catch (e) {
return null;
}
}
return null;
}