- AWS X-Ray SDK Core (aws-xray-sdk-core)
- Express 4.14.0 or greater
AWS X-Ray and Express
The AWS X-Ray Express package automatically records information for incoming and outgoing requests and responses, via the middleware functions in this package. To configure sampling, dynamic naming, and more see the set up section.
The AWS X-Ray SDK Core has two modes -
Automatic mode uses the
cls-hooked package and automatically
tracks the current segment and subsegment. This is the default mode.
Manual mode requires that you pass around the segment reference.
In automatic mode, you can get the current segment/subsegment at any time:
var segment = AWSXRay.getSegment();
In manual mode, you can get the base segment off of the request object:
var segment = req.segment;
The X-Ray SDK provides two middlewares:
AWSXRay.express.closeSegment(). These two middlewares must be used together
and wrap all of your defined routes that you'd like to trace.
In automatic mode, the
openSegment middleware must be the last middleware added
before defining routes, and the
closeSegment middleware must be the
first middleware added after defining routes. Otherwise issues with the
context may occur.
To get started with a functional express application instrumented with the X-Ray SDK, check out our sample app.
Automatic mode example
For more automatic mode examples, see the Automatic Mode Examples.
Capture all incoming requests to
var AWSXRay = ;var xrayExpress = ;var app = ;//...app;app;app;app;
Manual mode examples
For more manual mode examples, e.g. what to do with the segment inside your route logic, see the Manual Mode Examples. Note that you don't have to manually start or close the segments since that is handled by the X-Ray middleware.
Capture All incoming requests to
var AWSXRay = ;var xrayExpress = ;var app = ;//...var AWSXRay = ;//Required at the start of your routesapp;app;app; //Required at the end of your routes / first in error handling routes