I have below questions specific to out project.
@nagendradarla
Are you looking for an example of how to instrument your code that is using the AWS SDK?
Are you also using a framework like express to process incoming requests, or do you want an example that's just an app using the AWS SDK?
Hi @chrisradek
Thanks for replying. I am looking for both.
I need step by step example on how to use aws-sdk with node in my project. I want to what and where the changes have to done.
I might be using express as well at later point of time.
I added below code in one of the *.js file which is entry point for my application
var AWSXRay = require('aws-xray-sdk');
AWSXRay.middleware.setSamplingRules('xray-rule.json');
var logger = require('winston');
AWSXRay.setLogger(logger);
I am getting below exception:
/TestProject-Lambda/node_modules/hapi/lib/protect.js:80:17)',
'at Server.
'at emitTwo (events.js:126:13)',
'at Server.emit (events.js:214:7)',
'at parserOnIncoming (_http_server.js:634:12)' ]
Serverless: Replying error in handler
@nagendradarla
Here's a simple example of how to use xray with the AWS SDK.
// wrapper.js - helper function to create initial segment
const xray = require('aws-xray-sdk');
function traceWrapper(task) {
const namespace = xray.getNamespace();
const rootSegment = new xray.Segment('Root');
namespace.run(() => {
xray.setSegment(rootSegment);
task();
});
process.once('beforeExit', () => {
rootSegment.close();
});
}
module.exports = {
traceWrapper: traceWrapper
};
// index.js - The main script to run
const {traceWrapper} = require('./wrapper');
const AWS = require('aws-sdk');
const xray = require('aws-xray-sdk');
xray.captureAWS(AWS);
xray.setLogger(console);
traceWrapper(function() {
const s3 = new AWS.S3({region: 'us-east-1'});
s3.listBuckets((err, data) => {
if (err) {
console.error(err);
} else {
console.log(data);
}
});
});
Assuming you have these files and the AWS XRay daemon runing, node index.js should print your S3 buckets and send trace data to XRay.
XRay requires a root Segment to signify the start of a trace. The traceWrapper function in my example creates the segment and that segment is used until the process exits.
Typically when using a framework like express, you'll want a trace to represent the actions that occur from the point that you receive a request from a client, to the point you send the response back, so you'll have a separate 'root' segment for each request you process. The XRay SDK has some helpers to make this easier, for example with express.
It looks like you might be using hapi and getting this error. If my above example doesn't help you resolve that, can you share a sample so I can reproduce your issue?
Thanks @chrisradek
@nagendradarla
When you're running inside a Lambda function and have xray enabled, then the xray sdk will automatically create the 'root' segment for you. Lambda provides the data the Xray SDK needs to be able to construct this. You don't need to write a wrapper in this case.
Have you already given that a try? I would recommend first testing your Lambda function using callbacks (instead of promises/ async await) to verify that tracing is working.
Hi @chrisradek
Can you let me know how to use XRay-Sdk with promises and async calls.
We have only promises and async functions in my current project.
@nagendradarla
Sorry for the delayed response.
If you're using async functions, then you'll want to depend on the @experimental version of the xray sdk. When you npm install the xray-sdk, just append @experimental to it. Internally, it depends on cls-hooks (instead of continuation-local-storage) which supports async/await.
You can use the stable version of the xray-sdk with promises (just not async/await) if you call AWSXRay.capturePromise() to patch the global Promise object after importing the xray SDK.
Is there a wait to await or have callback in segment/subsegment close function?
We'll revisit this issue you have created #85
Most helpful comment
@nagendradarla
Sorry for the delayed response.
If you're using
asyncfunctions, then you'll want to depend on the@experimentalversion of the xray sdk. When you npm install the xray-sdk, just append@experimentalto it. Internally, it depends on cls-hooks (instead of continuation-local-storage) which supports async/await.You can use the stable version of the xray-sdk with promises (just not async/await) if you call AWSXRay.capturePromise() to patch the global Promise object after importing the xray SDK.