npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@immobiliarelabs/fastify-sentry

v8.0.2

Published

Simple fastify plugin to integrates Sentry error reporting into your services

Downloads

68,260

Readme

Release code style: prettier semantic-release npm (scoped) license

In our Fastify applications, no matter how good our code is sometimes errors happens, we may need to catch and send them to Sentry for further analysis! This plugin aim to do just that, as easily as possible!

Plug, add your Sentry's DSN and you're good to go! This plugin standardize options and payload format then registers a default errorHandler that uses Sentry to report errors, it also decorates the fastify instance with the Sentry object so you can use it for your custom needs.

⚠️ Fastify 4 introduced some breaking changes, please refer to this version support table to find what works best for you!

Table of contents

Installation

You can install it with npm

# lastest stable version
$ npm i -S @immobiliarelabs/fastify-sentry
# latest development version
$ npm i -S @immobiliarelabs/fastify-sentry@next

or yarn

# lastest stable version
$ yarn add @immobiliarelabs/fastify-sentry
# latest development version
$ yarn add @immobiliarelabs/fastify-sentry@next

Migration guides

Please check these migration guides if you are migrating from an older version of the plugin.

Usage

const fastify = require('fastify')();

fastify.register(require('@immobiliarelabs/fastify-sentry'), {
    dsn: '<your sentry dsn>',
    environment: 'production',
    release: '1.0.0',
});

using Sentry in a route handler

const fastify = require('fastify')();

fastify.register(require('@immobiliarelabs/fastify-sentry'), {
    dsn: '<your sentry dsn>',
    environment: 'production',
    release: '1.0.0',
});

fastify.get('/user/:id', async function (req, reply) {
    const user = await getUserFromStorage(req.params.id);
    if (user.blocked) {
        this.Sentry.captureMessage('Blocked user tried to get in');
        ...
    } else {
        ...
    }
});

You can find more examples of usage in the examples folder :wink:.

Benchmarks

As for everything, using Sentry comes at a cost. You can see the benchmarks here

API

This module exports a plugin registration function.

The exported plugin adds the following decorators:

  • fastify.Sentry: a reference to the Sentry instance
  • reply.sentryEventId <string>: the id of a captured exception, accessible when defining a custom errorResponse handler in the plugin options
  • reply.sentryTransaction: the current request transaction, accessible when enabling tracing in Sentry

and adds a custom error handler that reports to Sentry all the errors that have a 5xx status code.

Configuration options

The plugin extends the standard Sentry options object with the following properties:

setErrorHandler

Attach the default error handler to the fastify instance.

type: boolean

default: true

shouldHandleError

Decide if the error should be sent to Sentry

This function is called in the default error handler that the plugin adds.

type: function

parameters:

  • error <FastifyError>
  • request <FastifyRequest>
  • reply <FastifyReply>

returns:

boolean: true if the error should be sent, false otherwise.

default: a function that returns true if the status code of the error is 5xx

errorResponse

Custom handler to respond the client.

This function is called in the dafult error handler right after the exception is captured, so the reply is decorated with event id assigned by the Sentry SDK.

type: function

parameters:

  • error <FastifyError>
  • request <FastifyRequest>
  • reply <FastifyReply>

returns: void

getTransactionName

Get the request transaction name.

type: function

parameters:

  • request <FastifyRequest>

returns: string

extractRequestData

Extract request metadata to attach the Sentry event.

type: function

parameters:

  • request <FastifyRequest>
  • keys <string[]> the fields to extract from the request (headers, method, protocol, url, cookies, query_string, data)

returns: object containing the extracted metadata. It can contain one or more properties named after the keys passed as parameter as well as other properties.

default: a function that returns an object like this:

{
    headers: {},
    method: 'method,
    protocol: 'https,
    cookies: {},
    query_string: {},
    data: 'request body as string'
}

extractUserData

Extract user metadata to attach the Sentry event.

type: function

parameters:

  • request <FastifyRequest>

returns: object containing the extracted metadata.

default: a function that looks for a user object in the request and returns an object like this:

{
    id: '',
    username: '',
    email: '',
}

skipInit

Skip the Sentry.init call that initializes the SDK. Useful if working in an environment that already initializes the Sentry SDK.

type: boolean

default: false

utils

The package has a /utils export that you can import

with CommonJS

const utils = require('@immobiliarelabs/fastify-sentry/utils')

or ESM

import utils from '@immobiliarelabs/fastify-sentry/utils'

and has a set of utilities used internally that can be useful when implementing your custom functions to pass to the plugin initialization.

getTransactionName

This is the default function used to build the transaction name.

extractRequestData

This is the default function used to extract the request metadata.

extractUserData

The default function used to extract the user metadata.

tryToExtractBody

The default function used to extract the body from the request.

extractPathForTransaction

An internal function used to get the transaction name and source.

shouldHandleError

The default function used to decide if an error event should be sent to Sentry.

errorResponse

The default function used to reply to a request that errored.

Compatibility

| | Version | | --- | --- | | fastify | >=4.0.0 | | sentry | ^7.0.0 | | Node.js | >=18 |

Powered Apps

fastify-sentry was created by the amazing Node.js team at ImmobiliareLabs, the Tech dept of Immobiliare.it, the #1 real estate company in Italy.

We are currently using fastify-sentry in our products as well as our internal toolings.

If you are using fastify-sentry in production drop us a message.

Support & Contribute

Made with ❤️ by ImmobiliareLabs & Contributors

We'd love for you to contribute to fastify-sentry! If you have any questions on how to use fastify-sentry, bugs and enhancement please feel free to reach out by opening a GitHub Issue.

License

fastify-sentry is licensed under the MIT license.
See the LICENSE file for more information.