Since this library uses CLS it appears it does not support Promises and Async/Await in v8 node.
What is the right way to support that? Manual mode? And can someone send an example using manual mode?
Thanks!
Hi Billnbell,
Since Promises are asynchronous in nature, it is required to use captureAsyncFunc() wrapper and manually close segment in then().
Following is the sample example for using Promise in Node SDK.
app.get('/test', function(req,res){
AWSXRay.captureAsyncFunc('promiseS3', function(){
Promise.resolve()
.then (s3Put());
});
});
function s3Put()
{
s3.putObject(params).promise()
.then(function(data){
AWSXRay.getSegment().close(); //remember to close segment, automatically references promiseS3
});
};
Note:
Chained promise is not supported since the call context is not passed to child promise but we are looking into support that use case. You are welcome to submit Pull Request as well.
Thanks!
I would like to share some findings for async/await support here.
The SDK currently relies on CLS to trace the call context. It doesn't work with async/await functionality right now. You can see the discussion here.
We will pay close attention to this issue and update the thread once there is new progress. Thanks for your patience.
@haotianw465 Are you looking into using cls-hooked or other alternatives to pass context? There does not seem to be too much progress on the other discussion in the past couple of months.
@niklasR I don't think we can have our SDK to depend on a forked CLS. And async_hooks provided by Node.js is still in experiment state per https://nodejs.org/api/async_hooks.html while the SDK is in production ready.
Having said that, we can probably look into an opt-in patch to provide a workaround for users that explicitly chose async/await. This workaround will probably have its limitations and break some existing SDK functionalities depend on how the forked CLS and original one diverge. A PR is also welcome.
@haotianw465 I ditched CLS for Zonejs and it works nicely for http request context in my backend Lambda function. I don't use Angular but Zonejs has a lot of support.
We are using cls-hooked instead of CLS for node 8 and higher and has been working fine so far. It would be great if this issue gets fixed so we can use it again with our async functions!
We are using cls-hooked instead of CLS for node 8 and higher
Is that in the context of this package in a way that allows any issues to be worked around?
No I mean the cls-hooked package itself, it's not working with aws-xray-sdk-node. They are still using continuation-local-storage but I think if they replace this with cls-hooked the library should work fine.
I must second that not being able to work with async/await kind of destroys the usability of this package right now. Probably not a long term solution, but using cls-hooked could solve the issue for now, for node 8.
I not sure how to best go about this, but something as trivial as this inside lib/context_utils.js could make the library useful again for us:
var cls = process.env.AWS_XRAY_EXPERIMENTAL_ASYNC_AWAIT ? require('cls-hooked') : require('continuation-local-storage');
Again, probably not a long term solution, but could help right now.
Thank you for your suggestion @nakedible-p. We are actively looking into this option right now and will update the thread once we make progress.
Hello, any update about this?
Closing in favor of https://github.com/aws/aws-xray-sdk-node/issues/60
Most helpful comment
Hello, any update about this?