Socket
Socket
Sign inDemoInstall

aws-xray-sdk-express

Package Overview
Dependencies
27
Maintainers
24
Versions
57
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    aws-xray-sdk-express

AWS X-Ray Middleware for Express (Javascript)


Version published
Weekly downloads
274K
increased by4.12%
Maintainers
24
Install size
2.14 MB
Created
Weekly downloads
 

Readme

Source

Requirements

  • 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 - manual and automatic. 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;

Middleware Usage

The X-Ray SDK provides two middlewares: AWSXRay.express.openSegment(<name>) and 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 cls-hooked context may occur.

Sample App

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 / and /directory

var AWSXRay = require('aws-xray-sdk-core');
var xrayExpress = require('aws-xray-sdk-express');
var app = express();

//...

app.use(xrayExpress.openSegment('defaultName'));

app.get('/', function (req, res) {
  var segment = AWSXRay.getSegment();
  segment.addAnnotation('page', 'home');

  //...

  res.render('index');
});

app.get('/directory', function (req, res) {
  var segment = AWSXRay.getSegment();
  segment.addAnnotation('page', 'directory');

  //...

  res.render('directory');
});

app.use(xrayExpress.closeSegment());

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 = require('aws-xray-sdk-core');
var xrayExpress = require('aws-xray-sdk-express');
var app = express();

//...

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

//Required at the start of your routes
app.use(xrayExpress.openSegment('defaultName'));

app.get('/', function (req, res) {
  var segment = req.segment;

  //...

  res.render('index');
});

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

Keywords

FAQs

Last updated on 25 Mar 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc