Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made setting context modes idempotent #371

Merged
merged 2 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/core/lib/context_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ var contextUtils = {
}
},

/**
* Gets current CLS namespace for X-Ray SDK or creates one if absent.
* @returns {Namespace}
* @alias module:context_utils.getNamespace
*/
getNamespace: function getNamespace() {
return cls.getNamespace(NAMESPACE) || cls.createNamespace(NAMESPACE);
},
Expand Down Expand Up @@ -139,7 +144,7 @@ var contextUtils = {

enableAutomaticMode: function enableAutomaticMode() {
cls_mode = true;
cls.createNamespace(NAMESPACE);
contextUtils.getNamespace(NAMESPACE);

logger.getLogger().debug('Overriding AWS X-Ray SDK mode. Set to automatic mode.');
},
Expand All @@ -153,7 +158,7 @@ var contextUtils = {
enableManualMode: function enableManualMode() {
cls_mode = false;

if (contextUtils.getNamespace(NAMESPACE))
if (cls.getNamespace(NAMESPACE))
cls.destroyNamespace(NAMESPACE);

logger.getLogger().debug('Overriding AWS X-Ray SDK mode. Set to manual mode.');
Expand Down
16 changes: 16 additions & 0 deletions packages/core/test/unit/context_utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,20 @@ describe('ContextUtils', function() {
assert.equal(ContextUtils.getNamespace().name, 'AWSXRay');
});
});

describe('#enableAutomaticMode', () => {
it('should respect existing CLS namespaces', (done) => {
ContextUtils.enableAutomaticMode();
const ns = ContextUtils.getNamespace();
const seg = new Segment('test');
ns.run(() => {
ContextUtils.setSegment(seg);

// Calling this again should do nothing
ContextUtils.enableAutomaticMode();
assert.deepEqual(ContextUtils.getSegment(), seg);
done();
});
});
});
});