Skip to content

Commit

Permalink
fix #594 memory leaks caused by cls-hooked (#595)
Browse files Browse the repository at this point in the history
* fix #594 memory leaks caused by cls-hooked

* fix #594 memory leaks caused by cls-hooked

* fix #594 memory leaks caused by cls-hooked

* fix #594 memory leaks caused by cls-hooked
  • Loading branch information
regevbr authored Sep 18, 2023
1 parent 5eb97ef commit 4266c27
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
13 changes: 11 additions & 2 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
9 changes: 7 additions & 2 deletions packages/core/lib/context_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 11 additions & 1 deletion packages/core/test/unit/context_utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down

0 comments on commit 4266c27

Please sign in to comment.