UNPKG

ca-apm-probe

Version:

CA APM Node.js Agent monitors real-time health and performance of Node.js applications

162 lines (126 loc) 5.21 kB
/** * 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 probeNameResolver = require('./lib/probename-resolver'); //export this module's single function //allows for: require('ca-agent')( options ); module.exports = exports = startAgent; function startAgent(options) { options.probeName = probeNameResolver.initialize(options); probeNameResolver.setProbeName(options.probeName); var configData = require("./lib/configdata"); configData.startConfiguration(options); var config = configData.getConfigData(); var collectormsg = require("./lib/collectormsg"); var stack = require('./lib/virtualstack'); var metrics = require('./lib/metrics'); var agent = require('./lib/agent'); var logger = require("./lib/logger.js"); var envinfo = require('./lib/envinfo'); var cluster = require('cluster'); require('./lib/probes/cluster')(cluster); if (options.slc) { require('./lib/probes/cluster')(options.slc); return; } options = resolveOptions(options); if (config.protocol == "http2"){ //TODO HTTP2 Not yet certified clientProtocol = require('./lib/http2-client'); } else if (config.protocol == "http"){ clientProtocol = require('./lib/http-client'); } else { clientProtocol = require('./lib/arf-client'); } agent.configure(options); agent.setAsynchConfigEventHandlers(collectormsg.sendRequireEvent, collectormsg.receiveRequireEventResponse, collectormsg.repostRequireEvent); agent.setAsynchEventModelHandlers(stack.pushAsyncTracerEvent, stack.popAsyncTracerEvent, stack.finishAsyncTracer); metrics.setMetricEventHandler(collectormsg.sendMetricEvent); clientProtocol.start(options); agent.setProbeName(options.probeName); stack.setTraceHandlers(clientProtocol.startTrace, clientProtocol.endTrace); envinfo.log(); agent.use(function (name, value) { //todo: repurpose this function to actually do the send to arf client. logger.debug('%s=%d', name, value); }); agent.on('poll::start', function () { logger.debug("Poll Start"); }); agent.on('poll::stop', function () { logger.debug("Poll Stop"); }); return this; } function resolveOptions(options) { var config = require("./lib/configdata").getConfigData(); var logger = require("./lib/logger.js"); if (!options) { var options = {}; } if (!options.protocol) { options.protocol = process.env.PROTOCOL; if (!options.protocol) { options.protocol = config.protocol; } if (!options.protocol) { options.protocol = 'arf'; } } if (!options.collectorAgentHost) { options.collectorAgentHost = process.env.INFRASTRUCTURE_AGENT_HOST || process.env.COLLECTOR_AGENT_HOST; if(!options.collectorAgentHost) { options.collectorAgentHost = config.infrastructureAgent.host; } if(!options.collectorAgentHost) { options.collectorAgentHost = 'localhost'; } } if (!options.collectorAgentPort) { options.collectorAgentPort = process.env.INFRASTRUCTURE_AGENT_PORT || process.env.COLLECTOR_AGENT_PORT; if (!options.collectorAgentPort) { options.collectorAgentPort = config.infrastructureAgent.port; if(options.protocol == 'arf') { if(!options.collectorAgentPort) { options.collectorAgentPort = 5005; } } } } if (!options.interval) { if(!config.interval) { options.interval = 15000; } else { options.interval = config.interval; } } if (logger.isDebug()) { logger.debug('final resolved options:\n %s', require('util').inspect(options)); } return options; } // This is for backward compatibility module.exports.start = function (collectorAgentHost, collectorAgentPort, probeName) { var options; if (typeof collectorAgentHost === "object") { options = collectorAgentHost; } else { options = {collectorAgentHost: collectorAgentHost, collectorAgentPort: collectorAgentPort, probeName: probeName}; } startAgent(options); };