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

Refactor x-ray usage #9

Open
doriansmiley opened this issue Oct 11, 2019 · 0 comments
Open

Refactor x-ray usage #9

doriansmiley opened this issue Oct 11, 2019 · 0 comments
Assignees
Labels
bug Something isn't working help wanted Extra attention is needed
Milestone

Comments

@doriansmiley
Copy link
Owner

doriansmiley commented Oct 11, 2019

Refactor the AbstractController.register function to properly use XRay. Should look like this:

// you can override invoke in a subclass to adjust the return type
    protected async invoke(event): Promise<any> {
        return new Promise<any>((resolve, reject)=>{
            // add X-Ray telemetry passing the subsegment
            AWSXRay.captureAsyncFunc(this.getSegmentName(), async (subsegment) => {
                try {
                    const result = await this.processRequest(event);
                    resolve(result);
                } catch(e) {
                    this.log(LogLevels.ERROR, e.message, event, e);
                    reject(e);
                } finally {
                    subsegment.close();
                }

            });
        });
    }

    protected async processRequest(event): Promise<any> {
        // to open a subsegment for x-ray call AWSXRay.captureAsyncFunc
        // you'll have to promisfy the call or wrap in a promise like invoke
        // log request received
        this.log(LogLevels.INFO, this.getSegmentName() + ' Request received', event);
        try {
            // first validate the incoming request.
            this.checkValidation(event);

            // stub for override
            // in your sub classes you should supply value for result and error
            return null;
        } catch (e) {
            // log error response
            this.log(LogLevels.ERROR, e.message, null, e);
            throw e;
        }
    }

In index.ts make sure we are opening up the main segment and closing it as follows

var AWSXRay = require('aws-xray-sdk');

app.use(AWSXRay.express.openSegment('defaultName'));  // required at the start of your routes

app.get('/', function (req, res) {
  res.render('index');
});

app.use(AWSXRay.express.closeSegment()); //Required at the end of your routes / first in error handling routes

Reference is at: https://docs.aws.amazon.com/xray-sdk-for-nodejs/latest/reference/

@doriansmiley doriansmiley self-assigned this Oct 11, 2019
@doriansmiley doriansmiley added this to the v2 milestone Aug 6, 2020
@doriansmiley doriansmiley added bug Something isn't working help wanted Extra attention is needed labels Aug 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant