deep-framework
Version:
320 lines (251 loc) • 8.21 kB
JavaScript
var _ = require('underscore');
var CLSUtils = require('./utils').CLSUtils;
var MWUtils = require('./middleware/mw_utils');
var SamplingRules = require('./sampling/sampling_rules');
var SegmentUtils = require('./segments/segment_utils');
/**
* A module representing the AWSXRay SDK.
* @namespace AWSXRay
*/
var AWSXRay = {
/**
* @memberof AWSXRay
* @type {object}
* @namespace AWSXRay.plugins
*/
plugins: {
/**
* Exposes the AWS EC2 plugin.
* @memberof AWSXRay.plugins
*/
EC2: require('./segments/plugins/ec2_plugin'),
/**
* Exposes the AWS ECS plugin.
* @memberof AWSXRay.plugins
*/
ECS: require('./segments/plugins/ecs_plugin'),
/**
* Exposes the AWS Elastic Beanstalk plugin.
* @memberof AWSXRay.plugins
*/
ElasticBeanstalk: require('./segments/plugins/beanstalk_plugin'),
},
/**
* Enables use of plugins to capture additional data for segments.
* @param {Array} plugins - A configurable subset of AWSXRay.plugins.
* @memberof AWSXRay
* @see AWSXRay.plugins
*/
config: function (plugins) {
var length = plugins.length;
var origin, pluginData = {};
var loadPlugins = function(setOrigin) {
var counter = length;
for (var i = 0; i < length; i++) {
plugins[i](function (data) {
if (data) {
_.extend(pluginData, data);
}
counter = --counter;
if (counter <= 0) {
setOrigin();
}
});
}
};
var setOrigin = function () {
if (_.isEmpty(pluginData))
return;
SegmentUtils.setPluginData(pluginData);
if (pluginData.elastic_beanstalk)
origin = 'AWS::ElasticBeanstalk::Environment';
else if (pluginData.ecs)
origin = 'AWS::ECS::Container';
else if (pluginData.ec2)
origin = 'AWS::EC2::Instance';
if (origin)
SegmentUtils.setOrigin(origin);
};
loadPlugins(setOrigin);
},
/**
* Overrides the default whitelisting file to specify what params to capture on each AWS Service call.
* If a service or API is not listed, no additional data is captured.
* The base whitelisting file can be found at /lib/resources/aws_whitelist.json
* @param {string} location - The path to the custom whitelist file.
* @memberof AWSXRay
*/
setAWSWhitelist: function (location) {
require('./segments/attributes/aws').setAWSWhitelist(location);
},
/**
* Overrides the default sampling rules file to specify at what rate to sample at for specific routes.
* The base sampling rules file can be found at /lib/resources/default_sampling_rules.json
* @param {string} location - The path to the custom sampling rules file.
* @memberof AWSXRay
*/
setSamplingRules: function (location) {
if(_.isUndefined(location))
throw new Error('Please specify a path to the local sampling rules file.');
MWUtils.setSampler(new SamplingRules(location));
},
/**
* Overrides the default streaming threshold (100).
* The threshold represents the maximum number of subsegments on a single segment before
* the SDK begins to send the completed subsegments out of band of the main segment.
* Reduce this threshold if you see the 'Segment too large to send' error.
* @param {number} threshold - The new threshold to use.
* @memberof AWSXRay
* @see module:capture.capture
*/
setStreamingThreshold: function setStreamingThreshold(threshold) {
return SegmentUtils.setStreamingThreshold(threshold);
},
/**
* Configures the address and port the daemon is expected to be on.
* @param {string} address - Address of the daemon the segments should be sent to. Expects 'x.x.x.x', ':yyyy' or 'x.x.x.x:yyyy' IPv4 formats.
* @module SegmentEmitter
* @memberof AWSXRay
* @function
* @see module:SegmentEmitter.setDaemonAddress
*/
setDaemonAddress: require('./segment_emitter').setDaemonAddress,
/**
* @param {string} name - The name of the new subsegment.
* @param {function} fcn - The function conext to wrap.
* @param {Segment|Subsegment} [parent] - The parent for the new subsegment, for manual mode.
* @memberof AWSXRay
* @function
* @see module:capture.capture
*/
capture: require('./capture').capture,
/**
* @param {string} name - The name of the new subsegment.
* @param {function} fcn - The function conext to wrap.
* @param {Segment|Subsegment} [parent] - The parent for the new subsegment, for manual mode.
* @memberof AWSXRay
* @function
* @see module:capture.capture
*/
captureAsync: require('./capture').captureAsync,
/**
* @param {string} name - The name of the new subsegment.
* @param {function} fcn - The function conext to wrap.
* @param {Segment|Subsegment} [parent] - The parent for the new subsegment, for manual mode.
* @memberof AWSXRay
* @function
* @see module:capture.captureCallback
*/
captureCallback: require('./capture').captureCallback,
/**
* @param {AWS} awssdk - The Javascript AWS SDK.
* @memberof AWSXRay
* @function
* @see module:aws_p.captureAWS
*/
captureAWS: require('./patchers/aws_p').captureAWS,
/**
* @param {AWS.Service} service - An instance of a AWS service to wrap.
* @memberof AWSXRay
* @function
* @see module:aws_p.captureAWSClient
*/
captureAWSClient: require('./patchers/aws_p').captureAWSClient,
/**
* @param {http|https} module - The built in Node.js HTTP or HTTPS module.
* @memberof AWSXRay
* @function
* @see module:http_p.captureHTTPs
*/
captureHTTPs: require('./patchers/http_p').captureHTTPs,
/**
* @param {function} function - The AWS Lambda handler to capture.
* @memberof AWSXRay
* @function
* @see module:lambda_p.captureLambda
*/
captureLambda: require('./patchers/lambda_p').captureLambda,
/**
* @param {mysql} module - The MySQL npm module.
* @memberof AWSXRay
* @function
* @returns {module}
* @see module:mysql_p.captureMySQL
*/
captureMySQL: require('./patchers/mysql_p').captureMySQL,
/**
* @param {pg} module - The PostgreSQL npm module.
* @memberof AWSXRay
* @function
* @returns {module}
* @see module:postgres_p.capturePostgres
*/
capturePostgres: require('./patchers/postgres_p').capturePostgres,
/**
* Enables manual mode.
* @memberof AWSXRay
* @function
* @see Utils.CLSUtils.enableManualMode
*/
enableManualMode: CLSUtils.enableManualMode,
/**
* Enables CLS mode.
* @memberof AWSXRay
* @function
* @see Utils.CLSUtils.enableCLSMode
*/
enableCLSMode: CLSUtils.enableCLSMode,
/**
* Exposes the Middleware Utils class.
* @memberof AWSXRay
* @see module:mw_utils
*/
middleware: require('./middleware/mw_utils'),
express: {
/**
* Returns Express.js middleware.
* @memberof AWSXRay.express
* @method openSegment
* @returns {function}
* @see module:express_mw.openSegment
*/
openSegment: require('./middleware/express_mw').openSegment,
/**
* Returns Express.js middleware.
* @memberof AWSXRay.express
* @method closeSegment
* @returns {function}
* @see module:express_mw.closeSegment
*/
closeSegment: require('./middleware/express_mw').closeSegment
},
/**
* Returns the current set segment or subsegment. Only available in CLS mode.
* @memberof AWSXRay
* @function
* @returns {Segment|Subsegment}
* @see Utils.CLSUtils.getSegment
*/
getSegment: CLSUtils.getSegment,
/**
* Exposes the segment class.
* @memberof AWSXRay
* @see Segment
*/
Segment: require('./segments/segment'),
/**
* Exposes the subsegment class.
* @memberof AWSXRay
* @see Subsegment
*/
Subsegment: require('./segments/attributes/subsegment'),
};
(function () {
if (process.env.npm_package_version)
SegmentUtils.setServiceVersion(process.env.npm_package_version);
require('pkginfo')(module);
SegmentUtils.setSDKVersion(module.exports.version);
CLSUtils.enableCLSMode();
MWUtils.setSampler(new SamplingRules());
})();
module.exports = AWSXRay;