0.0.3 • Published 1 year ago

@tangerinelife/fastify-sentry v0.0.3

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

Fastify Sentry

Fastify sentry integration using the sentry SDK, compatible with both fastify v2 and v3

Installation

npm install @tangerinelife/fastify-sentry

Usage

Import the Fastify Sentry library and initialize with the minimum required configuration

import * as FastifySentry from "@tangerinelife/fastify-sentry";

FastifySentry.init({
  service: process.env.APP.NAME,
  release: `${process.env.npm_package_name}@${process.env.npm_package_version}`, // optional but needed for sourceMaps
  dsn: process.env.SENTRY.DSN,
  environment: 'development',
  database: 'postgres', // optional but needed for database calls tracing
  tracesSampleRate: process.env.SENTRY.TRACES_SAMPLE_RATE, // skip this option to disable tracing
  redact: { body: ['Token', 'AuthenticationKey', 'bvn'], queryString: ['token'] }, // optional
  rewriteFrameOptions: { root: __dirname || process.cwd() }, // optional but needed for sourceMaps
});

Attach the handlers to your fastify instance

// We use onRequest hook here to register the request and tracing handlers

fastify.addHook('onRequest', (request, reply, done) => {
  FastifySentry.Handlers.requestHandler(request, reply);

  FastifySentry.Handlers.tracingHandler(request, reply);

  done();
});

// We use setErrorHandler to register the error Handler

fastify.setErrorHandler((error, request, reply) => {
  FastifySentry.Handlers.errorHandler(error, request, reply, {
    shouldHandleError() {
      // you can use this method to determine if an error should be reported, like filtering out certain errors
      return true;
    },
  });
});

// If you are using NestJS, the error handler may be used in a custom exception filter or error interceptor

Sensitive data filtering The redact option we supplied during initialization is used to prevent sensitive data from being sent to sentry. It is optional and some sensible default has been set. Here is the complete object:

redact: {
  header: [] // list of keys to redact from request header,
  body: [] // list of keys to redact from request payload,
  queryString: [] // list of keys to redact from url query string,
  replaceWith: '', // what will replace the redacted field
};

That is all you need to use this package. Start catching bugs and fixing things.

Sentry NodeJS docs

Check out the official sentry NodeJS docs to learn more about sentry https://docs.sentry.io/platforms/node and for sourceMap configuration.

Contributing

Please feel free to fork this package and contribute by submitting a pull request to enhance the functionalities.

Thanks

Thank you for choosing this library