Skip to content

Commit 5b9fc05

Browse files
authored
Merge branch 'sig/flush-util' into sig/astro-waitUntil
2 parents d8d1770 + 4f05f12 commit 5b9fc05

File tree

13 files changed

+47
-66
lines changed

13 files changed

+47
-66
lines changed

packages/nextjs/src/common/_error.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { captureException, withScope } from '@sentry/core';
2-
import { vercelWaitUntil } from '@sentry/utils';
2+
import { flushSafelyWithTimeout, vercelWaitUntil } from '@sentry/utils';
33
import type { NextPageContext } from 'next';
4-
import { flushSafelyWithTimeout } from './utils/responseEnd';
54

65
type ContextOrProps = {
76
req?: NextPageContext['req'];

packages/nextjs/src/common/utils/edgeWrapperUtils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import {
99
startSpan,
1010
withIsolationScope,
1111
} from '@sentry/core';
12-
import { vercelWaitUntil, winterCGRequestToRequestData } from '@sentry/utils';
12+
import { flushSafelyWithTimeout, vercelWaitUntil, winterCGRequestToRequestData } from '@sentry/utils';
1313

1414
import type { EdgeRouteHandler } from '../../edge/types';
15-
import { flushSafelyWithTimeout } from './responseEnd';
1615
import { commonObjectToIsolationScope, escapeNextjsTracing } from './tracingUtils';
1716

1817
/**

packages/nextjs/src/common/utils/responseEnd.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import type { ServerResponse } from 'http';
2-
import { flush, setHttpStatus } from '@sentry/core';
2+
import { setHttpStatus } from '@sentry/core';
33
import type { Span } from '@sentry/types';
4-
import { fill, logger } from '@sentry/utils';
5-
6-
import { DEBUG_BUILD } from '../debug-build';
4+
import { fill } from '@sentry/utils';
75
import type { ResponseEndMethod, WrappedResponseEndMethod } from '../types';
86

97
/**
@@ -43,16 +41,3 @@ export function finishSpan(span: Span, res: ServerResponse): void {
4341
setHttpStatus(span, res.statusCode);
4442
span.end();
4543
}
46-
47-
/**
48-
* Flushes pending Sentry events with a 2 second timeout and in a way that cannot create unhandled promise rejections.
49-
*/
50-
export async function flushSafelyWithTimeout(): Promise<void> {
51-
try {
52-
DEBUG_BUILD && logger.log('Flushing events...');
53-
await flush(2000);
54-
DEBUG_BUILD && logger.log('Done flushing events');
55-
} catch (e) {
56-
DEBUG_BUILD && logger.log('Error while flushing events:\n', e);
57-
}
58-
}

packages/nextjs/src/common/utils/wrapperUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import {
1414
withIsolationScope,
1515
} from '@sentry/core';
1616
import type { Span } from '@sentry/types';
17-
import { isString, vercelWaitUntil } from '@sentry/utils';
17+
import { flushSafelyWithTimeout, isString, vercelWaitUntil } from '@sentry/utils';
1818

19-
import { autoEndSpanOnResponseEnd, flushSafelyWithTimeout } from './responseEnd';
19+
import { autoEndSpanOnResponseEnd } from './responseEnd';
2020
import { commonObjectToIsolationScope, escapeNextjsTracing } from './tracingUtils';
2121

2222
declare module 'http' {

packages/nextjs/src/common/withServerActionInstrumentation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import {
55
withIsolationScope,
66
} from '@sentry/core';
77
import { captureException, continueTrace, getClient, handleCallbackErrors, startSpan } from '@sentry/core';
8-
import { logger, vercelWaitUntil } from '@sentry/utils';
8+
import { flushSafelyWithTimeout, logger, vercelWaitUntil } from '@sentry/utils';
99

1010
import { DEBUG_BUILD } from './debug-build';
1111
import { isNotFoundNavigationError, isRedirectNavigationError } from './nextNavigationErrorUtils';
12-
import { flushSafelyWithTimeout } from './utils/responseEnd';
12+
1313
import { escapeNextjsTracing } from './utils/tracingUtils';
1414

1515
interface Options {

packages/nextjs/src/common/wrapApiHandlerWithSentry.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import {
77
withIsolationScope,
88
} from '@sentry/core';
99
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core';
10-
import { isString, logger, objectify, vercelWaitUntil } from '@sentry/utils';
10+
import { flushSafelyWithTimeout, isString, logger, objectify, vercelWaitUntil } from '@sentry/utils';
1111
import type { NextApiRequest } from 'next';
1212
import type { AugmentedNextApiResponse, NextApiHandler } from './types';
13-
import { flushSafelyWithTimeout } from './utils/responseEnd';
1413
import { escapeNextjsTracing } from './utils/tracingUtils';
1514

1615
export type AugmentedNextApiRequest = NextApiRequest & {

packages/nextjs/src/common/wrapRouteHandlerWithSentry.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ import {
1010
withIsolationScope,
1111
withScope,
1212
} from '@sentry/core';
13-
import { propagationContextFromHeaders, vercelWaitUntil, winterCGHeadersToDict } from '@sentry/utils';
13+
import {
14+
flushSafelyWithTimeout,
15+
propagationContextFromHeaders,
16+
vercelWaitUntil,
17+
winterCGHeadersToDict,
18+
} from '@sentry/utils';
1419
import { isNotFoundNavigationError, isRedirectNavigationError } from './nextNavigationErrorUtils';
1520
import type { RouteHandlerContext } from './types';
16-
import { flushSafelyWithTimeout } from './utils/responseEnd';
1721
import {
1822
commonObjectToIsolationScope,
1923
commonObjectToPropagationContext,

packages/nextjs/src/common/wrapServerComponentWithSentry.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@ import {
1313
withIsolationScope,
1414
withScope,
1515
} from '@sentry/core';
16-
import { propagationContextFromHeaders, uuid4, vercelWaitUntil, winterCGHeadersToDict } from '@sentry/utils';
16+
import {
17+
flushSafelyWithTimeout,
18+
propagationContextFromHeaders,
19+
uuid4,
20+
vercelWaitUntil,
21+
winterCGHeadersToDict,
22+
} from '@sentry/utils';
1723

1824
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core';
1925
import { isNotFoundNavigationError, isRedirectNavigationError } from '../common/nextNavigationErrorUtils';
2026
import type { ServerComponentContext } from '../common/types';
21-
import { flushSafelyWithTimeout } from './utils/responseEnd';
2227
import { commonObjectToIsolationScope, commonObjectToPropagationContext } from './utils/tracingUtils';
2328

2429
/**

packages/nuxt/src/server/sdk.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { applySdkMetadata, flush, getGlobalScope } from '@sentry/core';
1+
import { applySdkMetadata, getGlobalScope } from '@sentry/core';
22
import {
33
type NodeOptions,
44
getDefaultIntegrations as getDefaultNodeIntegrations,
55
httpIntegration,
66
init as initNode,
77
} from '@sentry/node';
88
import type { Client, EventProcessor, Integration } from '@sentry/types';
9-
import { logger, vercelWaitUntil } from '@sentry/utils';
9+
import { flushSafelyWithTimeout, logger, vercelWaitUntil } from '@sentry/utils';
1010
import { DEBUG_BUILD } from '../common/debug-build';
1111
import type { SentryNuxtServerOptions } from '../common/types';
1212

@@ -85,16 +85,3 @@ export function mergeRegisterEsmLoaderHooks(
8585
}
8686
return options.registerEsmLoaderHooks ?? { exclude: [/vue/] };
8787
}
88-
89-
/**
90-
* Flushes pending Sentry events with a 2-second timeout and in a way that cannot create unhandled promise rejections.
91-
*/
92-
export async function flushSafelyWithTimeout(): Promise<void> {
93-
try {
94-
DEBUG_BUILD && logger.log('Flushing events...');
95-
await flush(2000);
96-
DEBUG_BUILD && logger.log('Done flushing events');
97-
} catch (e) {
98-
DEBUG_BUILD && logger.log('Error while flushing events:\n', e);
99-
}
100-
}

packages/solidstart/src/server/utils.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
1-
import { flush, getGlobalScope } from '@sentry/node';
1+
import { getGlobalScope } from '@sentry/node';
22
import type { EventProcessor, Options } from '@sentry/types';
3-
import { logger } from '@sentry/utils';
4-
import { DEBUG_BUILD } from '../common/debug-build';
3+
import { flushSafelyWithTimeout, logger } from '@sentry/utils';
54

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

109
if (isServerless) {
11-
try {
12-
DEBUG_BUILD && logger.log('Flushing events...');
13-
await flush(2000);
14-
DEBUG_BUILD && logger.log('Done flushing events');
15-
} catch (e) {
16-
DEBUG_BUILD && logger.log('Error while flushing events:\n', e);
17-
}
10+
await flushSafelyWithTimeout();
1811
}
1912
}
2013

packages/sveltekit/src/server/utils.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { captureException, flush } from '@sentry/node';
2-
import { logger, objectify } from '@sentry/utils';
1+
import { captureException } from '@sentry/node';
2+
import { flushSafelyWithTimeout, objectify } from '@sentry/utils';
33
import type { RequestEvent } from '@sveltejs/kit';
44

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

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

2524
if (!platformSupportsStreaming) {
26-
try {
27-
DEBUG_BUILD && logger.log('Flushing events...');
28-
await flush(2000);
29-
DEBUG_BUILD && logger.log('Done flushing events');
30-
} catch (e) {
31-
DEBUG_BUILD && logger.log('Error while flushing events:\n', e);
32-
}
25+
await flushSafelyWithTimeout();
3326
}
3427
}
3528

packages/utils/src/flush-safely.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { flush } from '@sentry/core';
2+
import { DEBUG_BUILD } from './debug-build';
3+
import { logger } from './logger';
4+
5+
/**
6+
* Flushes pending Sentry events with a default 2-second timeout and in a way that cannot create unhandled promise rejections.
7+
*/
8+
export async function flushSafelyWithTimeout(timeout = 2000): Promise<void> {
9+
try {
10+
DEBUG_BUILD && logger.log('Flushing events...');
11+
await flush(timeout);
12+
DEBUG_BUILD && logger.log('Done flushing events');
13+
} catch (e) {
14+
DEBUG_BUILD && logger.log('Error while flushing events:\n', e);
15+
}
16+
}

packages/utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export * from './breadcrumb-log-level';
44
export * from './browser';
55
export * from './dsn';
66
export * from './error';
7+
export * from './flush-safely';
78
export * from './worldwide';
89
export * from './instrument';
910
export * from './is';

0 commit comments

Comments
 (0)