Skip to content

feat(node): Add Fastify integration #9138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed

feat(node): Add Fastify integration #9138

wants to merge 2 commits into from

Conversation

mydea
Copy link
Member

@mydea mydea commented Sep 28, 2023

This adds a new Fastify integration to @sentry/node, which does two things:

  1. Isolate scope per request
  2. Capture errors

Note that it does not support performance monitoring for now, as we're exploring to only add this in the upcoming OTEL-based performance world. But even so, this should already be useful (and can also be used in the node-experimental world).

Usage

const Sentry = require('@sentry/node');
const fastify = require('fastify');

const app = fastify();

Sentry.init({
  integrations: [
    new Sentry.Integrations.Fastify({ fastify: app })
  ],
});

// Add routes etc. to app

ref #4784

@mydea mydea requested review from lforst and AbhiPrasad September 28, 2023 14:58
@mydea mydea self-assigned this Sep 28, 2023
@AbhiPrasad
Copy link
Member

AbhiPrasad commented Sep 28, 2023

I really would like us to avoid the pattern of passing in the app instance to the integration. I know we've done this in previous cases, but it leads to the situation where Sentry isn't the first thing you call in your app, which has other consequences.

Could we work around this somehow? Ideally we can fix all cases of this in v8.

@mydea
Copy link
Member Author

mydea commented Sep 28, 2023

I really would like us to avoid the pattern of passing in the app instance to the integration. I know we've done this in previous cases, but it leads to the situation where Sentry isn't the first thing you call in your app, which has other consequences.

Could we work around this somehow? Ideally we can fix all cases of this in v8.

The challenge I am trying to solve is to make this work in current @sentry/node, but also leverage the error handling for @sentry/node-experimental. And monkey patching there is a bit different as we'll need to make sure to not interact with the OTEL patches etc.

IMHO even with this pattern you can solve this by adding the integration later, like:

Sentry.init({
  integrations: []
});

// later your app is setup
getCurrentHub().getClient().addIntegration(new Sentry.Integrations.Fastify({ fastify: app });

Would this be acceptable in your book? Or would you generally want to avoid any instance passing, in any place?

If we want to prioritize that we do not need to pass the instance in at all, I'll have to see how to do this nicely. FWIW, we can also duplicate the plugin for node-experimental (it's not really that complicated, the error capturing code), though it would be nice to use the same code there I guess.

Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right that getCurrentHub().getClient().addIntegration does work in this case, maybe we should be talking about that more in the docs.

lgtm for errors, nice work!

@mydea mydea force-pushed the fn/fastify branch 2 times, most recently from 1b24faa to c04f1a5 Compare October 3, 2023 10:29
@github-actions
Copy link
Contributor

github-actions bot commented Oct 3, 2023

size-limit report 📦

Path Size
@sentry/browser (incl. Tracing, Replay) - Webpack (gzipped) 84.24 KB (0%)
@sentry/browser (incl. Tracing) - Webpack (gzipped) 31.41 KB (0%)
@sentry/browser - Webpack (gzipped) 22 KB (0%)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (gzipped) 78.77 KB (0%)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (gzipped) 28.59 KB (-0.01% 🔽)
@sentry/browser - ES6 CDN Bundle (gzipped) 21 KB (-0.01% 🔽)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (minified & uncompressed) 254.38 KB (0%)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (minified & uncompressed) 86.66 KB (0%)
@sentry/browser - ES6 CDN Bundle (minified & uncompressed) 62.35 KB (0%)
@sentry/browser (incl. Tracing) - ES5 CDN Bundle (gzipped) 31.45 KB (-0.01% 🔽)
@sentry/react (incl. Tracing, Replay) - Webpack (gzipped) 84.27 KB (0%)
@sentry/react - Webpack (gzipped) 22.05 KB (0%)
@sentry/nextjs Client (incl. Tracing, Replay) - Webpack (gzipped) 102.23 KB (0%)
@sentry/nextjs Client - Webpack (gzipped) 50.99 KB (0%)

@aldy505
Copy link

aldy505 commented Nov 10, 2023

Hello! Is there anything blocking this PR?

@mydea
Copy link
Member Author

mydea commented Nov 10, 2023

Hey! Mostly we are exploring a bit if we want to follow this path or try something else. (e.g. can we automatically set this up without having the user add any plugins manually)! We'll keep you updated here.

scope.setTag('baz', 'wat');
});

await new Promise(resolve => setTimeout(resolve, 10));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require('node:timers/promises').setTimeout exist. We don't need to await new Promise here. Although, it might not work with Node 8.

@csvan
Copy link

csvan commented Dec 22, 2023

Very excited for this - my employer uses Fastify extensively and recently started integrating Sentry. Please let me know if I can assist in any way to get this through.

@mydea mydea closed this Mar 12, 2024
@mydea mydea deleted the fn/fastify branch March 12, 2024 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants