diff --git a/packages/core/README.md b/packages/core/README.md index 5c9655bc..4be00797 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -22,9 +22,17 @@ mechanisms, but a few are supplied. See Capturing Function Calls below. The AWS X-Ray SDK has two modes: `manual` and `automatic`. By default, the SDK is in automatic mode. You can flip the mode of the SDK using the following: - AWSXRay.enableManualMode(); +```js +AWSXRay.enableAutomaticMode(); + +AWSXRay.enableManualMode(); - AWSXRay.enableAutomaticMode(); +/* see https://github.com/aws/aws-xray-sdk-node/pull/595 + for details on using this environment variable + to prevent memory leaks when using manual mode + */ +process.env.AWS_XRAY_MANUAL_MODE = 'true'; +``` #### Automatic mode @@ -54,6 +62,7 @@ section for different usages. **Environment variables always override values set in code.** AWS_XRAY_DEBUG_MODE Enables logging of debug messages to console output. Logging to a file is no longer built in. See 'configure logging' below. + AWS_XRAY_MANUAL_MODE For overriding the default automatic mode. See 'Automatic mode'. AWS_XRAY_TRACING_NAME For overriding the default segment name to use with the middleware. See 'dynamic and fixed naming modes'. AWS_XRAY_DAEMON_ADDRESS For setting the daemon address and port. diff --git a/packages/core/lib/context_utils.js b/packages/core/lib/context_utils.js index d6f75718..fe20f440 100644 --- a/packages/core/lib/context_utils.js +++ b/packages/core/lib/context_utils.js @@ -205,8 +205,13 @@ var contextUtils = { } }; -cls.createNamespace(NAMESPACE); -logger.getLogger().debug('Starting the AWS X-Ray SDK in automatic mode (default).'); +if (process.env.AWS_XRAY_MANUAL_MODE) { + cls_mode = false; + logger.getLogger().debug('Starting the AWS X-Ray SDK in manual mode.'); +} else { + cls.createNamespace(NAMESPACE); + logger.getLogger().debug('Starting the AWS X-Ray SDK in automatic mode (default).'); +} if (process.env.AWS_XRAY_CONTEXT_MISSING) { contextUtils.setContextMissingStrategy(process.env.AWS_XRAY_CONTEXT_MISSING); diff --git a/packages/core/test/unit/context_utils.test.js b/packages/core/test/unit/context_utils.test.js index 26756134..593a32f3 100644 --- a/packages/core/test/unit/context_utils.test.js +++ b/packages/core/test/unit/context_utils.test.js @@ -11,6 +11,7 @@ var RUNTIME_ERROR = 'RUNTIME_ERROR'; var RUNTIME_ERROR_FCN_NAME = 'contextMissingRuntimeError'; var IGNORE_ERROR = 'IGNORE_ERROR'; var IGNORE_ERROR_FCN_NAME = 'contextMissingIgnoreError'; +var cls = require('cls-hooked/context'); describe('ContextUtils', function() { function reloadContextUtils() { @@ -30,11 +31,20 @@ describe('ContextUtils', function() { afterEach(function() { sandbox.restore(); delete process.env.AWS_XRAY_CONTEXT_MISSING; + delete process.env.AWS_XRAY_MANUAL_MODE; + cls.reset(); reloadContextUtils(); }); + it('should start in manual mode when process.env.AWS_XRAY_MANUAL_MODE is present', function() { + process.env.AWS_XRAY_MANUAL_MODE = '1'; + cls.reset(); + reloadContextUtils(); + assert.equal(cls.getNamespace('AWSXRay'), undefined); + }); + it('should start in automatic mode by creating the X-Ray namespace', function() { - assert.equal(ContextUtils.getNamespace().name, 'AWSXRay'); + assert.notEqual(cls.getNamespace('AWSXRay'), undefined); }); it('should set the contextMissingStrategy to LOG_ERROR by default', function() {