Giter VIP home page Giter VIP logo

fastify-xray's Introduction

fastify-xray

npm version Build Status

Requirements

  • AWS X-Ray SDK Core (aws-xray-sdk-core)
  • Fastify 3.0.0 or greater

AWS X-Ray and Fastify

The AWS X-Ray Fastify package automatically records information for incoming and outgoing requests and responses, via the 'enable' function 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 or subsegment at any time:

const segment = AWSXRay.getSegment();

In manual mode, you can get the base segment off of the request object:

const segment = request.segment;

This plugin should be register as soon as possible after Fastify initialisation.

Plugin usage

Automatic mode examples

For more automatic mode examples, see the example code.

const fastify = require("fastify")();

fastify.register(require("fastify-xray"), {
  defaultName: "My App Name",
});

// Error capturing is attached to the fastify onError hook

Manual mode examples

For more manual mode examples, see manual mode examples. The X-Ray SDK can be used identically inside Restify routes. Note that you don't have to manually start or close the segments since that is handled by the X-Ray middleware.

const fastify = require("fastify")();

fastify.register(require("fastify-xray"), {
  defaultName: "My App Name",
});

//...

fastify.get("/", async function (request, reply) {
  const segment = request.segment;

  //...

  return "hello";
});

// Error capturing is attached to the fastify onError hook

Give your own AWSXRay instance

In case you need to use your own, already configured, AWSRay instance, here is two possibilities.

By passing the AWSXRay instance as plugin option:

const AWSXRay = require('aws-xray-sdk-core')
const fastify = require("fastify")();

// ... configure your AWSXRay instance

fastify.register(require("fastify-xray"), {
  defaultName: "My App Name",
  AWSXRay
});

By decorating the Fastify instance with the AWSXRay instance:

const AWSXRay = require('aws-xray-sdk-core')
const fastify = require("fastify")();

// ... configure your AWSXRay instance

fastify.decorate('AWSXRay', AWSXRay)

fastify.register(require("fastify-xray"), {
  defaultName: "My App Name",
});

License

MIT License

fastify-xray's People

Contributors

jeromemacias avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

fastify-xray's Issues

TypeError when using with [email protected]

Library versions where error is encountered

Error

~/node_modules/aws-xray-sdk-core/lib/utils.js:19
  var stat = status.toString();
                      ^
TypeError: Cannot read property 'toString' of undefined
  at Object.getCauseTypeFromHttpStatus (~/node_modules/aws-xray-sdk-core/lib/utils.js:19:23)
  at Object.fastifyXrayOnResponse (~/node_modules/fastify-xray/plugin.js:84:33)
  at onResponseIterator (~/node_modules/fastify/lib/reply.js:502:10)
  at next (~/node_modules/fastify/lib/hooks.js:70:20)
  at hookRunner (~/node_modules/fastify/lib/hooks.js:84:3)
  at ServerResponse.onResFinished (~/node_modules/fastify/lib/reply.js:485:7)
  at ServerResponse.emit (events.js:203:15)
  at ServerResponse.EventEmitter.emit (domain.js:448:20)
  at ServerResponse.emitted (~/node_modules/emitter-listener/listener.js:134:21)
  at onFinish (_http_outgoing.js:671:10)
  at process._tickCallback (internal/process/next_tick.js:61:11)

From initial investigation, this is because [email protected] does not yet have the getter for statusCode. This results in fastify-xray/plugin.js:84:33 to pass undefined to getCauseTypeFromHttpStatus

node_modules/fastify-xray/plugin.js

const cause = AWSXRay.utils.getCauseTypeFromHttpStatus(reply.statusCode) // reply doesn't have a statusCode getter/property as of [email protected]

node_modules/aws-xray-sdk-core/lib/utils.js

  getCauseTypeFromHttpStatus: function getCauseTypeFromHttpStatus(status) {
    var stat = status.toString();
    if (stat.match(/^[4][0-9]{2}$/) !== null)
      return 'error';
    else if (stat.match(/^[5][0-9]{2}$/) !== null)
      return 'fault';
  }

Quick workaround that I did is to inject a getter for this:

    if (!Reply.prototype.statusCode) {
      Object.defineProperty(Reply.prototype, 'statusCode', {
        get: function() {
          return this.res.statusCode
        }
      })
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.