sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

@sentry/nextjs not reporting issues i serverless functions

Open ThePaulMcBride opened this issue 5 years ago • 28 comments

  • [x] Review the documentation: https://docs.sentry.io/
  • [x] Search for existing issues: https://github.com/getsentry/sentry-javascript/issues
  • [x] Use the latest release: https://github.com/getsentry/sentry-javascript/releases
  • [x] Provide a link to the affected event from your Sentry account

Package + Version

  • [ ] @sentry/browser
  • [ ] @sentry/node
  • [ ] raven-js
  • [ ] raven-node (raven for node)
  • [x] other:

Version:

6.5.1

Description

I've recently started using the @sentry/nextjs package for a Nextjs site. I've followed through with the installation guide with one exception. I'm using environment variables instead of the file generated by the wizard. On the client side it works great, the releases and bugs are being tracked, but I'm getting no reports from my serveless functions when they throw errors.

import { withSentry } from "@sentry/nextjs";

const handler = async (req, res) => {
  throw new Error("API throw error test");
  res.status(200).json({ name: "John Doe" });
};

export default withSentry(handler);

This is the serverless function I am testing and nothing is showing up in my Sentry dashboard when it fails.

ThePaulMcBride avatar Jun 03 '21 11:06 ThePaulMcBride

Hey, thank you for reporting. Could you share your next config with us? Also, is this happening during local development, or when deployed on something like Vercel?

AbhiPrasad avatar Jun 03 '21 13:06 AbhiPrasad

Sure, The contents on my next.config.js is below...

const { withSentryConfig } = require("@sentry/nextjs");
const withImages = require("next-images");
const withBundleAnalyzer = require("@next/bundle-analyzer")({
  enabled: process.env.ANALYZE === "true",
});
const webpack = require("webpack");
const withPlugins = require("next-compose-plugins");
const withTM = require("next-transpile-modules")(["@medv/finder"]);

const basePath = "";

const nextConfig = {
  basePath,
  productionBrowserSourceMaps: true,
  images: {
    domains: ["x", "y", "z"],
  },
  webpack(config, options) {
    config.module.rules.push({
      test: /\.ejs$/,
      use: {
        loader: "ejs-compiled-loader",
        options: {
          htmlmin: true,
          htmlminOptions: {
            removeComments: true,
          },
        },
      },
    });

    config.module.rules.push({
      test: /\.worker\.js$/,
      use: { loader: "worker-loader" },
    });

    config.plugins.push(
      new webpack.ProvidePlugin({
        _: "underscore",
      })
    );

    config.plugins.push(
      new options.webpack.DefinePlugin({
        "process.env.NEXT_IS_SERVER": JSON.stringify(
          options.isServer.toString()
        ),
      })
    );

    return config;
  },

  async redirects() {
    return [
      {
        source: "/",
        destination: `${process.env.API_URL}/`,
        permanent: true,
      },
      {
        source: "/blog",
        destination: `${process.env.API_URL}/blog`,
        permanent: true,
      },
      {
        source: "/blog/:slug*",
        destination: `${process.env.API_URL}/blog/:slug*`,
        permanent: true,
      },
      {
        source: "/changelog",
        destination: "/changelog/page/1",
        permanent: true,
      },
    ];
  }
};

const SentryWebpackPluginOptions = {};

// Make sure adding Sentry options is the last code to run before exporting, to
// ensure that your source maps include changes from all other Webpack plugins
module.exports = withPlugins(
  [
    withBundleAnalyzer,
    withImages,
    withTM,
    [withSentryConfig, SentryWebpackPluginOptions],
  ],
  nextConfig
);

This happens when deployed to vercel, though it doesn't report errors locally either (is it supposed to?).

ThePaulMcBride avatar Jun 03 '21 13:06 ThePaulMcBride

same issue here, any luck?

a-ayala avatar Jun 04 '21 11:06 a-ayala

No, I've not managed to get it working yet

ThePaulMcBride avatar Jun 04 '21 11:06 ThePaulMcBride

6.5.0 originally broke all serverless functions. 6.5.1 definitely broke reporting. Any serverless function wrapped with withSentry that throws an error will not trigger a Sentry error in production. Reverting back to 6.4.1 has fixed this

nickfreeman avatar Jun 09 '21 19:06 nickfreeman

For context: we had this issue when we first manually integrated Sentry with Vercel (before the Next.js plugin). The issue, as I remember, is that Sentry queues up exceptions to be sent to the server but Vercel terminates the function instantaneously — causing the exception to be lost.

Adding await Sentry.flush(2000) to the error path fixed the issue, the Next.js library should probably do that by default.

DavidChouinard avatar Jun 24 '21 17:06 DavidChouinard

@DavidChouinard interesting, having no issues with error logging for serverless Vercel functions in 6.4.1 but also tried 6.7.2 yesterday which was unable to log exceptions for serverless functions

nickfreeman avatar Jun 24 '21 17:06 nickfreeman

For me, all serverless endpoints are broken when wrapping them with withSentry. See https://github.com/getsentry/sentry-javascript/issues/3750

Reverting to 6.4.1 didn't change anything.

Vadorequest avatar Jun 25 '21 10:06 Vadorequest

Yeah I have this running on a few projects hosted on Vercel and the server side error are never reported

ThePaulMcBride avatar Jun 25 '21 10:06 ThePaulMcBride

@ThePaulMcBride Maybe this comment might give you some direction. https://github.com/getsentry/sentry-javascript/issues/3691#issuecomment-868377000

I only fixed this issue myself yesterday, it's under-documented, unfortunately.

Vadorequest avatar Jun 25 '21 12:06 Vadorequest

I would have thought that this would be handled in the withSentry wrapped function. I assume it is already catching any errors that bubble up so should have the opportunity to flush them to sentry before rethrowing/handling the error.

That way the serverless function wouldn't be terminated before the error is reported. I wonder if it would be possible to make it work like that?

ThePaulMcBride avatar Jun 25 '21 12:06 ThePaulMcBride

About a month ago a change was made to the withSentry function to add a blind try/catch around the flush call. The change was labeled:

It seems that awaiting a flush() call is throwing errors in Webpack 5. This crashes the server as the handler has already sent a response at that point. This patch wraps the flush call in a try, catch until we can figure out a better fix.

It seems like the flush call is probably failing silently and causing the symptoms we're seeing here. Don't know what the solution is but seems like a good place to start digging. (cc. @AbhiPrasad)

DavidChouinard avatar Jun 27 '21 03:06 DavidChouinard

Possibly related issue

DavidChouinard avatar Jun 28 '21 16:06 DavidChouinard

Hey everyone, thanks for all the comments and feedback.

We are currently working on a patch to fix these issues (and some of the linked issues) by changing how we do server side initialisation of the Sentry SDK - we hope to push something out ASAP.

AbhiPrasad avatar Jun 28 '21 16:06 AbhiPrasad

Is it still recommended to use { target: 'experimental-serverless-trace' } as stated on the Sentry + Next.js manual setup docs?

Screen Shot 2021-06-29 at 5 24 17 pm

Gomah avatar Jun 29 '21 03:06 Gomah

Not sure what kind of help this is but a current temporary solution I am using is to manually check for client and call Sentry.Init manually if not present.

// Added to the top of my route handlers
if (!Sentry.getCurrentHub().getClient()) {
  console.log('Manual Sentry.Init')
  Sentry.init({ dsn: process.env.NEXT_PUBLIC_SENTRY_DSN })
}

Not exactly understanding what init does, I dont know the the real performance hit I am taking with this manual hack but it appears to be adding around ~100ms delay

@Gomah the experimental flag seemed to have not effect for me.

tomrule007 avatar Jul 06 '21 04:07 tomrule007

@AbhiPrasad any update?

DavidChouinard avatar Jul 13 '21 02:07 DavidChouinard

Hey. The release in 6.9.0 reworked our server side init as per https://github.com/getsentry/sentry-javascript/pull/3786. This means you should start getting transactions/errors in Vercel serverless functions. We are still investigating some issues with flushing events, you can see the PR description for details, but things should work now.

AbhiPrasad avatar Jul 14 '21 11:07 AbhiPrasad

@AbhiPrasad How bad are these flushing issues? Do you have a timeline as to when these issues will be resolved?

I think getting some issues reported is better than none, but flushing issues still seems like a huge concern to me.

alexbchr avatar Jul 14 '21 12:07 alexbchr

We have a fix on the way for the flushing issues, but no timeline at the moment. Will update this issue as soon as that is merged and released.

AbhiPrasad avatar Jul 14 '21 12:07 AbhiPrasad