Developer Guide

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.

Next, use the X-Ray SDK for Node.js to instrument your AWS SDK for JavaScript in Node.js clients. Whenever you make a call to a downstream AWS service or resource with an instrumented client, the SDK records information about the call in a subsegment. AWS services and the resources that you access within the services appear as downstream nodes on the service map to help you identify errors and throttling issues on individual connections.

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:

  • cls – 0.1.5

  • continuation-local-storage – 3.2.0

  • pkginfo – 0.4.0

  • underscore – 1.8.3

The SDK pulls these libraries in when you install it with NPM.

To trace AWS SDK clients, the X-Ray SDK for Node.js requires a minimum version of the AWS SDK for JavaScript in Node.js.

  • aws-sdk – 2.7.15

Dependency Management

The X-Ray SDK for Node.js is available from NPM.

For local development, install the SDK in your project directory with npm.

~/nodejs-xray$ npm install aws-xray-sdk
nodejs-xray@0.0.0 ~/nodejs-xray
└─┬ aws-xray-sdk@1.1.0
  ├─┬ continuation-local-storage@3.2.0
  │ ├─┬ async-listener@0.6.3
  │ │ └── shimmer@1.0.0
  │ └── emitter-listener@1.0.1
  ├── moment@2.17.1
  ├── pkginfo@0.4.0
  ├── semver@5.3.0
  ├── underscore@1.8.3
  └─┬ winston@2.3.1
    ├── async@1.0.0
    ├── colors@1.0.3
    ├── cycle@1.0.3
    ├── eyes@0.1.8
    ├── isstream@0.1.2
    └── stack-trace@0.0.9

Use the --save option to save the SDK as a dependency in your application's package.json.

~/nodejs-xray$ npm install aws-xray-sdk --save
nodejs-xray@0.0.0 ~/nodejs-xray
└── aws-xray-sdk@1.1.0