Skip to content

feat(nitro-utils): Use patchEventHandler from nitro-utils #14612

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

Merged
merged 2 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ Sentry.init({
environment: 'qa', // dynamic sampling bias to keep transactions
tracesSampleRate: 1.0, // Capture 100% of the transactions
tunnel: 'http://localhost:3031/', // proxy server
debug: !!process.env.DEBUG,
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { getDefaultIsolationScope } from '@sentry/core';
import * as Sentry from '@sentry/nuxt';
import { defineEventHandler } from '#imports';

export default defineEventHandler(event => {
Sentry.setTag('my-isolated-tag', true);
Sentry.setTag('my-global-scope-isolated-tag', getDefaultIsolationScope().getScopeData().tags['my-isolated-tag']); // We set this tag to be able to assert that the previously set tag has not leaked into the global isolation scope

throw new Error('Nuxt 3 Server error');
});
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { expect, test } from '@playwright/test';
import { waitForError } from '@sentry-internal/test-utils';
import { waitForError, waitForTransaction } from '@sentry-internal/test-utils';

test.describe('server-side errors', async () => {
test('captures api fetch error (fetched on click)', async ({ page }) => {
const transactionEventPromise = waitForTransaction('nuxt-3-top-level-import', async transactionEvent => {
return transactionEvent?.transaction === 'GET /api/server-error';
});

const errorPromise = waitForError('nuxt-3-top-level-import', async errorEvent => {
return errorEvent?.exception?.values?.[0]?.value === 'Nuxt 3 Server error';
});

await page.goto(`/fetch-server-error`);
await page.getByText('Fetch Server Data', { exact: true }).click();

const transactionEvent = await transactionEventPromise;
const error = await errorPromise;

expect(error.transaction).toEqual('GET /api/server-error');
Expand All @@ -18,6 +23,32 @@ test.describe('server-side errors', async () => {
expect(exception.type).toEqual('Error');
expect(exception.value).toEqual('Nuxt 3 Server error');
expect(exception.mechanism.handled).toBe(false);

expect(error.tags?.['my-isolated-tag']).toBe(true);
expect(error.tags?.['my-global-scope-isolated-tag']).not.toBeDefined();
expect(transactionEvent.tags?.['my-isolated-tag']).toBe(true);
expect(transactionEvent.tags?.['my-global-scope-isolated-tag']).not.toBeDefined();
});

test('isolates requests', async ({ page }) => {
const transactionEventPromise = waitForTransaction('nuxt-3-top-level-import', async transactionEvent => {
return transactionEvent?.transaction === 'GET /api/server-error';
});

const errorPromise = waitForError('nuxt-3-top-level-import', async errorEvent => {
return errorEvent?.exception?.values?.[0]?.value === 'Nuxt 3 Server error';
});

await page.goto(`/fetch-server-error`);
await page.getByText('Fetch Server Data', { exact: true }).click();

const transactionEvent = await transactionEventPromise;
const error = await errorPromise;

expect(error.tags?.['my-isolated-tag']).toBe(true);
expect(error.tags?.['my-global-scope-isolated-tag']).not.toBeDefined();
expect(transactionEvent.tags?.['my-isolated-tag']).toBe(true);
expect(transactionEvent.tags?.['my-global-scope-isolated-tag']).not.toBeDefined();
});

test('captures api fetch error (fetched on click) with parametrized route', async ({ page }) => {
Expand Down
30 changes: 30 additions & 0 deletions packages/nitro-utils/src/nitro/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { GLOBAL_OBJ, flush, getClient, logger, vercelWaitUntil } from '@sentry/core';

/**
* Flushes Sentry for serverless environments.
*/
export async function flushIfServerless(): Promise<void> {
const isServerless = !!process.env.LAMBDA_TASK_ROOT || !!process.env.VERCEL || !!process.env.NETLIFY;

// @ts-expect-error - this is not typed
if (GLOBAL_OBJ[Symbol.for('@vercel/request-context')]) {
vercelWaitUntil(flushWithTimeout());
} else if (isServerless) {
await flushWithTimeout();
}
}

/**
* Flushes Sentry.
*/
export async function flushWithTimeout(): Promise<void> {
const isDebug = getClient()?.getOptions()?.debug;

try {
isDebug && logger.log('Flushing events...');
await flush(2000);
isDebug && logger.log('Done flushing events');
} catch (e) {
isDebug && logger.log('Error while flushing events:\n', e);
}
}
1 change: 1 addition & 0 deletions packages/nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
},
"devDependencies": {
"@nuxt/module-builder": "^0.8.4",
"@sentry-internal/nitro-utils": "8.42.0",
"nuxt": "^3.13.2"
},
"scripts": {
Expand Down
37 changes: 3 additions & 34 deletions packages/nuxt/src/runtime/plugins/sentry.server.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,13 @@
import {
GLOBAL_OBJ,
flush,
getClient,
getDefaultIsolationScope,
getIsolationScope,
logger,
vercelWaitUntil,
withIsolationScope,
} from '@sentry/core';
import { patchEventHandler } from '@sentry-internal/nitro-utils';
import { GLOBAL_OBJ, flush, getClient, logger, vercelWaitUntil } from '@sentry/core';
import * as Sentry from '@sentry/node';
import { H3Error } from 'h3';
import { defineNitroPlugin } from 'nitropack/runtime';
import type { NuxtRenderHTMLContext } from 'nuxt/app';
import { addSentryTracingMetaTags, extractErrorContext } from '../utils';

export default defineNitroPlugin(nitroApp => {
nitroApp.h3App.handler = new Proxy(nitroApp.h3App.handler, {
async apply(handlerTarget, handlerThisArg, handlerArgs: Parameters<typeof nitroApp.h3App.handler>) {
// In environments where we cannot make use of OTel httpInstrumentation, we still need to ensure requests are properly isolated (e.g. when just importing the Sentry server config at the top level instead of `--import`).
// If OTel httpInstrumentation works, requests will be already isolated by the SentryHttpInstrumentation.
// We can identify this by comparing the current isolation scope to the default one. The requests are properly isolated if
// the current isolation scope is different from the default one. If that is not the case, we fork the isolation scope here.
const isolationScope = getIsolationScope();
const newIsolationScope = isolationScope === getDefaultIsolationScope() ? isolationScope.clone() : isolationScope;

logger.log(
`[Sentry] Patched h3 event handler. ${
isolationScope === newIsolationScope ? 'Using existing' : 'Created new'
} isolation scope.`,
);

return withIsolationScope(newIsolationScope, async () => {
try {
return await handlerTarget.apply(handlerThisArg, handlerArgs);
} finally {
await flushIfServerless();
}
});
},
});
nitroApp.h3App.handler = patchEventHandler(nitroApp.h3App.handler);

nitroApp.hooks.hook('error', async (error, errorContext) => {
// Do not handle 404 and 422
Expand Down
Loading