The X-Ray SDK for Node.js
The X-Ray SDK for Node.js is a library for Express framework Node.js web applications that provides classes and methods for generating and sending trace data to the X-Ray daemon. Trace data includes information about incoming HTTP requests served by the application, and calls that the application makes to downstream services using the AWS SDK or HTTP clients.
If you use Express, start by adding the SDK as middleware on your application server to trace incoming requests. The middleware creates a segment for each traced request, and completes the segment when the response is sent. While the segment is open you can use the SDK client's methods to add information to the segment and create subsegments to trace downstream calls. The middleware also automatically captures exceptions that your application throws while the segment is open.
The middleware applies sampling rules to incoming requests to determine which requests to trace. You can configure the X-Ray SDK for Node.js to adjust the sampling behavior or to record information about the AWS compute resources on which your application runs.
The X-Ray SDK for Node.js also provides instrumentation for downstream calls to HTTP web APIs and SQL queries. Wrap your HTTP client in the SDK's capture method to record information about outgoing HTTP calls. For SQL clients, use the capture method for your database type.
While a segment is open, you can also create subsegments for any function in your application, and add debug information to segments and subsegments in annotations and metadata. X-Ray indexes annotations for use with expression filters.
Annotations and Metadata
Annotations and metadata are arbitrary text that you add to segments with the X-Ray SDK. Annotations are indexed for use with filter expressions. Metadata are not indexed but can be viewed in the raw segment with the X-Ray console or API. Anyone that you grant read access to X-Ray can view this data.
For reference documentation about the SDK's classes and methods, see the AWS X-Ray SDK for Node.js API Reference.
The X-Ray SDK for Node.js requires Node.js and the following libraries:
The SDK pulls these libraries in when you install it with NPM.
The X-Ray SDK for Node.js is available from NPM.
For local development, install the SDK in your project directory with npm.
npm install email@example.com ~/nodejs-xray └─┬ firstname.lastname@example.org ├─┬ email@example.com │ ├─┬ firstname.lastname@example.org │ │ └── email@example.com │ └── firstname.lastname@example.org ├── email@example.com ├── firstname.lastname@example.org ├── email@example.com ├── firstname.lastname@example.org └─┬ email@example.com ├── firstname.lastname@example.org ├── email@example.com ├── firstname.lastname@example.org ├── email@example.com ├── firstname.lastname@example.org └── email@example.com
--save option to save the SDK as a dependency in your application's
~/nodejs-xray$ npm install aws-xray-sdk
--firstname.lastname@example.org ~/nodejs-xray └── email@example.com