Skip to content
Closed
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
3 changes: 1 addition & 2 deletions packages/nextjs/src/common/_error.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { captureException, withScope } from '@sentry/core';
import { vercelWaitUntil } from '@sentry/utils';
import { flushSafelyWithTimeout, vercelWaitUntil } from '@sentry/utils';
import type { NextPageContext } from 'next';
import { flushSafelyWithTimeout } from './utils/responseEnd';

type ContextOrProps = {
req?: NextPageContext['req'];
Expand Down
3 changes: 1 addition & 2 deletions packages/nextjs/src/common/utils/edgeWrapperUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import {
startSpan,
withIsolationScope,
} from '@sentry/core';
import { vercelWaitUntil, winterCGRequestToRequestData } from '@sentry/utils';
import { flushSafelyWithTimeout, vercelWaitUntil, winterCGRequestToRequestData } from '@sentry/utils';

import type { EdgeRouteHandler } from '../../edge/types';
import { flushSafelyWithTimeout } from './responseEnd';
import { commonObjectToIsolationScope, escapeNextjsTracing } from './tracingUtils';

/**
Expand Down
19 changes: 2 additions & 17 deletions packages/nextjs/src/common/utils/responseEnd.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import type { ServerResponse } from 'http';
import { flush, setHttpStatus } from '@sentry/core';
import { setHttpStatus } from '@sentry/core';
import type { Span } from '@sentry/types';
import { fill, logger } from '@sentry/utils';

import { DEBUG_BUILD } from '../debug-build';
import { fill } from '@sentry/utils';
import type { ResponseEndMethod, WrappedResponseEndMethod } from '../types';

/**
Expand Down Expand Up @@ -43,16 +41,3 @@ export function finishSpan(span: Span, res: ServerResponse): void {
setHttpStatus(span, res.statusCode);
span.end();
}

/**
* Flushes pending Sentry events with a 2 second timeout and in a way that cannot create unhandled promise rejections.
*/
export async function flushSafelyWithTimeout(): Promise<void> {
try {
DEBUG_BUILD && logger.log('Flushing events...');
await flush(2000);
DEBUG_BUILD && logger.log('Done flushing events');
} catch (e) {
DEBUG_BUILD && logger.log('Error while flushing events:\n', e);
}
}
4 changes: 2 additions & 2 deletions packages/nextjs/src/common/utils/wrapperUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import {
withIsolationScope,
} from '@sentry/core';
import type { Span } from '@sentry/types';
import { isString, vercelWaitUntil } from '@sentry/utils';
import { flushSafelyWithTimeout, isString, vercelWaitUntil } from '@sentry/utils';

import { autoEndSpanOnResponseEnd, flushSafelyWithTimeout } from './responseEnd';
import { autoEndSpanOnResponseEnd } from './responseEnd';
import { commonObjectToIsolationScope, escapeNextjsTracing } from './tracingUtils';

declare module 'http' {
Expand Down
4 changes: 2 additions & 2 deletions packages/nextjs/src/common/withServerActionInstrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
withIsolationScope,
} from '@sentry/core';
import { captureException, continueTrace, getClient, handleCallbackErrors, startSpan } from '@sentry/core';
import { logger, vercelWaitUntil } from '@sentry/utils';
import { flushSafelyWithTimeout, logger, vercelWaitUntil } from '@sentry/utils';

import { DEBUG_BUILD } from './debug-build';
import { isNotFoundNavigationError, isRedirectNavigationError } from './nextNavigationErrorUtils';
import { flushSafelyWithTimeout } from './utils/responseEnd';

import { escapeNextjsTracing } from './utils/tracingUtils';

interface Options {
Expand Down
3 changes: 1 addition & 2 deletions packages/nextjs/src/common/wrapApiHandlerWithSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import {
withIsolationScope,
} from '@sentry/core';
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core';
import { isString, logger, objectify, vercelWaitUntil } from '@sentry/utils';
import { flushSafelyWithTimeout, isString, logger, objectify, vercelWaitUntil } from '@sentry/utils';
import type { NextApiRequest } from 'next';
import type { AugmentedNextApiResponse, NextApiHandler } from './types';
import { flushSafelyWithTimeout } from './utils/responseEnd';
import { escapeNextjsTracing } from './utils/tracingUtils';

export type AugmentedNextApiRequest = NextApiRequest & {
Expand Down
8 changes: 6 additions & 2 deletions packages/nextjs/src/common/wrapRouteHandlerWithSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ import {
withIsolationScope,
withScope,
} from '@sentry/core';
import { propagationContextFromHeaders, vercelWaitUntil, winterCGHeadersToDict } from '@sentry/utils';
import {
flushSafelyWithTimeout,
propagationContextFromHeaders,
vercelWaitUntil,
winterCGHeadersToDict,
} from '@sentry/utils';
import { isNotFoundNavigationError, isRedirectNavigationError } from './nextNavigationErrorUtils';
import type { RouteHandlerContext } from './types';
import { flushSafelyWithTimeout } from './utils/responseEnd';
import {
commonObjectToIsolationScope,
commonObjectToPropagationContext,
Expand Down
9 changes: 7 additions & 2 deletions packages/nextjs/src/common/wrapServerComponentWithSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ import {
withIsolationScope,
withScope,
} from '@sentry/core';
import { propagationContextFromHeaders, uuid4, vercelWaitUntil, winterCGHeadersToDict } from '@sentry/utils';
import {
flushSafelyWithTimeout,
propagationContextFromHeaders,
uuid4,
vercelWaitUntil,
winterCGHeadersToDict,
} from '@sentry/utils';

import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core';
import { isNotFoundNavigationError, isRedirectNavigationError } from '../common/nextNavigationErrorUtils';
import type { ServerComponentContext } from '../common/types';
import { flushSafelyWithTimeout } from './utils/responseEnd';
import { commonObjectToIsolationScope, commonObjectToPropagationContext } from './utils/tracingUtils';

/**
Expand Down
17 changes: 2 additions & 15 deletions packages/nuxt/src/server/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { applySdkMetadata, flush, getGlobalScope } from '@sentry/core';
import { applySdkMetadata, getGlobalScope } from '@sentry/core';
import {
type NodeOptions,
getDefaultIntegrations as getDefaultNodeIntegrations,
httpIntegration,
init as initNode,
} from '@sentry/node';
import type { Client, EventProcessor, Integration } from '@sentry/types';
import { logger, vercelWaitUntil } from '@sentry/utils';
import { flushSafelyWithTimeout, logger, vercelWaitUntil } from '@sentry/utils';
import { DEBUG_BUILD } from '../common/debug-build';
import type { SentryNuxtServerOptions } from '../common/types';

Expand Down Expand Up @@ -85,16 +85,3 @@ export function mergeRegisterEsmLoaderHooks(
}
return options.registerEsmLoaderHooks ?? { exclude: [/vue/] };
}

/**
* Flushes pending Sentry events with a 2-second timeout and in a way that cannot create unhandled promise rejections.
*/
export async function flushSafelyWithTimeout(): Promise<void> {
try {
DEBUG_BUILD && logger.log('Flushing events...');
await flush(2000);
DEBUG_BUILD && logger.log('Done flushing events');
} catch (e) {
DEBUG_BUILD && logger.log('Error while flushing events:\n', e);
}
}
13 changes: 3 additions & 10 deletions packages/solidstart/src/server/utils.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { flush, getGlobalScope } from '@sentry/node';
import { getGlobalScope } from '@sentry/node';
import type { EventProcessor, Options } from '@sentry/types';
import { logger } from '@sentry/utils';
import { DEBUG_BUILD } from '../common/debug-build';
import { flushSafelyWithTimeout, logger } from '@sentry/utils';

/** Flush the event queue to ensure that events get sent to Sentry before the response is finished and the lambda ends */
export async function flushIfServerless(): Promise<void> {
const isServerless = !!process.env.LAMBDA_TASK_ROOT || !!process.env.VERCEL;

if (isServerless) {
try {
DEBUG_BUILD && logger.log('Flushing events...');
await flush(2000);
DEBUG_BUILD && logger.log('Done flushing events');
} catch (e) {
DEBUG_BUILD && logger.log('Error while flushing events:\n', e);
}
await flushSafelyWithTimeout();
}
}

Expand Down
13 changes: 3 additions & 10 deletions packages/sveltekit/src/server/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { captureException, flush } from '@sentry/node';
import { logger, objectify } from '@sentry/utils';
import { captureException } from '@sentry/node';
import { flushSafelyWithTimeout, objectify } from '@sentry/utils';
import type { RequestEvent } from '@sveltejs/kit';

import { DEBUG_BUILD } from '../common/debug-build';
import { isHttpError, isRedirect } from '../common/utils';

/**
Expand All @@ -23,13 +22,7 @@ export async function flushIfServerless(): Promise<void> {
const platformSupportsStreaming = !process.env.LAMBDA_TASK_ROOT && !process.env.VERCEL;

if (!platformSupportsStreaming) {
try {
DEBUG_BUILD && logger.log('Flushing events...');
await flush(2000);
DEBUG_BUILD && logger.log('Done flushing events');
} catch (e) {
DEBUG_BUILD && logger.log('Error while flushing events:\n', e);
}
await flushSafelyWithTimeout();
}
}

Expand Down
16 changes: 16 additions & 0 deletions packages/utils/src/flush-safely.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { flush } from '@sentry/core';
import { DEBUG_BUILD } from './debug-build';
import { logger } from './logger';

/**
* Flushes pending Sentry events with a default 2-second timeout and in a way that cannot create unhandled promise rejections.
*/
export async function flushSafelyWithTimeout(timeout = 2000): Promise<void> {
try {
DEBUG_BUILD && logger.log('Flushing events...');
await flush(timeout);
DEBUG_BUILD && logger.log('Done flushing events');
} catch (e) {
DEBUG_BUILD && logger.log('Error while flushing events:\n', e);
}
}
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './breadcrumb-log-level';
export * from './browser';
export * from './dsn';
export * from './error';
export * from './flush-safely';
export * from './worldwide';
export * from './instrument';
export * from './is';
Expand Down
Loading