Cannot Sentry in serverless function

Simple serverless function:

import { VercelRequest, VercelResponse } from '@vercel/node';
import * as Sentry from '@sentry/node';

export default async function functionSentry(
	request: VercelRequest,
	response: VercelResponse,
) {
	Sentry.init({
		dsn: 'https://dsn_value@sentry.onpremise.org/66',
		environment: process.env.NODE_ENV,
		serverName: 'trololo',
		release: '0',
		debug: false,
		// Add Tracing by setting tracesSampleRate
		// We recommend adjusting this value in production
		tracesSampleRate: 1.0,

		// Set sampling rate for profiling
		// This is relative to tracesSampleRate
		profilesSampleRate: 1.0,
	});

	Sentry.captureException(new Error('test'));

	throw new Error('test2');
}

Calling the corresponding endpoint does not generate any issues in Sentry.

Naturally, the Sentry server works, and if the function is run without Vercel, issues are generated in Sentry.

I assume that Vercel kills the function process after exception as well as after response.send(). But, what to do?

1 Like

Hello, same issue here.
I did manage to get errors in though via

import * as Sentry from "@sentry/serverless";

export const initSentry = () => {
    Sentry.init({
        environment: process.env.VERCEL_TARGET_ENV || 'unknown',
        dsn: process.env.SENTRY_DSN,
        tracesSampleRate: 1,
        profilesSampleRate: 1,
    });
};

with

export async function POST(req: Request) {
  initSentry();
   ...

However, we still do not have tracing working.

Emphasis on waitUntil(Sentry.flush(5000)); Code example:

try {

  someFunc();

} catch (error) {
  Sentry.captureException(error, {
    extra: {
    method: request.method,
    url: request.url,
    headers: request.headers,
    body: request.body,
    query: request.query,
    cookies: request.cookies,
    },
  });

  response
    .status((error as ExtError).status || 500)
    .json((error as ExtError).statusText || 'Internal Server Error');
} finally {
  waitUntil(Sentry.flush(5000));
}