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

Replace continuation-local-storage with cls-hooked #227

Merged
merged 2 commits into from
Dec 4, 2019
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
*.log
docs
package-lock.json
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ notifications:
on_success: never
on_failure: always

matrix:
include:
- node_js: "8"
env: TEST_ASYNC=test-async
- node_js: "10"
env: TEST_ASYNC=test-async
- node_js: "12"
env: TEST_ASYNC=test-async

script:
- lerna bootstrap --hoist
- lerna run test
- "if [[ $TEST_ASYNC == 'test-async' ]]; then lerna run $TEST_ASYNC || exit 0; fi"
6 changes: 3 additions & 3 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ The AWS X-Ray SDK (the SDK) automatically records information for incoming and o
requests and responses (via middleware). It also automatically records local data
such as function calls, time, variables (via metadata and annotations), and Amazon
EC2 instance data (via plugins). Currently, only Express
applications are supported for automatic capturing. See the [aws-xray-sdk-express]
(https://github.com/aws/aws-xray-sdk-node/tree/master/packages/express) package for additional information.
applications are supported for automatic capturing. See the
[aws-xray-sdk-express](https://github.com/aws/aws-xray-sdk-node/tree/master/packages/express) package for additional information.

The SDK exposes the Segment and Subsegment objects so you can create your own capturing
mechanisms, but a few are supplied.
Expand Down Expand Up @@ -302,7 +302,7 @@ the X-Ray SDK provides helper functions under `AWSXRay.middleware`.
See the [aws-xray-sdk-express](https://github.com/aws/aws-xray-sdk-node/tree/master/packages/express) module for more information.

For additional information about and examples for using the CLS namespace to create
a new context, see: https://github.com/othiym23/node-continuation-local-storage.
a new context, see: https://github.com/jeff-lewis/cls-hooked.

### Capture subsegmenets within chained native Promise using automatic mode

Expand Down
4 changes: 2 additions & 2 deletions packages/core/lib/aws-xray.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ var AWSXRay = {
isAutomaticMode: contextUtils.isAutomaticMode,

/**
* Enables automatic mode. Automatic mode uses 'continuation-local-storage'.
* @see https://github.com/othiym23/node-continuation-local-storage
* Enables automatic mode. Automatic mode uses 'cls-hooked'.
* @see https://github.com/jeff-lewis/cls-hooked
* @memberof AWSXRay
* @function
* @see module:context_utils.enableAutomaticMode
Expand Down
6 changes: 3 additions & 3 deletions packages/core/lib/context_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @module context_utils
*/

var cls = require('continuation-local-storage');
var cls = require('cls-hooked');

var logger = require('./logger');
var Segment = require('./segments/segment');
Expand Down Expand Up @@ -127,8 +127,8 @@ var contextUtils = {
},

/**
* Enables automatic mode. Automatic mode uses 'continuation-local-storage'.
* @see https://github.com/othiym23/node-continuation-local-storage
* Enables automatic mode. Automatic mode uses 'cls-hooked'.
* @see https://github.com/jeff-lewis/cls-hooked
* @alias module:context_utils.enableAutomaticMode
*/

Expand Down
5 changes: 3 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@types/continuation-local-storage": "*",
"atomic-batcher": "^1.0.2",
"aws-sdk": "^2.304.0",
"continuation-local-storage": "^3.2.0",
"cls-hooked": "^4.2.2",
"date-fns": "^1.29.0",
"pkginfo": "^0.4.0",
"semver": "^5.3.0"
Expand All @@ -38,7 +38,8 @@
},
"scripts": {
"test": "mocha --recursive ./test/ -R spec && tsd",
"test-d": "tsd"
"test-d": "tsd",
"test-async": "./node_modules/.bin/mocha --recursive ./test_async/ -R spec"
},
"keywords": [
"amazon",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
var assert = require('chai').assert;
var http = require('http');
var AWSXRay = require('../../');
var Segment = AWSXRay.Segment;

AWSXRay.enableAutomaticMode();

describe('Integration', function() {
describe('#async', function() {
it('should maintain segment in async functions', function(done) {
var sharedPromise = null;

var requestCount = 0;
var server = http
.createServer(function(req, res) {
var ns = AWSXRay.getNamespace();
ns.bindEmitter(req);
ns.bindEmitter(res);

ns.run(function () {
var segment = new Segment('root');

AWSXRay.setSegment(segment);

if (!sharedPromise) {
sharedPromise = Promise.resolve();
}

// execute an async task
sharedPromise.then(async () => {

await sleep(0);

var retrievedSegment = AWSXRay.getSegment();
res.end();

// setTimeout so the assertion isn't caught by the promise
setTimeout(function() {
assert.equal(segment.id, retrievedSegment.id);
if (++requestCount === 2){
done();
}
});
});
});
}).listen(8080, '0.0.0.0', function() {

var address = server.address();

var count = 0;
function cb(err) {
if (err) {
throw err;
}

if (++count === 2) {
server.close();
}
}
sendRequest(address, cb);
sendRequest(address, cb);
});
})
});
});

function sendRequest(address, cb) {
http
.request({
hostname: address.address,
port: address.port,
path: '/'
})
.on('response', function(res) {
res.on('end', cb).resume();
})
.on('error', cb)
.end();
}

function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}